2012-04-11 25 views
6

我想在UITextView上使用addTarget:action(UITextField或UIButton)。我們可以在UITextView上採用哪些addTarget操作?

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

我知道這個問題是舊的引用單元格中的代表,但我想我會指出,'addTarget的事實:動作:forControlEvents:'方法在'繼承UITontrol「中的」UITextField「和」UIButton「。 'UITextView'不從'UIControl'繼承。所以標題中問題的答案是沒有。 – 2013-08-14 14:35:25

回答

10

爲此我們使用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; 
} 
0

肯定得到一個更好的辦法「的編輯沒有結束」的消息是通過實現UITextViewDelegate

TXT_First_Tag.delegate = self; 

... 

- (void)textViewDidEndEditing:(UITextView *)textView { 
    // stuff you'd put in C6Loop 
2

如果u想要做一些任務編輯文本視圖比之前使用

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

,或者如果你想編輯的文本視圖後做多使用

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

爲您必須添加協議名爲UITextViewDelegateviewdidload編寫

yourtextview.delegate = self; 
0

CHAPS,即使 您迴應並沒有直接解決我的問題,它幫助我縮小它。

我在每個單元格中都有textviews,並且需要傳遞正在編輯的文本視圖,所以我可以更新數據庫。

簡而言之,我將每個單元格中的每個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]; 
    } 
相關問題