xcode 如何在presentViewController中使用后退按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20792547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use back button with presentViewController?
提问by user3085646
Here is my code for calling a new view controller that is wrapped in a UINavigationController. I want a simple back button on the restaurantResults controller. My selector does not seem to work. I've tried using pop commands. Will those work since I am using presentViewController and not any sort of push?
这是我调用包装在 UINavigationController 中的新视图控制器的代码。我想在 restaurantResults 控制器上有一个简单的后退按钮。我的选择器似乎不起作用。我试过使用 pop 命令。因为我使用的是 presentViewController 而不是任何类型的推送,这些会起作用吗?
Pretty sure my selector is wrong right now because it says self.navigationController, which can't possibly be right.
很确定我的选择器现在是错误的,因为它说 self.navigationController,这不可能是正确的。
Here is where I call the new view controllers and set up the back button:
这是我调用新视图控制器并设置后退按钮的地方:
- (void)searchBarSearchButtonClicked:(UISearchBar *)foodNearSearchBar
{
restaurantsViewController *restaurantResults = [[restaurantsViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:restaurantResults];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:@selector(backPressed:)];
restaurantResults.navigationItem.leftBarButtonItem = backButton;
[self presentViewController:navController animated:YES completion:Nil];
}
Here is my selector:
这是我的选择器:
-(void)backPressed: (id)sender
{
[self.navigationController popViewControllerAnimated: YES]; // or popToRoot... if required.
}
I also tried:
我也试过:
- (void)backPressed:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
};
回答by FreeXu
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:nil
ction:@selector(backPressed:)];
restaurantResults.navigationItem.leftBarButtonItem = backButton;
these code should be used on the restaurantsViewController; the target is self.
这些代码应该用在 restaurantViewController 上;目标是自己。
回答by user1664018
//This works - Just did it //You can create back button in the view you want to see back button -
//这有效 - 刚刚做到了 //您可以在要查看后退按钮的视图中创建后退按钮 -
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Login"
style:UIBarButtonItemStylePlain
target:self
action:@selector(backPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
[self.navigationItem setHidesBackButton:NO];
// And just do dismiss the view controller in the viewcontroller where the back button is - as below-
// 然后在后退按钮所在的视图控制器中关闭视图控制器 - 如下所示 -
- (void)backPressed:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
};
回答by Gil Sand
If you are using a UINavigationController
and you want the default iOS back button, you do not need to set it programatically. There is literally nothing to add, it's built in by default.
如果您使用的是 aUINavigationController
并且想要默认的 iOS 后退按钮,则不需要以编程方式设置它。几乎没有什么可添加的,它是默认内置的。
By pushingthe UIViewController into the navigation controller, it'll be in the navigation controller stack of controllers, and iOS will therefore add a navigation bar, and a back button when you dig into the navigation stack.
通过将UIViewController推入导航控制器,它将位于控制器的导航控制器堆栈中,因此当您深入导航堆栈时,iOS 将添加一个导航栏和一个后退按钮。
If its not working, check the following :
如果它不起作用,请检查以下内容:
That the controller you're pushing from is the root of the UINavigationController. You can set this by code or in storyboard. (you're OK)
That you're pushing from the navigation controller and not the viewcontroller. Essentially you have to do
navigationcontroller.push()
and notself.push()
, otherwise it just won't work. (depends whatself
is here, but I'm pretty sure your mistake is here)
您正在推送的控制器是 UINavigationController 的根。您可以通过代码或故事板进行设置。(你没事)
你是从导航控制器而不是视图控制器推送的。基本上你必须做
navigationcontroller.push()
而不是self.push()
,否则它就是行不通的。(取决于self
这里是什么,但我很确定你的错误在这里)
I see that you're using presentViewController
which is for modals, its fine if that is your intent, but if you want a navigation stack, why not embed self
in a navigation controller in the first place, hide its navigation bar, and then simply push your next controller onto it.
我看到您正在使用presentViewController
which 用于模态,如果这是您的意图,那很好,但是如果您想要一个导航堆栈,为什么不首先嵌入self
导航控制器,隐藏其导航栏,然后只需按下您的下一个控制器就可以了。
That way you dont have to manually create that back button, and let iOS deal with everything.
这样你就不必手动创建后退按钮,让 iOS 处理所有事情。
If you mustdo it that way, you can only "dismiss" when you "present", and "pop" when you "push". But I don't have enough information to know why yours does not work. Try a few things and give us more feedback. But from what I see you're going for a more complicated solution than necessary.
如果一定要那样做,就只能在“呈现”时“解除”,“推”时“弹出”。但我没有足够的信息来知道为什么你的不起作用。尝试一些事情并给我们更多反馈。但是从我看来,您正在寻求一个比必要更复杂的解决方案。
Also I would really start with a simple button that says "close" and see if it works that way before trying to embed it in a bar with an item. That way you tackle one problem, and one new concept at a time
此外,我真的会从一个简单的按钮开始,上面写着“关闭”,然后在尝试将它嵌入到带有项目的栏中之前看看它是否能这样工作。这样你一次解决一个问题,一个新概念