程序按钮和故事板按钮

时间:2014-02-20 15:27:04

标签: ios objective-c uibutton programmatically-created

我在导航栏中有一个按钮和一个搜索字段,我为所有视图添加了一个类的代码。

问题是:使用按钮查看另一个按钮,添加了故事板的搜索字段不再可触摸且无法正常工作。

UIImage * buttonImage = [UIImage imageNamed:@"chat-notify.png"];

button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
           action:@selector(goChat)
 forControlEvents:UIControlEventTouchDown];
//[button setTitle:@"1" forState:UIControlStateNormal];
button.frame = CGRectMake(280.0, 25.0, 30.0, 30.0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];

[self.view addSubview:button];

然后在控制器中:

 notificationViewController* notification = [[notificationViewController alloc]init];
//[notification methodA];

[notification makeButtonNotification];

[self.view addSubview:notification.view];

有谁知道我的错误在哪里?

由于

1 个答案:

答案 0 :(得分:0)

为什么要添加按钮确保它在那里并更改它的隐藏状态会更容易,然后你可以将它全部保存在故事板中

UIControlEventTouchUpInside

是您应该使用的不是“UIControlEventTouchDown”

TouchUpInside通常是您从按钮触发操作的事件。用户在按下按钮后从按钮上抬起。您不希望触发Touch Down上的操作,因为使用可能会将手指从按钮上移开以取消按下,这是iOS上的标准和预期行为

更新:根据您对此答案的评论,您可能需要将按钮添加到视图中的特定点继承人。在您的故事板中,将添加视图,因为它们在从上到下的那个stroyboard中列出,底部的视图是顶视图。

尝试显然确保您想要接收的视图不是直接位于每个视图之上,并且它们不重叠。还可以尝试在特定视图的上方或下方添加以编程方式生成的按钮,而不是仅仅将其作为主视图中的顶级子视图,而不是您想要的声音。

当你添加这样的按钮时

[self.view addSubview:button];

这样做可能会更好

[self.view insertSubview:button aboveSubview:someOtherView]

要做到这一点,您可能需要将“someOtherView”连接到IBOutlet,以便您可以在代码中引用它。因为看起来你最有可能将按钮放在其他所有东西上,当你需要更精确时。