Swift委托值为零

时间:2017-04-03 08:22:16

标签: ios swift xcode delegates

请在我的代码中帮助配置代理。目前,我得到的值为零。

这是第一堂课:

    @IBOutlet weak var loginField: UITextField!
    @IBOutlet weak var passField: UITextField!

    extension LoginViewController: LoginDelegate {

    func checkFields() -> [String:String] {

        var result = [String:String]()
        if loginField.text == "" && passField.text == "" {
            CRNotifications.showNotification(type: .info, title: "Введите логин и пароль", message: "Укажите логин и пароль для авторизации в системе", dismissDelay: 4)
        } else if loginField.text == "" {
            CRNotifications.showNotification(type: .info, title: "Введите логин", message: "Укажите логин для авторизации в системе", dismissDelay: 4)
        } else if passField.text == "" {
            CRNotifications.showNotification(type: .info, title: "Введите пароль", message: "Укажите пароль для авторизации в системе", dismissDelay: 4)
        } else {
            result["login"] = loginField.text
            result["password"] = passField.text
        }
        return result
    } 
}

这是第二个:

    protocol LoginDelegate {
    func checkFields() -> [String:String] 
}

class SettingsRepositoryImpl: NSObject, SettingsService {

    // MARK: Authorization
    var delegate:LoginDelegate?

    func authorization() -> AnyPromise {

        let loginAndPassword = delegate?.checkFields()

        delegate? = LoginViewController()

        let login = loginAndPassword?["login"]
        let password = loginAndPassword?["password"]

        print(login)
        print(password)

        let url = "http://gps-tracker.com.ua/login.php"
        let parameters: Parameters = [
            "login": login,
            "password": password
        ]

        let promise = Promise<Bool> { fulfill, reject in

            if Reachability.isConnectedToNetwork() == true
            {
                Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default).responseJSON { response in
                    switch response.result {
                    case .success:
                        fulfill(true)
                    case .failure:
                        CRNotifications.showNotification(type: .error, title: "Ошибка авторизации", message: "Неправильный логин или пароль", dismissDelay: 4)
                        fulfill(false)
                    }
                }
            }
            else
            {
                CRNotifications.showNotification(type: .error, title: "Ошибка связи", message: "Проблема с интернет соединением", dismissDelay: 4)
            }
        }
        return AnyPromise(promise)
    }

}

我应该补充一点,checkFields()没有零值?我完全糊涂了。我非常希望得到你的帮助。

1 个答案:

答案 0 :(得分:3)

您正在LoginViewController函数中创建authorization的新实例,但您应该如何在SettingsRepositoryImpl内创建LoginViewController的实例

fileprivate let settingsAPI = SettingsRepositoryImpl()

然后在LoginViewController

override func viewDidLoad() {
    super.viewDidLoad()
    self.settingsAPI.delegate = self
}

你也应该将你的代表改为

weak var delegate: LoginDelegate?

然后在你的函数中删除它

delegate? = LoginViewController()

您可以在authorization内拨打LoginViewController,如

self.settingsAPI.authorization()