Search code examples
iphoneuibuttontargetself

UIButton in a subview, trigger action in current ViewController


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);
   [tvMain addSubview:btn];

Solution

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