iPhone SDK 2:UIButton 在从 nib 文件加载的嵌套视图中不起作用 [英] iPhone SDK 2: UIButton not working in nested views loaded from nib file

查看:24
本文介绍了iPhone SDK 2:UIButton 在从 nib 文件加载的嵌套视图中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在基于 Nib 的视图中有一个按钮,它的行为不像按钮(永远不会调用目标方法).我尝试动态添加按钮,但没有帮助.

I have a button in a Nib based view which does not behave like a button (the target method is never called). I have tried to dynamically add the button but it doesnt help.

我将尝试描述应用程序结构(因为我怀疑它可能会导致此问题)并且我可能没有使用正确的设计模式.

I will try to describe the app structure (as I suspect it might be causing this issue) and I may not be using the right design pattern.

  • View A 与控制器 AC 相关联.(从根控制器中的笔尖加载并推送到导航控制器.)

  • View A is tied to Controller AC. (loaded from a nib in the root controller and pushed on to the navigation controller.)

视图 A 嵌入了多个子视图.让我们调用其中一个 A'.

View A has multiple subviews embedded. Lets call one of those A'.

在 AC::viewDidLoad 方法中:

In the AC::viewDidLoad method:

我将 3 个 viewController a,b,c 加载到 AC 的成员变量中.

I load 3 viewControllers a,b,c into member variables of AC.

根据应用设置,我将这些控制器之一的视图添加为 A' 的子视图

本质上在 AC::viewDidLoad 方法中:

Essentially in the AC::viewDidLoad method:

[A' addSubView:(a.view)];

现在 a.view 有一个按钮(带有标签).我搜索那个标签.并动态添加目标方法.此时 a.view 显示为 A' 的子视图,按钮也可见.太好了.

Now a.view has a button (which is tagged).I search for that tag. and Add a target Method to it dynamically. At this point the a.view is displayed as subview of A' and the button is also visible. Great.

但是触摸交互没有发生或者触摸动作被我的方法以外的人使用.

But touch interaction does not happen or the touch action is consumed by someone other than my method.

我尝试了很多变体,但执行从未达到我的方法.我什至尝试将目标添加到所有 touchUpEvents.没有.我检查了启用事件中的控制值,这是正确的.该按钮以某种方式被禁用.我用一个视图编写了一个简单的应用程序并做了同样的事情,它奏效了.所以我认为这与我将多个部分放在一起的方式有关.

I have tried a lot of variations but the execution never reaches my method. I even tried adding the target to all touchUpEvents. nothing. I checked the control values in the enabled events and that is correct. That button is simply disabled somehow. I wrote a simple app with one view and did the same, it worked. So I am thinking it is something to do with the way I am putting multiple pieces together.

有什么想法吗?

我希望我有类似 Spy++ 的东西,在那里我可以看到消息队列并查看谁在使用这些消息而不是我的应用程序,甚至可以查看类对象.我是这个平台的新手.我之前在这个线程上的挣扎. >

I wish I had something like Spy++ where I could see the message Queue and see who was consuming this messages instead of my app or even look at the class object. I am a newbie to this platform. My earlier struggle with this is on this thread.

推荐答案

终于解决了!!!并从中学到了一些东西.我花了几天时间才弄明白.

Resolved it finally!!! and learnt something in return. Did cost me a few days to figure this out.

我的 UIButton 对象不工作的原因是因为我发现在 UIIMageView 对象的情况下:

The reason my UIButton object was not working was because I found that in case of a UIIMageView object:

initWithImage:此方法调整接收器的帧以匹配指定图像的大小.它还默认禁用图像视图的用户交互."

"initWithImage: This method adjusts the frame of the receiver to match the size of the specified image. It also disables user interactions for the image view by default."

AND 我的 UIButton 已被指定为 nib 文件中 UIImageView 控件的子视图 !!!

没有错误/警告.它只是安静地禁用.

There was no errors / warnings. It just gets disabled quietly.

解决方案: 创建了一个容器 UIView 对象,该对象现在包含 UIImageView 和按钮,以便按钮显示为覆盖在图像上,但它实际上是图像的兄弟视图和虚拟的子视图容器 UIView.

Solution: Created a container UIView object which now contains the UIImageView AND the button so that the button appears as overlayed on the Image but it is actually a sibling of the image and a subview of the dummy container UIView.

这篇关于iPhone SDK 2:UIButton 在从 nib 文件加载的嵌套视图中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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