gpt4 book ai didi

iphone - UITextView 不响应另一个 UITextView 附件 View iPhone?

转载 作者:行者123 更新时间:2023-11-29 11:08:52 29 4
gpt4 key购买 nike

我正在开发一个基于消息的 iPhone 应用程序。我有一个屏幕可以回复收到的消息。此屏幕包含两个 UITextView,如 bottomTextView 和 topTextView。

topTextView 添加为 bottomTextView 的 InputAccessory View

当用户进入屏幕时 topTextView 必须成为 FirstResponder。它正在显示,但光标未放置在 topTextView 中。光标位于 TextView bottomTextView 中。如何使 topTextView 成为第一响应者,光标就位?

这是我试过的代码:

- (void)viewDidLoad
{
[super viewDidLoad];
bottomBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
bottomBarViewImage.image = [UIImage imageNamed: @"toolbarbg~iphone.png"];
[bottomBarView addSubview: bottomBarViewImage];
[self.view addSubview: bottomBarView];

bottomTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 7.5, 210, 25)];
bottomTextView.delegate = self;
bottomTextView.backgroundColor = [UIColor clearColor];
bottomTextView.font = [UIFont fontWithName:@"Helvetica" size:14];
bottomTextView.scrollEnabled = NO;
[bottomBarView addSubview: bottomTextView];

topBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
topBarViewImage.image = [UIImage imageNamed: @"toolbarbg~iphone.png"];
[topBarView addSubview: topBarViewImage];

topTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 7.5, 210, 25)];
topTextView.delegate = self;
topTextView.backgroundColor = [UIColor clearColor];
topTextView.font = [UIFont fontWithName:@"Helvetica" size:14];
topTextView.scrollEnabled = NO;
[topBarView addSubview: topTextView];

[bottomTextView becomeFirstResponder];
[bottomTextView setInputAccessoryView: topBarView];
}

-(void) textViewDidBeginEditing:(UITextView *)textView
{
if(textView == bottomTextView)
{
bottomTextView.scrollEnabled = NO;

[topTextView becomeFirstResponder];
}
}

带有 topBarViewtopTextView 正在显示,但光标未放置在 topTextView 中。你能帮我解决这个问题吗?提前致谢。

最佳答案

我想可能是因为你在UITextView的delegate textViewDidBeginEditing:中调用了[topTextView becomeFirstResponder];。因此,只有当您开始编辑 bottomTextView 时,topTextView 才会成为第一响应者。尝试在 viewDidLoad 中调用 [topTextView becomeFirstResponder]; 而不是 [bottomTextView becomeFirstResponder];。看看进展如何。我不确定,但是 becomeFirstResponder 可能不会调用 textViewDidBeginEditing:。不确定它是否有效,但值得一试...

编辑:

我发现了一个相关的问题 here 。可能是因为textView没有马上出现,所以不能成为first responder。这是@Tom 接受的答案:

My solution: check when the keyboard (and thus the accessory view) appeared!

Step 1) Listen for the notification (make sure this code is read before you want to receive the notification).

[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(changeFirstResponder)
name:UIKeyboardDidShowNotification
object:nil];

Step 2) When the keyboard has appeared, you can set the textfield in your inputaccessoryview to become first responder:

-(void)changeFirstResponder
{
[textField becomeFirstResponder]; //will return TRUE;
}

关于iphone - UITextView 不响应另一个 UITextView 附件 View iPhone?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12458706/

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