如何在UIAlertViewStylePlainTextInput Xcode

时间:2016-03-07 04:30:44

标签: ios objective-c xcode swift uialertview

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:APP_NAME
                                                message:@"Please enter amount"
                                               delegate:self
                                      cancelButtonTitle:@"Done"
                                      otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
alert.tag = 2;
[alert show];

以上是我在UIAlertView中的编码和类型数量。当键盘出现时,我的客户想要"完成"按钮而不是"返回"。请帮我怎么做?

2 个答案:

答案 0 :(得分:3)

试试这个

<强> UIAlertView中

<强>目标C

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: APP_NAME message:@"Please enter amount" delegate:self cancelButtonTitle:@"Done" otherButtonTitles: nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alert textFieldAtIndex:0];
textField.keyboardType = UIKeyboardTypeNumberPad;
// set return key type of current textfield
textField.returnKeyType = UIReturnKeyDone;
[alert show];

<强>夫特

var alert: UIAlertView = UIAlertView(title: APP_NAME, message: "Please enter amount", delegate: self, cancelButtonTitle: "Done", otherButtonTitles: "")
alert.alertViewStyle = .PlainTextInput
var textField: UITextField = alert.textFieldAtIndex(0)
textField.keyboardType = .NumberPad
 textField.returnKeyType = UIReturnKeyDone
 alert.show()

另一种类型的枚举

typedef NS_ENUM(NSInteger, UIReturnKeyType) {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),
};
  

注意 - UIAlertView已被弃用,因为iOS 8在这个地方使用UIAlertcontroller

<强> UIAlertController

<强>夫特

 var loginTextField: UITextField?
let alertController = UIAlertController(title:APP_NAME, message: "Please enter amount", preferredStyle: .Alert)
let ok = UIAlertAction(title: "Done", style: .Default, handler: { (action) -> Void in
            print("Ok Button Pressed")
        })

alertController.addAction(ok)
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
     // Enter the textfiled customization code here.
     loginTextField = textField
     //loginTextField?.placeholder = "xxx"
     loginTextField?.keyboardType = UIKeyboardType.NumberPad
     loginTextField?.returnKeyType = UIReturnKey.Done
        }
presentViewController(alertController, animated: true, completion: nil)

<强>目标C

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:APP_NAME
                                                                     message:@"Please enter amount"
                                                              preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *Done = [UIAlertAction actionWithTitle:@"Done"
                                                      style:UIAlertActionStyleDefault
                                                    handler:^(UIAlertAction *action) {
                                                            // handler code
                                                    }];

[alertController addAction:Done];

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    //handler code
    textField.keyboardType = UIKeyboardTypeNumberPad;
// set return key type of current textfield
   textField.returnKeyType = UIReturnKeyDone;
}];


[self presentViewController:alertController animated:YES completion:nil];

答案 1 :(得分:1)

enter image description here

的值

尝试此操作,在属性检查器中,将返回键更改为&#34;完成&#34;