Make back button go to the previous view programmatically

前端 未结 3 1314
-上瘾入骨i
-上瘾入骨i 2021-02-20 13:20

I have a UIBarButtonItem and would like to programmatically set the action that goes to the previous controller (in my case, my previous view is a UITableViewController).

<
3条回答
  •  攒了一身酷
    2021-02-20 13:52

    You could write this in your viewDidLoad method:

    UIBarButtonItem *btn=[[UIBarButtonItem alloc] initWithTitle:@"Back"     style:UIBarButtonItemStyleBordered target:self action:@selector(btnClick)];
    self.navigationItem.leftBarButtonItem=btn;
    

    and then use this:

    // call of method
    -(void)btnClick
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    

提交回复
热议问题