开发者

trouble popping view controller off of UINavigationController

I'm having trouble popping a UIViewController off of a UINavigation controller even though the reference to the nav seems to be correct and a count on viewControllers shows the right number.

I have a UINavController that gets it's first UIViewController set and then the nav controller is presented in a modal view:

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

That first UIViewController (manageWorkoutController) has a table view, and when one of the cells is touched a 2nd UIViewController is pushed onto the Navigation Controller:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSInd开发者_如何学GoexPath *)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

In the 2nd UIViewController (workoutDifficultyController) I'm setting up a Cancel and Save UIBarButtonItem in the viewDidLoad method, so that cancel button over-rides the default back button on the Nav Bar.

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

My problem is that in the cancelDifficulty method where I want to pop the current view off the nav controller and go back, I keep getting a 'EXEC_BAD_ACCESS' error. This is the method:

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

I've tried method chaining back to the class that actually owns the navController, as well as using popToViewController:Animated:. If I use NSLog to print out the viewControllers count from the cancelDifficulty method it shows the right number but if I try to interact with it I either get the EXEC_BAD_ACCESS or nothing happens at all.


Why aren't you just accessing the navigationController property of your view controller, rather than casting its parentViewController as a UINavigationController (which is a bad idea, regardless).


I would try the following and see if it works:

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

It could be that parentViewController isn't really a UINavigationController

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜