2012-07-14 38 views
1

我有兩個UITextView在我的iPhone應用程序中。當用戶選擇一個UITextView時,控制器自動轉到另一個UITextView。我在我的項目中嘗試了以下方法。但第二個UITextViewbecomefirstresponder,直到用戶點擊第二個UITextView內。如何成爲從一個UITextView到另一個UITextView的響應者?

-(void) viewDidLoad 
{ 
    TextView1 = [[UITextView alloc] initWithFrame:CGRectMake(10, 320, 300, 50)]; 
    TextView1.delegate = self; 
    TextView1.backgroundColor = [UIColor clearColor]; 
    TextView1.layer.borderWidth = 2; 
    TextView1.layer.borderColor = [UIColor blackColor].CGColor; 
    TextView1.layer.cornerRadius = 5; 
    [self.view addSubview: TextView1]; 

    TextView2 = [[UITextView alloc] initWithFrame:CGRectMake(10, 5, 300, 30)]; 
    TextView2.delegate = self; 
    TextView2.backgroundColor = [UIColor whiteColor]; 
    TextView2.layer.borderWidth = 2; 
    TextView2.layer.borderColor = [UIColor lightTextColor].CGColor; 
    TextView2.layer.cornerRadius = 5; 
    [self.view addSubview: TextView2]; 

    UIBarButtonItem *textBarButton = [[UIBarButtonItem alloc] initWithCustomView: TextView2]; 

    accessoryToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    accessoryToolBar.items = [NSArray arrayWithObjects:textBarButton, nil]; 
} 

-(BOOL) textViewShouldBeginEditing:(UITextView *)textView 
{ 
    NSLog(@"TextView Should Begin Editing"); 
    if (textView == messageTextView) 
    { 
     [TextView2 setInputAccessoryView:accessoryToolBar]; 
     [TextView1 resignFirstResponder]; 
     [TextView2 becomeFirstResponder]; 
    } 
    else 
    { 
     [TextView2 setInputAccessoryView:accessoryToolBar]; 
     [TextView1 resignFirstResponder]; 
    } 

    return YES; 
} 

我該如何解決這個問題?

回答

1

刪除完整的textViewShouldBeginEditing方法。這是錯誤的一點。

你想,儘快,當用戶觸摸第一個textView,第二個應該被選中,對吧?如果這是您想要的,請改用textViewDidBeginEditing:

- (void) textViewDidBeginEditing:(UITextView *)textView 
{ 
    if(textView == TextView1) { 
    [TextView2 becomeFirstResponder]; 
    } 
} 

這應該是你需要的一切。

+0

Hello Mr.Jaydee3感謝您的幫助。這是工作。再次感謝。 – Gopinath 2012-07-14 12:25:55

相關問題