ios UINavigationController 和后退按钮动作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14256051/
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
UINavigationController and back button action
提问by Harish
I have an two controllers
1st is self
and 2nd is maincontroller
, where I'm pushing maincontroller
in stack, so the back button is automatically coming.
我有两个controllers
1st isself
和 2nd is maincontroller
,我正在推maincontroller
入stack,因此后退按钮会自动出现。
Here I need to make an alert when the user presses the back button.
这里我需要在用户按下后退按钮时发出警报。
How can I do this?
我怎样才能做到这一点?
回答by Satheeshwaran
Or you can use the UINavigationController
's delegate methods. The method willShowViewController
is called when the back button of your VC is pressed.
或者您可以使用UINavigationController
的委托方法。willShowViewController
当按下 VC 的后退按钮时调用该方法。
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
回答by Satish Azad
First hide the back button by using
首先使用隐藏后退按钮
self.navigationItem.hidesBackButton = YES;
and then create your own Custom Button:
然后创建您自己的自定义按钮:
UIBarButtonItem *backBtn =[[UIBarButtonItem alloc]initWithTitle:@"back" style:UIBarButtonItemStyleDone target:self action:@selector(popAlertAction:)];
self.navigationItem.leftBarButtonItem=backBtn;
[backBtn release];
and your selector is here:
你的选择器在这里:
- (void)popAlertAction:(UIBarButtonItem*)sender
{
//Do ur stuff for pop up
}
回答by Jawad Zeb
Best and Easiest way
最好和最简单的方法
Try putting this into the view controller where you want to detect the press:
尝试将其放入要检测新闻的视图控制器中:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
}
[super viewWillDisappear:animated];
}
回答by Lorenzo B
Create your own UIBarButtonItem
and set it as the leftBarButtonItem
in viewDidLoad
method of mainController
.
创建您自己的UIBarButtonItem
并将其设置为 的leftBarButtonItem
inviewDidLoad
方法mainController
。
For example (here I used a system item but you can also create a different one, see class reference for details).
例如(这里我使用了一个系统项,但您也可以创建一个不同的项,有关详细信息,请参阅类参考)。
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showAlertView:)];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
// only if you don't use ARC
// [leftBarButtonItem release];
where
在哪里
- (void)showAlertView:(id)sender
{
// alert view here...
}
回答by spider1983
add a custom back button with an action and set your alert in that action method.You can add your custom back button from here: http://www.applausible.com/blog/?p=401
添加带有操作的自定义后退按钮并在该操作方法中设置警报。您可以从此处添加自定义后退按钮:http: //www.applausible.com/blog/?p=401
回答by Vladimir
viewControllerCount- is the var that holds the number of viewControllers previously was in the UINavigationController. Then, we check if viewControllerCount > [viewControllers count]if so, we know that we will get back (i.e. Back button imitation).
viewControllerCount- 是保存以前在 UINavigationController 中的 viewController 数量的变量。然后,我们检查是否viewControllerCount > [viewControllers count]如果是,我们知道我们会回来(即Back button仿制)。
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
NSArray *viewControllers = [navigationController viewControllers];
if (viewControllerCount > [viewControllers count])
{
// your code
}
viewControllerCount = [viewControllers count];
}
回答by Prajwal Udupa
create a button and give the button action as follows.
创建一个按钮并按如下方式提供按钮操作。
[self alert];
and when the alert is displayed, after tapping over yes
当显示警报时,点击是后
[self.navigationController popViewController];
after this,
在这之后,
self.navigationController.LeftBarButton = myButton;
this may help
这可能有帮助