Adding a UIBarButtonItem programmatically to UINavigationBar

前端 未结 4 849
执念已碎
执念已碎 2021-01-07 19:21

I dropped in a UINavigationBar in UIInterfaceBuilder. I present this view modally and just want a UIBackBarButton to return to my last

4条回答
  •  别那么骄傲
    2021-01-07 19:55

    making a call such as this from a view controller

    {
        NextViewController* vcRootView = [[NextViewController alloc] initWithNibName:@"NextView" bundle:[NSBundle mainBundle]];
        UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vcRootView];
        [vcRootView release];
    
        [self.navigationController presentModalViewController:navController animated:YES];
        [navController release];    
    
    }
    

    will present NextViewController as a Modal view on the calling view and NextViewController will have a navigationController for it.

    In The NextViewController implementation file all you need is this

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self 
                                                                      action:@selector(barButtonBackPressed:)];
        self.navigationItem.leftBarButtonItem = backButton;
        [backButton release];
    }
    
    
    -(void)barButtonBackPressed:(id)sender{
        [self dismissModalViewControllerAnimated:YES];
    }
    

    to have the back button to dismiss the modalview. Hope it helps.

提交回复
热议问题