How to show back button on the RootViewController of the UINavigationController?

前端 未结 4 661
独厮守ぢ
独厮守ぢ 2021-01-08 01:29

Here is my code. I want to put a back button on the opening rootviewController.

- (void)selectSurah:(id)sender {

    SurahTableViewController * surahTableVi         


        
4条回答
  •  一生所求
    2021-01-08 01:50

    Faizan,

    Helium3 comment makes sense.

    I suppose that your button is needed to dismiss the controller presented modally, is it true? Correct if I'm wrong.

    If so, you could just create a new UIBarButtonItem and set is a left (or right) button for the UINavigationController navigationItem. To not break encapsulation create it in the viewDidLoad method for your SurahTableViewController controller.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // make attention to memory leak if you don't use ARC!!!
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close"
               style:UIBarButtonItemStyleBordered
                 target:self
                 action:@selector(close:)];
    }
    
    -(void)close:(id)sender
    {
        // to dismiss use dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
        // dismissModalViewControllerAnimated: is deprecated
    
        [self dismissViewControllerAnimated:YES completion:^{ NSLog(@"controller dismissed"); }];
    }
    

提交回复
热议问题