简体   繁体   English

为什么我的 prepareForSegue 会崩溃

[英]why does my prepareForSegue Crash

I am trying to pass in IndexPath through a segue though a navigationController, but it crashes on line:我试图通过一个导航控制器通过 segue 传递 IndexPath,但它在线崩溃:

view.selectedAccount = self.selectedAccountRow;

thanks谢谢

-(IBAction)changeButtonPressed:(id)sender {

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.accountTableView];
NSIndexPath *indexPath = [self.accountTableView indexPathForRowAtPoint:buttonPosition]
self.indexPath = indexPath;
NSLog(@"%ld",(long)indexPath.row);
}

and

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [super prepareForSegue:segue sender:sender];
    ViewController2 *view = [segue destinationViewController];
    view.selectedAccount = self.indexPath;
}

answer here: Set NSString Object, prepareForSegue through UINavigationController在这里回答: 通过 UINavigationController 设置 NSString 对象,prepareForSegue

I see many errors:我看到很多错误:

self.selectedAccountRow never gets defined, change to this: self.selectedAccountRow永远不会被定义,改为:

-(IBAction)changeButtonPressed:(id)sender {
  CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.accountTableView];
  NSIndexPath *indexPath = [self.accountTableView indexPathForRowAtPoint:buttonPosition]
  self.selectedAccountRow = indexPath;
}

When using [segue destinationViewController] you need to ensure that the class your assingning is the right one:使用 [segue destinationViewController] 时,您需要确保您指定的类是正确的:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  ViewController2 *view = (ViewController2*)[segue destinationViewController];
  view.selectedAccount = self.selectedAccountRow;
}

There's no need to declare the as strong, as it is the default one, but it's recommender as it's easier to read, and it's a good coding practice.没有必要声明为强,因为它是默认的,但它是推荐的,因为它更容易阅读,而且它是一种很好的编码实践。

@property (strong, nonatomic) NSIndexPath *selectedAccount;

You need to name better your classes and properties, if you declare a viewController subclass, the name should be self explanatory, maybe you can name it detailViewController instead on viewController2 .你需要更好地命名你的类和属性,如果你声明一个viewController子类,这个名字应该是不言自明的,也许你可以在viewController2它命名为detailViewController

Also, you should consider using a 2-3 letters prefix for your classes, please read good obj-c practices in here: Objective-C Good practices此外,您应该考虑为您的类使用 2-3 个字母前缀,请在此处阅读良好的 obj-c 实践: Objective-C Good Practice

Right click on your view in IB and remove and instances where there is a yellow warning sign.右键单击您在 IB 中的视图并删除有黄色警告标志的实例。 Run and everything should work.运行,一切都应该工作。

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

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