UIAlertView动作没有迅速发生

时间:2015-12-12 15:47:24

标签: ios swift uialertview

我的应用中有一个登录页面。用户输入他们的用户名和密码。我有一个API,告诉我用户名和密码是否正确以及用户是否为id。如果它们不正确,则显示UIAlertView(),询问您是否要创建帐户。视图有两个按钮。 A"否"解除视图的按钮和"是"应该联系API以创建用户帐户的按钮。我之前已经创建了警报操作,但它不能与我下面的代码一起使用。如果你不介意,请你看看并帮助我诊断问题?

RelativeLayout (root)  
  --> ScrollView
    --> RelativeLayout
      --> Views (e.g. Buttons, TextViews, etc.)

3 个答案:

答案 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"