看来有很多问题要问这个,但我尝试过的任何事都不适合我。简单地说,我希望UIAlertView
带UIAlertViewStylePlainTextInput
,但输入框在显示时预先加载了给定字符串 - 不是占位符,而是默认条目。这可能吗?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"message" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[[alert textFieldAtIndex:0] setText:[NSString stringWithFormat:@"%@", @"preloaded string"]];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
上面的示例使用了无效的setText。我也尝试过:
[[alert textFieldAtIndex:0] setPlaceholder:@"text"];
只是为了兴趣,但这甚至不显示占位符,所以也许我错过了其他的东西。
任何指针?
答案 0 :(得分:1)
改为使用UIAlertController
:
let alertController = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
// Configure UITextField here...
textField.placeholder = "Placeholder"
}
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (_) -> Void in
let textField = alertController.textFields![0] as UITextField
let value = textField.text ?? "Untitled"
/// Save value here...
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alertController.addAction(okAction)
alertController.addAction(cancelAction)
presentViewController(alertController, animated: true, completion: nil)
答案 1 :(得分:1)
你可以像
那样做<强> AlertView 强>
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"sample" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil];
[alert setAlertViewStyle: UIAlertViewStylePlainTextInput];
// Alert style customization
[[alert textFieldAtIndex:0] setDelegate:self];
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[alert textFieldAtIndex:0] setText:@"Richard"];
[[alert textFieldAtIndex:0] setPlaceholder:@"textvalue"];
[alert show];
<强> UIAlertController 强>
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:@"sample"
message:@" "
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
[textField setText:@"Richard"];
textField.placeholder = NSLocalizedString(@"textvalue", @"Login");
}];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// do your action on ok click
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// do your action on cancel click
}]];
UIViewController *viewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
if ( viewController.presentedViewController && !viewController.presentedViewController.isBeingDismissed ) {
viewController = viewController.presentedViewController;
}
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:viewController.view.frame.size.height*2.0f];
[alert.view addConstraint:constraint];
[viewController presentViewController:alert animated:YES completion:^{
}];
答案 2 :(得分:0)
您需要创建自己的自定义“弹出式”视图,其类似于here
中的UIAlertview
或使用像SDCAlertView
这样的开源实现
你还可以做的是使用UIAlertController(因为UIAlertview
在IOS 8中也被弃用)
UITextField *alert = alertController.textFields.firstObject;
[alert setText@"YOUR ALERT TEXT HERE"];
答案 3 :(得分:0)
它正在100%工作
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"message" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[[alert textFieldAtIndex:0] setText:[NSString stringWithFormat:@"%@", @"preloaded string"]];
[alert show];