【发布时间】:2025-11-22 15:30:01
【问题描述】:
我有一个自定义 UIAlertVIew,其中包含 2 个文本字段,用于输入值。首次显示 customUIAlertVIew 时,视图向上移动以为键盘腾出空间。
不幸的是,每个后续显示都不是这种情况。随后的每一次,alertView 都不会向上移动键盘。
我正在寻找导致此问题的原因,以及将来如何解决此问题。
编辑: 下面是如何创建自定义 UIAlertVIew 的代码。 CustomUIAlertView.m
-(id)initWithTitle:(NSString *)title {
if (self = [super init]){
self.title = title;
self.message = @"\n\n\n\n\n\n";
[self addButtonWithTitle:@"Cancel"];
[self addButtonWithTitle:@"Done"];
[self setDelegate:self];
}
return self;
}
-(void)didPresentAlertView:(UIAlertView *)alertView{
NSArray *subViews = [UIApplication sharedApplication].keyWindow.rootViewController.
presentedViewController.view.subviews;
if (subViews.count >1){
UIView *presentedView = [subViews objectAtIndex:1];
UILabel *player1Label = [[UILabel alloc]initWithFrame:CGRectMake(20, 55, 130, 21)];
player1Label.text = [NSString stringWithFormat:@"Player 1 Score"];
// player1Label.textInputMode = UIKeyboardTypeNumberPad;
player1Label.tag = 42;
[presentedView addSubview:player1Label];
UILabel *player2Label = [[UILabel alloc]initWithFrame:CGRectMake(20, 75, 130, 21)];
player2Label.text = [NSString stringWithFormat:@"player 2 Score"];
player2Label.tag = 43;
[presentedView addSubview:player2Label];
self.p1ScoreField = [[UITextField alloc]initWithFrame:CGRectMake(150, 55, 80, 21)];
self.p1ScoreField.placeholder= @"score";
self.p1ScoreField.tag = 44;
self.p1ScoreField.keyboardType = UIKeyboardTypeNumberPad;
self.p1ScoreField.borderStyle = UITextBorderStyleRoundedRect;
self.p1ScoreField.delegate = self;
[self.p1ScoreField addTarget:self action:@selector(UITextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[presentedView addSubview:self.p1ScoreField];
self.p2ScoreField = [[UITextField alloc]initWithFrame:CGRectMake(150, 75, 80, 21)];
self.p2ScoreField.tag = 45;
self.p2ScoreField.placeholder = @"score";
self.p2ScoreField.keyboardType = UIKeyboardTypeNumberPad;
self.p2ScoreField.borderStyle = UITextBorderStyleRoundedRect;
self.p2ScoreField.delegate = self;
[self.p2ScoreField addTarget:self action:@selector(UITextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[presentedView addSubview:self.p2ScoreField];
}
}
视图呈现在单独的视图控制器中。在 viewDidAppear 方法的 alertView 上调用 Alloc/Init。
- (IBAction)addNewRoundButtonPressed:(id)sender {
[alertView show];
}
【问题讨论】:
-
完成。检查编辑。
标签: ios uialertview