【发布时间】:2025-12-17 21:40:02
【问题描述】:
我添加了显示登录和密码文本字段的 UIAlertController 代码,它适用于 iOS 8,但在 iOS 9 中不起作用。文本框缩小如下图
我正在尝试的代码如下:
- (void)toggleLoginLdap:(UIViewController *)currentVC
{
if ([UIAlertController class])
{
self.alertController= [UIAlertController
alertControllerWithTitle:@"Title of Msg"
message:@"Hello"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action){
NSString *userName = self.alertController.textFields[0].text;
NSString *password = self.alertController.textFields[1].text;
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
[self.alertController dismissViewControllerAnimated:YES completion:nil];
}];
[self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Username/Email";
//textField.preservesSuperviewLayoutMargins = YES;
textField.autoresizesSubviews = YES;
}];
[self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Password";
textField.secureTextEntry = YES;
}];
//alertController.view.autoresizesSubviews = YES;
[self.alertController addAction:ok];
[self.alertController addAction:cancel];
[currentVC presentViewController:self.alertController animated:YES completion:nil];
}
还尝试在 rootviewcontroller 上显示,但没有运气,相同的代码适用于 ios 8。项目很旧并且得到 iOS 5 的支持。任何帮助将不胜感激。谢谢。
【问题讨论】:
-
所以文本字段应该是垂直布局还是水平布局?
-
我刚试过你的代码。它工作正常。你在哪里调用这个方法 toggleLoginLdap ?
-
水平布局(下一个另一个),它也适用于我的新演示项目,但它不适用于我的旧项目(它是在以前版本的 xcode 中创建的)。
-
toggleLoginLdap 从另一个 viewController 调用。
-
请张贴方法,您在哪里调用它。也许你应该在主线程上调用它!!!
标签: objective-c uialertview ios9 xcode7 uialertcontroller