简体   繁体   中英

Back button without using navigation controller in IOS

I haven't embedd navigation controller with my view controller. Now i'm using a code to go back to home screen to with the use of back button but there nothing appears, i don't know why. I don't want to use navigation controller in my vc. This is the code,

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"car_marker.png"]
                                                               style:UIBarButtonItemStylePlain
                                                              target:self.navigationController
                                                              action:@selector(popViewControllerAnimated:)];
self.navigationItem.leftBarButtonItem = backButton;

It should look like this only, 在此处输入图片说明

You will need to change your approach because you cannot pop without a navigation controller .

You should embed your viewcontroller into navigation controller and then you can hide with [[self navigationController] setNavigationBarHidden:YES animated:YES]; and then you can add any kind of custom button to perform pop action.

For adding a custom back button to your view

- (void)addCustomBackButtonToView
{
    UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(20, 20, 44.0f, 30.0f)];
    [backButton setImage:[UIImage imageNamed:@"back.png"]  forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
}

- (void) popVC{
  [self.navigationController popViewControllerAnimated:YES];
}

You can hide your navigation controller by below code self.navigationController.navigationBar.isHidden = true

and use swipegesture option to move back, even though you have to use navigation controller,@ iPeter has a valid point.

1) Add two ViewControllers make first as initial controller(entry point).

2) Add view on both ViewController with size you wish to have.

3) Add buttons on both views as subviews.

4) create segue from firstButton ->(use show) -> second controller.

5) From second ->(use show) -> first controller.

see the image below-:

在此处输入图片说明

This is not preferred way of doing pop/push. You must use Navigation Controller(that works like a stack for controllers you push) that will provide you default Back button or you can create custom .

在此处输入图片说明

You can not push or pop view controllers as you are not using the Navigation controller. Try using Present & dismiss controller which can called from any controller Instace.

Step 1 : Present to " Controller which is shown with no navigation bar but back button"

[self presentViewController:@"<ViewControllerToBePresented>" animated:NO completion:nil];

Step 2: add backBatton in ViewDidLoad

-(void)addBack{

UIButton *backBtn = [[UIButton alloc] initWithFrame: CGRectMake(20.0f, 20.0f, 50.0f, 30.0f)];
[backBtn setTitle:@"back" forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];

}

Step 3: Dismisscontroller to go back to previous scene

-(void) dismissViewController{

[self dismissViewControllerAnimated:NO completion:nil];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM