开发者

How to get a reference to the view at the top of the NavigationController

开发者 https://www.devze.com 2023-04-09 20:01 出处:网络
I am in a some UIViewControlle and need to go back to the previous one, I use this code: NSLog(@\"%开发者_StackOverflow社区@\", [self.navigationController popViewControllerAnimated:YES]);

I am in a some UIViewControlle and need to go back to the previous one, I use this code:

NSLog(@"%开发者_StackOverflow社区@", [self.navigationController popViewControllerAnimated:YES]);
// Here I need to reference to the view I am going go to to call some function before this view is being displayed
NSLog(@"Done button pressed");

But (as written in the comment), I need to get a reference to the view that will be displayed after the pop function executed (the previous viewController.


You can use the UINavigationController property topViewController which gives you the view controller at the top of the stack. Just call this before poping it, or it will be set to the next one :)

You can also access the viewControllers property to get all the viewcontrollers the navigation controller stack contains.


Have a look at the viewControllers property in the online docs. It will give you an array of view controllers in the navigation stack:

The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.

So the view controller you want will be at index n-2.


Besides what @Geoffroy and @Maurice Kelly said..

I should get reference to the object before calling the popViewControllerAnimated: function

  // I should get reference to the object here before calling the popViewControllerAnimated:  function
    NSLog(@"%@", [self.navigationController popViewControllerAnimated:YES]);
    NSLog(@"Done button pressed");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号