7

我想在 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];
4

4 回答 4

11

为此,我们使用 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;
}
于 2012-04-11T11:27:27.377 回答
3

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

yourTextView.delegate = self;

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

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

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

-(void)textViewDidEndEditing:(UITextView *)textView {}
于 2012-04-11T10:33:23.240 回答
0

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

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

简而言之,我将每个单元格中的每个 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];
    }
于 2012-06-07T01:30:01.170 回答
0

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

TXT_First_Tag.delegate = self;

... 

- (void)textViewDidEndEditing:(UITextView *)textView {
  // stuff you'd put in C6Loop
于 2012-04-11T09:55:24.603 回答