RubyMotion Cookbook笔记-第二章第一节显示弹出对话框
实验目的
制作一个弹出警告对话框,可输入账号与密码
代码部分
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
#初始化一个UIAlertView实例
alert = UIAlertView.alloc.initWithTitle "Hello World",
message: "It's a trap!",
delegate: self,
cancelButtonTitle: "Cancel",
otherButtonTitles: "Ok",nil
#设置外观样式,这里使用的是登陆名和密码
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput
#设置文本输入框的键盘布局
text = alert.textFieldAtIndex 0
text.keyboardType = UIKeyboardTypeNamePhonePad
alert.show
true
end
end
扩展部分
关于UIAlertView的初始化参数中最后一项 otherButtonTitles,是一个字符串的序列,必须以一个nil变量结尾。序列中的每个字符串都被初始化为一个对应标题值的按钮。
如下代码则会额外增加一个按钮
alert = UIAlertView.alloc.initWithTitle "Hello World",
message: "It's a trap!",
delegate: self,
cancelButtonTitle: "Cancel",
otherButtonTitles: "Ok","Third Button",nil
![](https://i-blog.csdnimg.cn/blog_migrate/b0991fc0e6ddea1deb292807a7e948d2.png)
关于UIAlertViewStyle的枚举值常用的有以下几种:
- UIAlertViewStyleDefault = 0, //缺省样式
- UIAlertViewStyleSecureTextInput, //密文输入框
- UIAlertViewStylePlainTextInput, //明文输入框
- UIAlertViewStyleLoginAndPasswordInput //上方明文下方密文输入框