【问题标题】:UIButton in a subview, trigger action in current ViewController子视图中的 UIButton,在当前 ViewController 中触发动作
【发布时间】:2010-12-03 10:27:05
【问题描述】:

我有问题。我有一个普通的视图控制器,我在滚动视图中添加 UITextViews。我向这些 UITextviews 添加了动态数量的 UIButtons,我想在其中添加一个目标。我将它们添加到 UITextViews 的原因是,将它们添加到 viewcontroller 添加文本视图的原点会使它们最终出现在屏幕之外而不是滚动,当然。但是当我这样做时,按钮会触发动作。

我的问题是:如何将视图控制器指定为目标?使用 self 或使用在 appdelegate 中创建的 var 作为目标不会触发它。如果“从 textview 向上的两个超级别”可以工作,我会使用它,只是不知道如何正确指定它。

我的代码:

   UIImage *img=[UIImage imageNamed:@"phonebutton40x30.png"];
   UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
   [btn addTarget:self action:@selector(phoneemail:) forControlEvents:UIControlEventTouchUpInside];
   [btn setImage:img forState:UIControlStateNormal];
   btn.tag=700+i;
   btn.frame=CGRectMake(xoffs+3, yoffs+19, 50, 38);
   [tvMain addSubview:btn];

【问题讨论】:

  • 你能澄清你在问什么吗?当您将按钮添加到 [self.view addSubview:btn] 时,它是否按预期工作,但当您将其添加到 tvMain 时却没有?也就是说,如果你只是将[tvMain addSubview:btn]这一行改为[self.view addSubview:btn],它是否有效?

标签: iphone uibutton target self


【解决方案1】:

只需从代码中删除冒号

[btn addTarget:self action:@selector(phoneemail:) forControlEvents:UIControlEventTouchUpInside];

修正的是 [btn addTarget:self action:@selector(phoneemail) forControlEvents:UIControlEventTouchUpInside];

如果我猜的话,这可能会对你有所帮助。

【讨论】:

  • 不,冒号用于方法,用于功能。 (方法可能不需要冒号。)正如我所说,当我将其设置为导航到视图控制器的子视图时,它使用完全相同的代码,而不是 UITextView。
【解决方案2】:

然后通过将按钮添加到滚动视图而不是文本视图来解决它。

【讨论】:

    猜你喜欢
    • 2013-12-24
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 2011-06-16
    • 2021-12-18
    • 1970-01-01
    相关资源
    最近更新 更多