麻烦从UINavigationController弹出视图控制器

时间:2009-11-02 15:07:34

标签: iphone objective-c iphone-sdk-3.0

我无法从UINavigation控制器弹出UIViewController,即使对nav的引用似乎是正确的,并且viewControllers上的计数显示正确的数字。

我有一个UINavController获取它的第一个UIViewController集,然后导航控制器以模态视图显示:

workoutNavController.viewControllers = [NSArray arrayWithObject:manageWorkoutController];
[self presentModalViewController:workoutNavController animated:YES];

第一个UIViewController(manageWorkoutController)有一个表视图,当其中一个单元被触摸时,第二个UIViewController被推到导航控制器上:

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

    switch (indexPath.row) {

        case 0:
            //do nothing no nav-view here
            break;

        //DIFFICULTY
        case 1:
            workoutDifficultyController.title = @"Workout Difficulty";
            [(UINavigationController *)self.parentViewController pushViewController:workoutDifficultyController 
                                                                           animated:YES];
            break;

        //DATE
        case 2:
            workoutDateController.title = @"Workout Date";
            [(UINavigationController *)self.parentViewController pushViewController:workoutDateController 
                                                                           animated:YES];
            break;

        default:
            break;

    }//end switch

}//end didSelectRowAtIndexPath

在第二个UIViewController(workoutDifficultyController)中,我在viewDidLoad方法中设置了一个Cancel和Save UIBarButtonItem,这样取消按钮就会覆盖导航栏上的默认后退按钮。

- (void)viewDidLoad {

    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelDifficulty:)];
    [self.navigationItem setLeftBarButtonItem:cancelButton];
    [cancelButton release];

    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveDifficulty:)];
    [self.navigationItem setRightBarButtonItem:saveButton];
    [saveButton release];

    [super viewDidLoad];

}//end viewDidLoad

我的问题是在cancelDifficulty方法中,我想从导航控制器弹出当前视图并返回,我不断收到'EXEC_BAD_ACCESS'错误。这是方法:

-(IBAction)cancelDifficulty:(id)sender {
    [(UINavigationController *)self.parentViewController popViewControllerAnimated:YES];
}//cancelDifficulty

我已经尝试将方法链接回实际拥有navController的类,以及使用popToViewController:Animated:。如果我使用NSLog从cancelDifficulty方法打印出viewControllers计数,它会显示正确的数字,但如果我尝试与它进行交互,我要么得到EXEC_BAD_ACCESS,要么根本没有任何事情发生。

2 个答案:

答案 0 :(得分:2)

为什么不直接访问视图控制器的navigationController属性,而不是将其parentViewController强制转换为UINavigationController(不管怎样都是个坏主意。)

答案 1 :(得分:0)

我会尝试以下操作,看看它是否有效:

-(IBAction)cancelDifficulty:(id)sender {
    [(UINavigationController *)self.navigationController popViewControllerAnimated:YES];
}//cancelDifficulty

可能是parentViewController实际上不是UINavigationController