在导航回UINavigationController堆栈中的上一个控制器之前,向用户提示UIAlertView [英] Prompting an UIAlertView to user before navigating back to the previous controller in a UINavigationController stack
问题描述
我试图在导航到上一个控制器之前提示UIAlertView,并且如果用户决定留在同一View Controller上,则阻止导航.使用 CCTBackButtonActionHelper ,可以轻松生成UIALertView,但我面临的唯一问题是它会更改颜色就像单击任何禁用的UIBarButton控件一样,后退按钮的颜色变为灰色.但是,单击导航栏上的任意位置将恢复其原始颜色.那我该如何防止其颜色改变?
I'm trying to prompt a UIAlertView before navigating to the previous controller and preventing navigation if the user decides to stay on the same View Controller. Using CCTBackButtonActionHelper, the UIALertView can be easily generated but the only problem I'm facing is that it changes the color of the back button to gray just like any disabled UIBarButton control when clicked. However, clicking anywhere on the navigation bar restores its original color. So how could i prevent changing its color?
这就是我现在的做法.
在CustomNavigationContoller.m
In CustomNavigationContoller.m
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
BOOL should = [[CCTBackButtonActionHelper sharedInstance] navigationController:self navigationBar:navigationBar shouldPopItem:item];
if (!should) {
return NO;
}
return [super navigationBar:navigationBar shouldPopItem:item];
}
在CustomViewController.m
In CustomViewController.m
#pragma mark - Back button
- (void)cct_navigationBar:(UINavigationBar *)navigationBar willPopItem:(UINavigationItem *)item {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"Are you sure you want to go back?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alertView show];
}
#pragma mark - Alert view delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.cancelButtonIndex == buttonIndex) {
return;
}
[self.navigationController popViewControllerAnimated:YES];
}
推荐答案
我有一个简单的解决方案,对我来说非常理想,而无需使用任何第三方组件.
I have a simple solution, that has worked perfectly for me without using any third-party component.
#pragma mark - Configuration -
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationItem.titleView = self.titleView;
self.navigationController.navigationBarHidden = NO;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"red_cross"] style:UIBarButtonItemStyleBordered
target:self action:@selector(confirmPopViewController:)];
}
将您的viewController设置为与选择器一起使用自己的navigationItem.
Setup your viewController to use your own navigationItem with a selector.
#pragma mark - Events -
- (void)confirmPopViewController:(id)sender
{
[[[UIAlertView alloc] initWithTitle:@"My ViewController" message:@"Do you really want to close ?" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"Yes", nil] show];
}
实施该事件并在那里创建/显示您的UIAlertView.
Implement the event and create/show a your UIAlertView There.
#pragma mark - Delegates -
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex)
[self.navigationController popViewControllerAnimated:YES];
}
最后,使用UIAlertViewDelegate检查用户的选择并做出相应的响应.
Finally, with the UIAlertViewDelegate, check if the choice of your user and respond accordingly.
在这里,当用户确认我们 popViewControllerAnimated
或我们什么都不做时.
Here when the user confirm we popViewControllerAnimated
or we do nothing.
这篇关于在导航回UINavigationController堆栈中的上一个控制器之前,向用户提示UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!