-1

I have followed the tutorial Passing Data between View Controllers section Passing Data Forward. My code is:

MasterViewController.h:

-(void)pushViewController: (UIViewController *)detailVC animated:(BOOL)animated;

MasterViewController.m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    NSInteger num = indexPath.row;

    DetailViewController *detailVC  = [[DetailViewController alloc] initWithNumber:indexPath.row];
    detailVC.number = num;

    [self pushViewController:detailVC animated:YES];

}

I want to pass the integer num to my DetailViewController. But I am getting Thread 1: SIGABRT. What is happening?

Community
  • 1
  • 1
user1189952
  • 157
  • 4
  • 16

3 Answers3

1

MasterViewController doesn't inherit from UINavigationController.

You can't push to the navigation stack if there isn't one.

You can either add one, or present the new VC, depending upon what the design of your GUI is intended to be.

P.S. WHat's the point in setting the number twice as you are:

   NSInteger num = indexPath.row;
    DetailViewController *detailVC  = [[DetailViewController alloc] initWithNumber:indexPath.row];
    detailVC.number = num;

You either set it with init or set it directly, you're doing it both ways

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • If I try [self presentViewController:detailVC animated:YES completion:nil]; and tap one a cell in the table, the right page is shown, but there is no back-button anymore. Can you tell me what happens? – user1189952 Apr 15 '14 at 19:33
  • Because the back button is managed by the navigation controller (as part of the navigation bar). If you do actually already have a navigation controller somewhere in your app (i.e. maybe MasterViewController is itself pushed onto the navigation stack or embedded in one then you can get a handle to it and then do the push if you want the back button) – Gruntcakes Apr 15 '14 at 19:38
  • I do not understand this. What do I have to do exactly? – user1189952 Apr 15 '14 at 19:41
  • So I have to use pushViewController? Now that is not working: it gives me the crash. – user1189952 Apr 15 '14 at 19:57
  • If you use Xcode to create a new project of type Master-Detail application it will create a UITableViewController for you which is already embedded in a UINavigationController (which is set as the RVC in the storyboard). If you use this as your template to build your app it will help you so I suggest you do this. If you already have this template set up then my answer is not the correct reason for your problem. See if this line works [self.navigationController push….] – Gruntcakes Apr 15 '14 at 19:57
  • See if [self.navigationController pushViewController:detailVC animated:YES];works already without having to change anything else. – Gruntcakes Apr 15 '14 at 19:58
  • That is not showing the right ViewController for each cell and it crashes on tapping the back-button. – user1189952 Apr 15 '14 at 20:02
  • Then create the master-detail application project template, and replace the created table view controller and detail view controller that Xcode creates for you with your own view controllers and then you will have a navigation controller available within the app to use. – Gruntcakes Apr 15 '14 at 20:04
  • I think on declaring DetailViewController *detailVC = ... there is created another DetailViewController than the one I want to load. Then on tapping on a cell this is completely ignored and the old DetailViewController is loaded. Am I right? – user1189952 Apr 15 '14 at 20:13
  • I've no idea, the code you posted doesn't give any hint that there even are actually both new and old DetailViewControllers. In your code you are simply creating a VC and attempting to push it to the view controller. – Gruntcakes Apr 15 '14 at 20:20
  • But then how to affect my DetailViewController.m by pushing? – user1189952 Apr 15 '14 at 20:23
  • I've no idea what your question has turned into. Affect it how? You started off with one question but have only just recent revealed there's actually more to your code then you showed and now are asking something entirely different. Maybe you should start a brand new question on here stating what it is you actually want to do. – Gruntcakes Apr 15 '14 at 20:26
  • Like I said in the question: I just want to pass an integer to DetailViewController.m and then load different pages on tapping different cells. The integer is indexPath.row. In DetailViewController there is an if(number == 0) else if(number == 1) etc. – user1189952 Apr 15 '14 at 20:32
  • well, the loaded DetailViewController must look different for different cells tapped. In principle it is the same DetailViewController.m – user1189952 Apr 15 '14 at 20:40
  • How can I make detailVC persist beyond the lifetime of didSelectRowAtIndexPath then? – user1189952 Apr 15 '14 at 20:43
  • If you want the view controller to look different for each cell then you already are passing it the row number so you can just continue what you are doing now and in DetailViewController's viewWillAppear you can change the appearance depending upon what the row number is. – Gruntcakes Apr 15 '14 at 21:13
  • The line [self.navigationController pushViewController:detailVC animated:YES]; is not working properly. So the DetailViewController.m is not receiving number. – user1189952 Apr 15 '14 at 21:38
  • Which is why I said create the Master-Detail template in Xcode. – Gruntcakes Apr 15 '14 at 21:39
  • I tried to change the prototype-cell in Storyboard, and the DetailViewController, but it still crashes on the back-button. – user1189952 Apr 15 '14 at 21:56
0

My best guess (without seeing your @interface for DetailViewController) is that it's simply a matter of the detailVC instance going away after you put it on screen.

Simple solution: use an instance variable instead:

@interface WhateverClassYouAreIn
@property (nonatomic, strong) DetailViewController *detailVC
@end

// ...
self.detailVC  = [[DetailViewController alloc] initWithNumber:indexPath.row];

The reason this is happening is that without the instance variable, the storage duration of the detailVC is the scope of the method in which it is declared. In general, any VC that goes on screen should be held 'strongly' by something else, all the way back to the root vc.

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
-1

Is the method "initWithNumber" defined in DetailViewController.h ?

@interface DetailViewController : UIViewController
- (instancetype)initWithNumber:(NSInteger)number;
@end
Emilie
  • 2,413
  • 13
  • 12