how to create back button in navigation bar

48,274

Solution 1

As you said in your comment you use a modal controller

Add the following in viewWillappear

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

And in

- (IBAction)Back
  {
    [self dismissViewControllerAnimated:YES completion:nil]; // ios 6
  }

Solution 2

Swift 3

let backButton: UIBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(back))
    self.navigationItem.leftBarButtonItem = backButton

func back() {
    self.dismiss(animated: true, completion: nil) }

Solution 3

I had similar issue, but I am using Swift. Here is the answer in Swift 2.2.

        override func viewWillAppear(animated: Bool) {
            let backButton: UIBarButtonItem = UIBarButtonItem(title: "Back", style: .Plain, target: self, action: #selector(back))
            self.navigationItem.leftBarButtonItem = backButton;
            super.viewWillAppear(animated);
        }

        func back() {
            self.dismissViewControllerAnimated(true, completion: nil)
        }
Share:
48,274
janatan
Author by

janatan

Updated on July 09, 2022

Comments

  • janatan
    janatan almost 2 years

    I have 2 page. first is tableView and second is view when I to click on any cell go on to next page (view) in way modal segue. I want add back button in next page of navigation bar . this is my code in view page : ViewController.m

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.lable.text = obji.Name;
        self.lable2.text = obji.Descript;
    
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(Back)];
        self.navigationItem.leftBarButtonItem = backButton;
    }
    
    - (IBAction)Back
    {
        //I dont know that how return pervious page
    }