UINavigationController 和 UINavigationBarDelegate.ShouldPopItem() 与 MonoTouch [英] UINavigationController and UINavigationBarDelegate.ShouldPopItem() with MonoTouch

查看:12
本文介绍了UINavigationController 和 UINavigationBarDelegate.ShouldPopItem() 与 MonoTouch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在点击 UINavigationBar(由 UINavigationController 控制)的后退按钮时弹出 UIAlertView?在某些情况下,我想问用户你确定吗?"问题类型,以便他可以中止操作并停留在当前视图上,或者弹出导航堆栈并转到父视图.

How do I pop up an UIAlertView when the back button of a UINavigationBar (controlled by a UINavigationController) was tapped? Under certain conditions, I want to ask the user an "Are you sure?" type of question so he could either abort the action and stay on the current view or pop the navigation stack and go to the parent view.

我发现最吸引人的方法是在 UINavigationBar 的 Delegate 上覆盖 ShouldPopItem().

The most appealing approach I found was to override ShouldPopItem() on UINavigationBar's Delegate.

现在,这里有一个非常相似的问题:iphone navigationController :在退出当前视图之前等待 uialertview 响应

Now, there is a quite similar question here: iphone navigationController : wait for uialertview response before to quit the current view

还有一些其他类似的问题,例如这里:检查是否UIViewController 即将从导航堆栈中弹出?如何判断何时回来在 UINavigationControllerStack 中按下按钮

There are also a few other questions of similar nature, for example here: Checking if a UIViewController is about to get Popped from a navigation stack? and How to tell when back button is pressed in a UINavigationControllerStack

所有这些都将子类 UINavigationController"声明为可能的答案.

All of these state "subclass UINavigationController" as possible answers.

然后有一个读起来像子类化 UINavigationController 通常不是一个好主意:Monotouch:UINavigationController,覆盖initWithRootViewController

Then there is this one that reads like subclassing UINavigationController is generally not a good idea: Monotouch: UINavigationController, override initWithRootViewController

苹果文档 还说 UINavigationController 不打算被子类化.

The apple docs also say that UINavigationController is not intended to be subclassed.

其他一些人指出,在使用 UINavigationController 时甚至不可能覆盖 ShouldPopItem(),因为这不允许将自定义/子类 UINavigationBarDelegate 分配给 UINavigationBar.

A few others state that overriding ShouldPopItem() is not even possible when using a UINavigationController as that does not allow to assign a custom/subclassed UINavigationBarDelegate to the UINavigationBar.

我的子类化尝试都没有奏效,我的自定义委托未被接受.

None of my attempts of subclassing worked, my custom Delegate was not accepted.

我还在某处读到,可能可以在我的自定义 UINavigationController 中实现 ShouldPopItem(),因为它将自己分配为其 UINavigationBar 的代表.

I also read somewhere that it might be possible to implement ShouldPopItem() within my custom UINavigationController since it assigns itself as Delegate of its UINavigationBar.

不足为奇,这没有用.UINavigationController 的子类如何知道属于 UINavigationBarDelegate 的方法.它被拒绝:找不到合适的方法来覆盖".删除编译的覆盖"关键字,但该方法被完全忽略(如预期).我认为,使用 Obj-C 可以实现多个协议(类似于 C# AFAIK 中的接口)来实现这一目标.不幸的是,UINavigationBarDelegate 不是接口而是 MonoTouch 中的类,所以这似乎是不可能的.

Not much of a surprise, this didn't work. How would a subclass of UINavigationController know of the Methods belonging to UINavigationBarDelegate. It was rejected: "no suitable method found to override". Removing the "override" keyword compiled, but the method is ignored completely (as expected). I think, with Obj-C one could implement several Protocols (similar to Interfaces in C# AFAIK) to achieve that. Unfortunately, UINavigationBarDelegate is not an Interface but a Class in MonoTouch, so that seems impossible.

我在这里迷路了.当 UINavigationBar 由 UINavigationController 控制时,如何在 UINavigationBar 的 Delegate 上覆盖 ShouldPopItem()?或者有没有其他方法可以弹出 UIAlertView 并在可能弹出导航堆栈之前等待它的结果?

I'm pretty much lost here. How to override ShouldPopItem() on UINavigationBar's Delegate when it is controlled by a UINavigationController? Or is there any other way to pop up an UIAlertView and wait for it's result before possibly popping the navigation stack?

推荐答案

作为参考,我放弃ShouldPopItem()后走的路线是用UIBarButtonItem替换后退按钮 分配了一个自定义 UIButton 作为它的 CustomView.UIButton 被设计成看起来像原始的后退按钮,使用两个用于正常和按下状态的图像.最后,需要隐藏原来的后退按钮.

For reference, the route I took after giving up on ShouldPopItem() is to replace the back button with a UIBarButtonItem that has a custom UIButton assigned as it's CustomView. The UIButton is crafted to look like the original back button using two images for the normal and the pressed state. Finally, hiding the original back button is required.

对于它应该做的事情来说代码太多了.所以是的,谢谢 Apple.

Way too much code for what it's supposed to do. So yeah, thanks Apple.

顺便说一句:另一种可能性是创建一个带有秘密 UIButtonType 101(实际上是后退按钮)的 UIButton 但我避免了这种情况,因为它可能会在以后的 iOS 中崩溃版本.

BTW: Another possibility is creating a UIButton with the secret UIButtonType 101 (which is actually the back button) but I avoided this as it may break at any later iOS version.

这篇关于UINavigationController 和 UINavigationBarDelegate.ShouldPopItem() 与 MonoTouch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆