0

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

问题是:使用按钮视图,另一个按钮和添加故事板的搜索字段不是更可触摸且不起作用。

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];

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

谢谢

4

1 回答 1

0

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

UIControlEventTouchUpInside

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

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

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

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

当您添加这样的按钮时

[self.view addSubview:button];

这样做可能会更好

[self.view insertSubview:button aboveSubview:someOtherView]

为此,您可能需要将“someOtherView”连接到 IBOutlet,以便您可以在代码中引用它。当您需要更精确时,看起来您很可能会将按钮放在其他所有内容上。

于 2014-02-20T15:36:23.663 回答