开发者

Action for dynamically created UIButton inside UITextView

开发者 https://www.devze.com 2023-01-01 19:09 出处:网络
I have a scroll view; inside I add one or more text views; to each text view I add a button: UIScrollView

I have a scroll view; inside I add one or more text views; to each text view I add a button:

UIScrollView
   UITexView
      UIButton
   UITextView
      UIButton
   ...

This is part of the code:


- (void)viewDidLoad {
   ...
   [self loadUI];
   ...
}

-(void) loadUI {
   UITextView *textView;
   ...
   for (...) {
      UIButton *editButton = [[UIButton alloc] initWithFrame:
CGRectMake(self.scrollView.frame.size.width - 230, 0, 50, 20)];
      ...
      [editButton addTarget:self action:@selector(editPressed:) 
forControlEvents:UIControlEventTouchUpInside];
      ...
      textView = [[CustomTextView alloc] initWithFrame:
CGRectMake(10, dynamicHeight, self.queuedView.frame.size.width - 100, 500)];
      textView.userInteractionEnabled = NO;
      ...
      [textView addSubview:editButton];
      [editButton release];
      ...
      [self.scrollView addSubview:textView];
      [textVie开发者_运维问答w release];
   }
}

- (IBAction) editPressed:(id)sender {
   ...
}

For some reason, the editPressed method is not called when I press the button. Any ideas why? I tried setting the interaction enabled for the text view to YES, but still nothing. I changed form (IBAction) to (void), still nothing.

Thank you, Mihai


Apparently, textView.userInteractionEnabled = YES; did the trick. I tried this before but it did not work. Maybe I had to do a clean build after the change.


Try adding the button to the rightView of the textfield

dotextField.rightView = editButton instead of [textView addSubview:editButton];

0

精彩评论

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