开发者

UIButton in a subview, trigger action in current ViewController

开发者 https://www.devze.com 2023-01-28 12:36 出处:网络
I have a problem. I\'ve a normal viewcontroller where I add UITextViews in a scrollview. To these UITextviews I add a dynamic number of UIButtons, to which I want to add a target. The reason I add the

I have a problem. I've a normal viewcontroller where I add UITextViews in a scrollview. To these UITextviews I add a dynamic number of UIButtons, to which I want to add a target. The reason I add them to the UITextViews is that adding them to the viewcontroller adding the text view's origin will make them end up outside the screen and not scroll, of course. But when I do that, the buttons trigger the action.

My question is: how do I specify the viewcontroller as target? Using self or using the var created in the appdelegate as target does not trigger it. If "two superlevels up from the textview" will work I will use that, just don't know how to specify it correctly.

My code:

   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)开发者_如何学Python;
   [tvMain addSubview:btn];


Just remove colon from your code

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

The corrected one is [btn addTarget:self action:@selector(phoneemail) forControlEvents:UIControlEventTouchUpInside];

If I guess, this will probably help you.


Solved it back then by adding the buttons to the scrollview instead of the textview.

0

精彩评论

暂无评论...
验证码 换一张
取 消