-3

I am trying to pass in IndexPath through a segue though a navigationController, but it crashes on line:

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

Community
  • 1
  • 1
James Walker
  • 69
  • 1
  • 8

2 Answers2

0

I see many errors:

self.selectedAccountRow never gets defined, change to this:

-(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:

-(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.

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

Fantini
  • 2,067
  • 21
  • 32
  • i have hacked my code together, i work for a company so can't just copy the code. but it still crashed uses you advice – James Walker May 04 '15 at 14:10
  • I had an error, I've just edited my answer, in the second code snippet I've changed my code to `view.selectedAccount = self.selectedAccountRow;` if this doesn't fix your error can you please gave us more details?, or the new error you are getting. – Fantini May 04 '15 at 14:14
  • i have done that, still getting the "unrecognized selector sent to instance" – James Walker May 04 '15 at 14:22
  • Where did you called `[self performSegueWithIdentifier:sender:]`? from the IB? – Fantini May 04 '15 at 14:28
  • inside -(IBAction)changeButtonPressed:(id)sender – James Walker May 04 '15 at 14:44
  • Can you write down the code you are using to do the segue?, I can't see it on your question. If you implementation of the segue is wrong, the segue.destinationViewController could be nil, or it could be another controller, if that is the case, you WILL get the `unrecognized selector sent to instance` error because that other controller doesn't have the property you're trying to assign. – Fantini May 04 '15 at 15:01
  • code to perform the segue `[self performSegueWithIdentifier:@"toDetails" sender:self]; ` – James Walker May 04 '15 at 15:27
  • In the Storyboard, the destinationViewController has the right class assigned? (ViewController2) – Fantini May 04 '15 at 15:28
  • yes, the segue works fine without passing the value and the class is assigned properly – James Walker May 04 '15 at 15:37
  • Put a breakpoint on `view.selectedAccount = self.selectedAccountRow;` and see if `view` class is `ViewController2` – Fantini May 04 '15 at 15:41
  • (lldb) po view.class gives UINavigationController presume this is why it is failing? – James Walker May 04 '15 at 15:48
  • okay so its because the segue is to a navigation controller and then to ViewController2. Do i need to pass the variable to the navigationController and then to ViewController2? – James Walker May 04 '15 at 16:32
  • http://stackoverflow.com/questions/29187945/set-nsstring-object-prepareforsegue-through-uinavigationcontroller for answer – James Walker May 04 '15 at 16:35
  • Nice, I hope I helped somehow :). – Fantini May 04 '15 at 18:23
-2

Right click on your view in IB and remove and instances where there is a yellow warning sign. Run and everything should work.

spogebob92
  • 1,474
  • 4
  • 23
  • 32