UIAlertView 操作未发生 swift

UIAlertView actions not happening swift

我的应用程序中有一个登录页面。用户输入他们的用户名和密码。我有一个 API 告诉我用户名和密码是否正确,如果正确则告诉我用户 ID。如果它们不正确,它会显示一个 UIAlertView(),询问您是否要创建一个帐户。该视图有两个按钮。关闭视图的 "No" 按钮和应该联系 API 以创建用户帐户的 "Yes" 按钮。我之前已经创建了警报操作,但它不适用于我下面的代码。如果您不介意,请看一下并帮助我诊断问题吗?

//
//  File.swift
//  Reading Logs
//
//  Created by Andrew on 12/8/15.
//  Copyright © 2015 Wilson Apps for Education. All rights reserved.
//

import UIKit

class UserLogin {

var loginAlert = UIAlertView()
    var user: String = ""
    var pass: String = ""

    func checkLogin() -> Bool{
    let defaults = NSUserDefaults.standardUserDefaults()
    let stat = defaults.valueForKey("loggedIn")
    if(String(stat!) == "0"){
        return false
    }else{
        return true
    }
}


    func logout(){
        let defaults = NSUserDefaults.standardUserDefaults()
        defaults.setValue("0", forKey: "loggedIn")
        defaults.setValue("", forKey: "logKey")
        defaults.setValue("0", forKey: "userKey")
    }

    func login(username username: String, password: String, completion: (result: String) -> Void){
        self.user = username
        self.pass = password
        let url = "http://www.wilsonfamily5.org/rlog/checkLogin.php?u=\(username)&p=\(password)"
        let nsUrl = NSURL(string:url)
        let nsUrlRequest = NSURLRequest(URL: nsUrl!)
        NSURLSession.sharedSession().dataTaskWithRequest(nsUrlRequest){
            (data, response, error) in
            guard
                let data = data,
                let contents = NSString(data:data, encoding:NSUTF8StringEncoding)
                else { return }
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                if(contents as String == "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:nil, cancelButtonTitle: "No")
                    self.loginAlert.addButtonWithTitle("Yes")
                    self.loginAlert.show()
                }else{
                    let defaults = NSUserDefaults.standardUserDefaults()
                    defaults.setValue(contents as String, forKey: "userKey")
                    defaults.setValue("1", forKey: "loggedIn")
                    completion(result: "1")

                }
            })
            }.resume()
    }

    func register(username: String, password: String){
        let url = "http://www.wilsonfamily5.org/rlog/newAccount.php?u=\(username)&p=\(password)"
        let nsUrl = NSURL(string:url)
        let nsUrlRequest = NSURLRequest(URL: nsUrl!)
        NSURLSession.sharedSession().dataTaskWithRequest(nsUrlRequest){
            (data, response, error) in
            guard
                let data = data,
                let contents = NSString(data:data, encoding:NSUTF8StringEncoding)
                else { return }
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                let defaults = NSUserDefaults.standardUserDefaults()
                defaults.setValue(contents as String, forKey: "userKey")
                defaults.setValue("1", forKey: "loggedIn")
            })
            }.resume()
    }

    func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
        print("ButtonClicked")
        if(buttonIndex == 1){
            print("1ButtonClicked")
            register(user, password: pass)
        }
    }



}

您需要设置Delegate,以便接收提醒回调:

 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"

您应该使用 UIAlertViewController 而不是 UIAlertView 因为

UIAlertView is deprecated in iOS 9

这是 Swift 中的 UIAlertController 代码,它非常简单 use.The 主要是它是基于块的,不需要使用任何委托

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)

第一步

UIAlertViewDelegate 添加到您的 class;

class UserLogin, UIAlertViewDelegate {
....

第 2 步

设置委托并实现"Yes"按钮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 函数将被触发。