문제

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?

도움이 되었습니까?

해결책 2

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.

다른 팁

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

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

@interface DetailViewController : UIViewController
- (instancetype)initWithNumber:(NSInteger)number;
@end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top