【发布时间】:2012-12-24 17:31:08
【问题描述】:
我有两个controllers,第一个是self,第二个是maincontroller,我在stack中推maincontroller,所以返回按钮会自动出现。
这里我需要在用户按下后退按钮时发出警报。
我该怎么做?
【问题讨论】:
标签: ios objective-c uinavigationcontroller
我有两个controllers,第一个是self,第二个是maincontroller,我在stack中推maincontroller,所以返回按钮会自动出现。
这里我需要在用户按下后退按钮时发出警报。
我该怎么做?
【问题讨论】:
标签: ios objective-c uinavigationcontroller
或者您可以使用UINavigationController 的委托方法。 willShowViewController方法在你VC的后退按钮被按下时被调用。
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
【讨论】:
首先使用隐藏后退按钮
self.navigationItem.hidesBackButton = YES;
然后创建自己的自定义按钮:
UIBarButtonItem *backBtn =[[UIBarButtonItem alloc]initWithTitle:@"back" style:UIBarButtonItemStyleDone target:self action:@selector(popAlertAction:)];
self.navigationItem.leftBarButtonItem=backBtn;
[backBtn release];
你的选择器在这里:
- (void)popAlertAction:(UIBarButtonItem*)sender
{
//Do ur stuff for pop up
}
【讨论】:
b 怎么样?请修复它。
hidesBackButton = YES,因为这会在手动推送新的UINavigationItem 时导致一些视觉伪影。
最好最简单的方法
尝试将其放入要检测压力的视图控制器中:
-(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];
}
【讨论】:
创建自己的UIBarButtonItem,并将其设置为mainController的viewDidLoad方法中的leftBarButtonItem。
例如(这里我使用了一个系统项,但您也可以创建一个不同的,请参阅类参考了解详细信息)。
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showAlertView:)];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
// only if you don't use ARC
// [leftBarButtonItem release];
在哪里
- (void)showAlertView:(id)sender
{
// alert view here...
}
【讨论】:
添加带有操作的自定义后退按钮并在该操作方法中设置警报。您可以从此处添加自定义后退按钮:http://www.applausible.com/blog/?p=401
【讨论】:
viewControllerCount - 是保存先前在 UINavigationController 中的 viewController 数量的 var。然后,我们检查 viewControllerCount > [viewControllers count] 如果是,我们知道我们会返回(即后退按钮模仿)。
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
NSArray *viewControllers = [navigationController viewControllers];
if (viewControllerCount > [viewControllers count])
{
// your code
}
viewControllerCount = [viewControllers count];
}
【讨论】:
创建一个按钮并给按钮操作如下。
[self alert];
当显示警报时,点击是后
[self.navigationController popViewController];
之后,
self.navigationController.LeftBarButton = myButton;
这可能会有所帮助
【讨论】: