【问题标题】:How to becomeresponder from one UITextView to another UITextView?如何成为从一个 UITextView 到另一个 UITextView 的响应者?
【发布时间】:2012-07-14 11:25:46
【问题描述】:

我的 iPhone 应用中有两个 UITextView。当用户选择一个UITextView 时,控制器会自动转到另一个UITextView。我在我的项目中尝试了以下方式。但是第二个UITextView 不会becomefirstresponder,直到用户在第二个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;
}

我该如何解决这个问题?

【问题讨论】:

    标签: iphone ios uitextview uitextviewdelegate


    【解决方案1】:

    删除完整的textViewShouldBeginEditing 方法。这是错误的观点。

    您希望,当用户触摸第一个 textView 时,应该选择第二个,对吧?如果这是您想要的,请改用textViewDidBeginEditing:

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

    这应该是你需要的一切。

    【讨论】:

    • 您好,Jaydee 先生3 感谢您的帮助。这是工作。再次感谢。
    猜你喜欢
    • 2023-03-24
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    相关资源
    最近更新 更多