iPhone SDK 2:以编程方式添加一个信息按钮 [英] iPhone SDK 2: Programmatically adding an Info Button
问题描述
我想一个信息按钮添加到我的应用程序提供自定义的帮助。
I am trying to add an info button to my app to provide custom help.
而不是添加的按钮,笔尖和连接的情况下(touchUpInside)到控制器,我决定以编程方式添加按钮。按钮显示出来。当我添加目标事件处理程序,当按钮被感动被执行,这是行不通的。那我的方法(doHelp)不被要求触摸按钮。
Instead of adding the button to the nib and linking the event (touchUpInside) to the controller, I decided to add the button programmatically. The button shows up. When I add the target event handler to be executed when the button is touched, it does not work. That my method(doHelp) is not being called on touching the button.
当我调试它,该事件是不与按钮登记!虽然code不抛出任何异常。
When I debugged it, the event is not registered with the button! Although the code does not throw any exceptions.
下面是code段从视图:
Here is the code snippet FROM the view:
// Create a Button to get Help
UIButton *helpButton = [UIButton buttonWithType:UIButtonTypeInfoDark ] ;
buttonRect = helpButton.frame;
// CALCulate the bottom right corner
buttonRect.origin.x = rect.size.width - buttonRect.size.width - 8;
buttonRect.origin.y = rect.size.height - buttonRect.size.height - 8;
[helpButton setFrame:buttonRect];
[helpButton addTarget:self action:@selector(doHelp:) forControlEvents:UIControlEventTouchUpInside];
[helpButton setEnabled:TRUE];
[self addSubview:helpButton];
........
........
// Another METHOD ELSEWHERE in the VIEW object
-(void)doHelp:(id)Sender
{
[self setHelpNeeded:TRUE];
[self setNeedsDisplay];
}
我在做什么错吗?
我已经看过SDK帮助和样本,真的很狼狈不堪!
我希望另一双眼睛将帮助! : - )
What am I doing wrong please? I have looked at the SDK help and samples and am really flummoxed! Am hoping another pair of eyes will help! :-)
这code段是在情况下,你需要知道的视图对象。
我刚添加的doHelp帮助第一反应2 ...感谢。
This code snippet is in the View Object in case you need to know. I just added the doHelp to help the first 2 responders... thanks.
**更新09年6月4日** -
我一直在努力了一夜并没有什么工作。我觉得有什么错我已经设置了方法选择我的方法不会被调用的方式。其他的一切看起来不错。即使使用NIB文件不起作用。我已标记的按钮,检索它并添加方法选择器,但无济于事。也有一些是基本的,我做错了......哎呀!
的任何想法,任何人吗?的
**UPDATE 6/4/09 ** - I have been trying all night and nothing worked. I think there is something wrong in the way I have set up the method selector as my method never gets called. Everything else looks fine. Even using a NIB file does not work. I have tagged the button, retrieved it and added the method selector but to no avail. There is something fundamental which I am doing wrong... Argh!!! Any ideas, anyone?
推荐答案
终于解决了它!并学会一些回报。难道我花了几天摸不着头脑。
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."
和我的UIButton已经被指定为一个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:以编程方式添加一个信息按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!