gpt4 book ai didi

iphone - 如何成为从一个 UITextView 到另一个 UITextView 的响应者?

转载 作者:行者123 更新时间:2023-11-29 13:32:40 26 4
gpt4 key购买 nike

我的 iPhone 应用程序中有两个 UITextView。当用户选择一个 UITextView 时, Controller 会自动转到另一个 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;
}

我该如何解决这个问题?

最佳答案

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

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

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

这应该是您需要的一切。

关于iphone - 如何成为从一个 UITextView 到另一个 UITextView 的响应者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11483351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com