简体   繁体   English

将带有选择器的参数传递给自定义对象的新.xib

[英]Passing arguments with a selector to new .xib of a custom object

I have a tableview that will load a new .xib when pressed. 我有一个tableview,按下时将加载一个新的.xib。 The issue is, I need to pass a custom object over to the new .xib. 问题是,我需要将自定义对象传递给新的.xib。 So far, I have: 到目前为止,我有:

-(UITableViewCell *)doTestCellForTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {

 //some other code for current view
 TestObj *testobj = [[TestObj alloc]init];

 if(shouldGo){
   cell.userInteractionEnabled = YES;
   UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadTestView:)];
   tapped.numberOfTapsRequired = 1;
   [cell addGestureRecognizer:tapped];
 }

And in that method: 在这种方法中:

-(void)loadTestView:(TestObj *)withTestObj{

TestViewController *newView =
[[TestViewController alloc] init];
//Set the property of type TestObj
newView.testobjprop = withTestObj;

[self presentViewController:newView animated:YES completion:nil];
}

It throws NSInvalidArgumentException. 它引发NSInvalidArgumentException。 How can I make it to where the selector will send "testobj" that was initialized in the tableview method so I can access it there? 如何才能使选择器将其发送到在tableview方法中初始化的“ testobj”的位置,以便可以在那里访问它?

The new .xib loads in just fine if I comment out "newView.testobjprop = withTestObj;" 如果我注释掉“ newView.testobjprop = withTestObj;”,新的.xib加载就很好。 in the loadTestView method. 在loadTestView方法中。 So I assume the issue lies there, and it is a matter of how to pass an object from one .xib to another. 因此,我认为问题就在那儿,这与如何将一个对象从一个.xib传递到另一个有关。

The argument from -(void)loadTestView:(TestObj *)withTestObj is UIGestureRecognizer type so that's why you get NSInvalidArgumentException . -(void)loadTestView:(TestObj *)withTestObjUIGestureRecognizer类型,因此这就是为什么要获取NSInvalidArgumentException的原因。

If you change it to -(void)loadTestView:(UITapGestureRecognizer *)withTestObj it will be the normal behavior. 如果将它更改为-(void)loadTestView:(UITapGestureRecognizer *)withTestObj ,将是正常行为。

That's why you need to use didSelectRow:AtIndexPath so you can pass the correct value to the viewcontroller . 因此,您需要使用didSelectRow:AtIndexPath以便可以将正确的值传递给viewcontroller

Here you aren't using TestObj *testobj = [[TestObj alloc]init]; 这里您没有使用TestObj *testobj = [[TestObj alloc]init]; at all. 完全没有

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM