【问题标题】:Which addTarget actions can we take on UITextView?我们可以对 UITextView 执行哪些 addTarget 操作?
【发布时间】:2018-05-02 01:00:24
【问题描述】:

我想在 UITextView 上使用 addTarget:action,就像在(UITextField 或 UIButton)上一样。

我想在 UITextView 上调用一个方法。

请给出一些可能的解决方案...

谢谢...

UITextView  *TXT_First_Tag = [[UITextView alloc] initWithFrame:CGRectMake(5, textPosY, 350, 65)];
TXT_First_Tag.backgroundColor = [UIColor whiteColor];
TXT_First_Tag.font = [UIFont fontWithName:@"Arial-BoldMT" size:30.0];
TXT_First_Tag.editable =YES;
TXT_First_Tag.tag = i; 
TXT_First_Tag.textColor = [UIColor blackColor];


[TXT_First_Tag addTarget:self action:@selector(C6Loop) forControlEvents:UIControlEventEditingDidEnd]; //  This Line I want to use, it's working fine on textfield...
[scrollview addSubview:TXT_First_Tag];

【问题讨论】:

  • 我知道这个问题已经过时了,但我想我会指出addTarget:action:forControlEvents: 方法是从UITextFieldUIButton 继承自UIControl 的事实。 UITextView 不继承自 UIControl。所以标题中问题的答案是否定的。

标签: iphone objective-c ios uitextview


【解决方案1】:

为此,我们使用 UITextView 委托方法。

把它放在你的代码中。

UITextView  *TXT_First_Tag = [[UITextView alloc] initWithFrame:CGRectMake(5, textPosY, 350, 65)];
TXT_First_Tag.backgroundColor = [UIColor whiteColor];
TXT_First_Tag.font = [UIFont fontWithName:@"Arial-BoldMT" size:30.0];
TXT_First_Tag.editable =YES;
TXT_First_Tag.delegate  =   self;
TXT_First_Tag.tag = i; 
TXT_First_Tag.textColor = [UIColor blackColor];
[scrollview addSubview:TXT_First_Tag];

- (void)textViewDidBeginEditing:(UITextView *)textView{
    NSLog(@"Begin editing");
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    NSLog(@"DidEndEditing");
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    NSLog(@"ShouldBeginEditing");
    return TRUE;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    NSLog(@"ShouldEndEditing");
    return TRUE;
}

【讨论】:

    【解决方案2】:

    如果您的控制器实现了UITextViewDelegate 协议,那么您可以在控制器的实例方法viewDidLoad() 中将控制器设置为文本视图的委托:

    yourTextView.delegate = self;
    

    然后,如果您想在编辑文本视图之前执行一项任务,请使用:

    -(void)textViewDidBeginEditing:(UITextView *)textView {}
    

    而且,如果您想在编辑 文本视图 后执行任务,请使用:

    -(void)textViewDidEndEditing:(UITextView *)textView {}
    

    【讨论】:

      【解决方案3】:

      当然,获得“编辑结束”消息的更好方法是实现UITextViewDelegate?

      TXT_First_Tag.delegate = self;
      
      ... 
      
      - (void)textViewDidEndEditing:(UITextView *)textView {
        // stuff you'd put in C6Loop
      

      【讨论】:

        【解决方案4】:

        章节, 尽管您的回答没有直接解决我的问题,但它帮助我缩小了范围。

        我在每个单元格中都有文本视图,并且需要传递正在编辑的文本视图,以便我可以更新数据库。

        简而言之,我将每个单元格中的每个 textview 标签设置为 indexpath.row。然后我通过 textview.tag 在委托中引用该单元格

            - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
            {  
        
        
            static NSString *MyIdentifier = @"MyIdentifier";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
            if (cell == nil)
            {
            [[NSBundle mainBundle] loadNibNamed:@"frontCell" owner:self options:nil];
            cell = mainPageCell;
            self.mainPageCell = nil;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }    
        
        
            UITextView *trackDetails;
            trackDetails = (UITextView *)[cell viewWithTag:22];
            trackDetails.text = [[myArray objectAtIndex:indexPath.row] objectAtIndex:0];
            trackDetails.delegate = self;
            trackDetails.tag = indexPath.row;
        
        
            }
        
            - (void)textViewDidEndEditing:(UITextView *)textView
            {
            NSLog(@"%d",textView.tag);
            UPDATE DATABASE WITH CHANGED TEXT
            [textView resignFirstResponder];
            [self.tableView reloadData];
            }
        

        【讨论】:

          猜你喜欢
          • 2011-01-16
          • 1970-01-01
          • 2020-12-21
          • 1970-01-01
          • 2015-01-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多