带有MonoTouch的UINavigationController和UINavigationBarDelegate.ShouldPopItem() [英] UINavigationController and UINavigationBarDelegate.ShouldPopItem() with MonoTouch

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

问题描述

当点击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.

现在,这里有一个非常类似的问题:

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(),因为这不允许向UINavigationBar分配自定义/子类化的UINavigationBarDelegate.

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的方法.它被拒绝:找不到适合的方法来替代".删除编译后的"override"关键字,但是该方法被完全忽略(按预期方式).我认为,使用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.

我在这里迷路了.当它由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()后采取的方法是将后退按钮替换为分配了自定义UIButtonUIBarButtonItem因为它是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.

需要执行的代码过多.是的,谢谢苹果.

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.

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

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