Swift3:类型的值'(任意) - > ()'没有会员' setTitle'

时间:2017-04-20 10:39:29

标签: ios swift

我正在尝试登录,而且我现在正在进行测试,而且我正在尝试日志正确时我更改了按钮的标题,但是我收到了此错误:类型&的值#39;(任何) - > ()'没有会员' SetTitle'。然后我给你留下代码和截图。

我需要帮助,因为我被阻止了,我无法继续。所有帮助将不胜感激。谢谢。

enter image description here

这是我的代码

import UIKit

class LogViewController: UIViewController {

    @IBOutlet weak var _username: UITextField!
    @IBOutlet weak var _password: UITextField!

    @IBAction func _login_button(_ sender: Any) {
        let username = _username.text
        let password = _password.text

        if(username == "" || password == "")
        {
            return
        }

        DoLogin(username!, password!)
    }



    func DoLogin(_ user:String, _ psw:String)
    {

        let url = URL(string: "http://localhost/bill/userLogin.php")
        let session = URLSession.shared

        let request = NSMutableURLRequest(url: url!)
        request.httpMethod = "POST"

        let paramToSend = "username=" + user + "&password=" + psw
        request.httpBody = paramToSend.data(using: String.Encoding.utf8)

        let task = session.dataTask(with: request as URLRequest, completionHandler: {
                (data, response, error) in
            guard let _:Data = data else
            {
                return
            }

            let json:Any?
            do
            {
                json = try JSONSerialization.jsonObject(with: data!, options: [])
            }
            catch
            {
                return
            }

            guard let server_response = json as? NSDictionary else
            {
            return
            }

            if let data_block = server_response["data"] as? NSDictionary
            {
                if let session_data = data_block["session"] as? String
                {
                    let preferences = UserDefaults.standard
                    preferences.set(session_data, forKey: "session")

                    DispatchQueue.main.async {
                        execute:self.LoginDone()
                    }
                }
            }
        })
        task.resume()
    }

    func LoginDone()
    {
        _username.isEnabled = false
        _password.isEnabled = false
        _login_button.setTitle("Logout", for: .normal)
    }


    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

2 个答案:

答案 0 :(得分:2)

你有与_login_button相关的问题,因为它是@IBAction而不是@IBOutlet。

要解决此问题,请从故事板中拖出插座并将其与适当的文件连接。

而不是设置按钮的属性。

实施例

@IBOutlet weak var _login_button_outlet: UIButton!

func LoginDone()
{
    _username.isEnabled = false
    _password.isEnabled = false
    _login_button_outlet.setTitle("Logout", for: .normal)
}

答案 1 :(得分:0)

你试图将标题设置为_login_button,而不是按钮的@IBAction方法,所以设置标题你必须创建@IBOutlet弱var _login_button:UIButton!。