gpt4 book ai didi

ios - 当 UIAlertView 在屏幕上时,无法在 UIViewController 堆栈中隐藏键盘

转载 作者:可可西里 更新时间:2023-11-01 04:42:40 25 4
gpt4 key购买 nike

我花了一天的大部分时间来追踪一个非常奇怪的案例,即在事件的 UITextField 上调用 resignFirstResponder 并没有隐藏键盘,即使文本字段是第一响应者。当我将一个 View Controller 推到另一个具有事件文本字段的 View Controller 之上时,就会发生这种情况。键盘消失了(如预期的那样)。但是,如果我通过触摸第二个 View Controller 中的文本字段来恢复键盘,则对 resignFirstResponder 的后续调用无效。

下面是重现问题的简单代码。此代码是一个带有导航栏按钮的 View Controller ,用于隐藏键盘,另一个用于推送自身的另一个副本(带有确认 UIAlertView)。第一份副本没有问题。但是,如果您按下第二个副本(当第一个副本具有可见键盘时),则无法关闭键盘。只有在推送第二个副本时屏幕上出现 UIAlertView(确认)时才会发生这种情况。如果删除 #define ALERT 行,一切正常。

有人知道这里发生了什么吗?看起来 UIALertView 窗口以某种方式干扰了键盘并阻止它的窗口消失,这会混淆下一个 View 。除了在 UIALertView 消失后将第二个 View Controller 推到计时器上之外,这里还有其他解决方案吗?

对于复杂的描述,我们深表歉意。这是可运行的代码。我希望代码清晰。

@implementation DemoViewController

- (id) init {
if (!(self = [super init]))
return nil;

return self;
}

- (void) dealloc {
[_inputTextfield release];
[super dealloc];
}

- (void) loadView {
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];

_inputTextfield = [[UITextField alloc] initWithFrame:CGRectMake(0., 0., 320., 44.)];
_inputTextfield.borderStyle = UITextBorderStyleRoundedRect;
_inputTextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_inputTextfield.keyboardAppearance = UIKeyboardAppearanceAlert;
_inputTextfield.autocapitalizationType = UITextAutocapitalizationTypeNone;
_inputTextfield.autocorrectionType = UITextAutocorrectionTypeNo;
_inputTextfield.keyboardType = UIKeyboardTypeDefault;
[view addSubview:_inputTextfield];

self.view = view;
[view release];
}

- (void) viewWillAppear:(BOOL) animated {
[super viewWillAppear:animated];

UIButton *downButton = [UIButton buttonWithType:UIButtonTypeCustom];
[downButton setTitle: @"keyboard down" forState:UIControlStateNormal];
[downButton addTarget:self action:@selector(downButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[downButton sizeToFit];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:downButton] autorelease];

UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
[nextButton setTitle: @"next" forState:UIControlStateNormal];
[nextButton addTarget:self action:@selector(nextButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[nextButton sizeToFit];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:nextButton] autorelease];;

}

- (void) viewWillDisappear:(BOOL) animated {
[super viewWillDisappear:animated];
[_inputTextfield resignFirstResponder];
}

- (void) downButtonPressed:(id)sender {
[_inputTextfield resignFirstResponder];
}

#define ALERT

- (void) alertView:(UIAlertView *) alertView didDismissWithButtonIndex:(NSInteger) buttonIndex {
if (alertView.cancelButtonIndex == buttonIndex) {
return;
}
[self _nextButtonPressed];
}

- (void) _nextButtonPressed {
DemoViewController *nextViewController = [[DemoViewController alloc] init];
[self.navigationController pushViewController:nextViewController];
[nextViewController release];
}

- (void) nextButtonPressed:(id)sender {
#ifdef ALERT
UIAlertView *alert = [[UIAlertView alloc] init];
alert.message = @"Next view?";
alert.cancelButtonIndex = [alert addButtonWithTitle:@"No"];
[alert addButtonWithTitle:@"Yes"];
alert.delegate = self;
[alert show];
[alert release];
#else
[self _nextButtonPressed];
#endif
}

最佳答案

如果您不幸辞去了急救人员的职务,这里有一些可能会有所帮助的解决方案:

  1. 确定在您最后一次要求辞去第一响应者之后谁仍然是第一响应者。

  2. 尝试通过一次调用 self.view(容器 View )让所有第一响应者辞职

    [self.view endEditing:YES];
  3. 只有如果您尝试了上述所有方法但均无效,请考虑使用此解决方法。

    -(BOOL)textViewShouldEndEditing:(UITextView *)textView {
    NSArray *wins = [[UIApplication sharedApplication] windows];
    if ([wins count] > 1) {
    UIWindow *keyboardWindow = [wins objectAtIndex:1];
    keyboardWindow.hidden = YES;
    }
    return YES;
    }

关于ios - 当 UIAlertView 在屏幕上时,无法在 UIViewController 堆栈中隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7936181/

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