我的应用中有一个登录页面。用户输入他们的用户名和密码。我有一个API,告诉我用户名和密码是否正确以及用户是否为id。如果它们不正确,则显示UIAlertView(),询问您是否要创建帐户。视图有两个按钮。 A"否"解除视图的按钮和"是"应该联系API以创建用户帐户的按钮。我之前已经创建了警报操作,但它不能与我下面的代码一起使用。如果你不介意,请你看看并帮助我诊断问题?
RelativeLayout (root)
--> ScrollView
--> RelativeLayout
--> Views (e.g. Buttons, TextViews, etc.)
答案 0 :(得分:1)
你应该使用 UIAlertViewController 而不是 UIAlertView ,因为
UIAlertView在iOS 9中已弃用
这是Swift中UIAlertController的代码,它的使用非常简单。主要的是它是基于块的,不需要使用任何委托
let alertController = UIAlertController(title: "Default AlertController", message: "A standard alert", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action:UIAlertAction!) in
println("you have pressed the Cancel button");
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action:UIAlertAction!) in
println("you have pressed OK button");
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion:nil)
答案 1 :(得分:1)
<强>步骤1 强>
将UIAlertViewDelegate
添加到您的班级;
class UserLogin, UIAlertViewDelegate {
....
<强>步骤-2 强>
设置委托并实施&#34;是&#34;按钮loginAlert
对象;
self.loginAlert = UIAlertView(title: "No Account Found", message: "We did not find an account matching that criterea. Do you want us to create you an account?", delegate: self, cancelButtonTitle: "No", otherButtonTitles: "Yes")
self.loginAlert.show()
现在将触发clickedButtonAtIndex
功能。
答案 2 :(得分:0)
您需要设置代理,以便接收警报回调:
self.loginAlert = UIAlertView(title: "No Account Found", message: "We did not find an account matching that criterea. Do you want us to create you an account?", delegate:self, cancelButtonTitle: "No"