How to create a back button in a view controller to go to parent view controller

戏子无情 提交于 2019-11-28 06:52:53

问题


hi I have a view controller with a container and in the container a child view with a collection view when the user taps the collection view cell it sends me to detail view controller but now what i want to do is to add a back button in my detail view controller which sends me to the parentViewController


回答1:


Case 1 : Unwind Segue

This will work perfect according to your situation:

iOS Unwind Segue

Unwind Segues give you a way to “unwind” the navigation stack and specify a destination to go back to.

Case 2 : PopToRootViewController

If you Parent view is also your Root view controller, then you can easily get back using popToRootViewControllerAnimated:YES.

Create own back button add it to navigation bar with method backButtonTouch.

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil) style:UIBarButtonItemStyleDone target:self action:@selector(backButtonTouch:)]; 

Add above code into viewDidLoad.

-(void)backButtonTouch:(id)sender{
    [self.navigationController popToRootViewControllerAnimated:YES];
}



回答2:


  1. If you want to customise the back button, first hide the navigation bar

    [self.navigationController setNavigationBarHidden:YES];

  2. Now add a button, and create its touchUpInside event, in that event pop the controller

    [self.navigationController popViewControllerAnimated:YES];




回答3:


Here is my code to go back to parent view controller

 - (IBAction)ActionbtnBack:(id)sender {
        int flag = 0;
        for (UIViewController *controller in [[self.navigationController.viewControllers reverseObjectEnumerator] allObjects]) {
            NSLog(@"> %@",[controller class]);
            if ([controller isKindOfClass:[YourParentviewcontroller class]]) {
                flag=1;
                [self.navigationController popToViewController:controller
                                                      animated:YES];
                break;
            }
            else if ([controller isKindOfClass:[YourParentviewcontroller class]]) {
                flag=1;
                [self.navigationController popToViewController:controller
                                                      animated:YES];
                break;
            }
        }
        if (flag == 0) {
            YourParentviewcontroller *MoreVc = [[YourParentviewcontroller alloc] initWithNibName:@"parentViewcontrollerIdentifier" bundle:nil];
            [self.navigationController pushViewController:MoreVc animated:YES];
        }
    }




回答4:


On Back button action, add this line:

[self.navigationController popToRootViewControllerAnimated:YES];

This will move you to rootViewController.



来源:https://stackoverflow.com/questions/34714845/how-to-create-a-back-button-in-a-view-controller-to-go-to-parent-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!