UINavigationController 自定义 popViewControllerAnimated 方法
我制作了一个自定义 UINavigationController 类,以便我可以弹出 UIAlertView 并说,您确定要在点击后退按钮时离开此视图吗?
我有一个客户采用如下方法:
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
if([[self.viewControllers lastObject] class] == [weddingSetupController class]){
UIAlertView *exitAlert = [[UIAlertView alloc]
initWithTitle:@"Are you sure?"
message:@"By leaving the setup, all current changes will be lost. The setup can be retsrated later if you decide to leave now. However, it is recomened for your best experience that you complete the setup."
delegate:self cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil
];
[exitAlert show];
return nil;
}
else {
return [super popViewControllerAnimated:animated];
}
}
它效果很好,尽管有一个问题是,如果我说“否”,它会保留在同一视图上,不会弹出父视图,但导航栏会弹出。所以我让视图不会弹出,但栏始终返回到其父视图的状态。
I have made a custom UINavigationController class so that I can have a UIAlertView popup and say, are you sure you want to leave this view when tapping the back button.
I have a customer the method like so below:
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
if([[self.viewControllers lastObject] class] == [weddingSetupController class]){
UIAlertView *exitAlert = [[UIAlertView alloc]
initWithTitle:@"Are you sure?"
message:@"By leaving the setup, all current changes will be lost. The setup can be retsrated later if you decide to leave now. However, it is recomened for your best experience that you complete the setup."
delegate:self cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil
];
[exitAlert show];
return nil;
}
else {
return [super popViewControllerAnimated:animated];
}
}
It works well, although an issue is, that while if I say 'No', it stays on the same view, not popping the parent view, the navigation bar does pop. So I get the view not to pop but the bar always returns to the state of its parent view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您按下导航栏上的后退按钮时,它会
在 UINavigationBar 中调用:,而 UINavigationBar 又会调用导航控制器上的 popViewControllerAnimated。重写此方法(我使用类别,以避免子类化)并采用相同的方法。你最重要的观点只是晚了一步。
When you press the back button on the navigation bar, it's calling:
in UINavigationBar, which in turn calls popViewControllerAnimated on the navigation controller. Override this method (I use a category, to avoid subclassing) and take your same approach. Your overriding point was just a step late.
我认为理想情况下,用户永远不必“保存”任何东西 - 一切都被隐式保存。因此,如果他们离开设置并稍后返回,他们应该能够从中断的地方恢复。
但如果您仍然想这样做,那么使用顶部带有“取消”和“完成”按钮的模式视图可能会更好。现在我想起来了,邮件会要求您使用操作表保存未发送的草稿。
I think that ideally, the user never has to "save" anything - everything is saved implicitly. So if they leave setup and come back later, they should be able to resume where they left off.
But if you still want to do it this way, it might work better to use a modal view with Cancel and Done buttons at the top. And now that I think of it, Mail will ask you to save a unsent draft using an action sheet.