Swift 4如何打印文本到标签

时间:2018-12-18 21:40:36

标签: swift text

我正在尝试将文本打印到这样的文本字段中: 如何正确地做到这一点?

else {

        weak var worgnlogin: UILabel! {
            worgnlogin.text = ("brugernavn eller password er skrevet forkert")

          }
    }

这里有我的主要ViewController代码,我对所有用户信息做了一个数组,因为我将循环遍历并找到合适的用户登录:

import UIKit

ViewController类:UIViewController {

@IBOutlet weak var txtUserName: UITextField!
@IBOutlet weak var txtPassword: UITextField!
@IBOutlet weak var worgLogin: UILabel!

let user1 = ["user": "Karsten","userID":"1","userName":"Kalle","passWord":"1234" ]
let user2 = ["user": "Rene","userID":"2","userName":"Rene" ,"passWord":"1234" ]
let user3 = ["user": "Johan","userID":"3","userName":"Johan","passWord":"1234" ]




override func viewDidLoad() {
    super.viewDidLoad()
    let array = [user1,user2,user3]
    UserDefaults.standard.set(array, forKey: "users")
    // Do any additional setup after loading the view, typically from a nib.
    if UserDefaults.standard.bool(forKey: "ISUSERLOGGEDIN") == true {
        //user is already logged in just navigate him to home screen
        let homeVc = self.storyboard?.instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
        self.navigationController?.pushViewController(homeVc, animated: false)
    }
}


@IBAction func authenticateUser(_ sender: Any) {
    if txtUserName.text == "userName" && txtPassword.text == "passWord" {
        //navigate to home screen
        UserDefaults.standard.set(true, forKey: "ISUSERLOGGEDIN")
        let homeVc = self.storyboard?.instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
        self.navigationController?.pushViewController(homeVc, animated: true)
    }else {
        displayMyAlertMessage(userMessage: "Brugernavn eller Password er skrevet forkert");

        return;
    }
}

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

func displayMyAlertMessage(userMessage:String)
{

    let myAlert = UIAlertController(title:"Alert", message:userMessage, preferredStyle: UIAlertController.Style.alert);

    let okAction = UIAlertAction(title:"Ok", style:UIAlertAction.Style.default, handler:nil);

    myAlert.addAction(okAction);

    self.present(myAlert, animated:true, completion:nil);

}

} 我希望这可以更好地理解:)

2 个答案:

答案 0 :(得分:0)

worgnlogin.text = "brugernavn eller password er skrevet forkert"

答案 1 :(得分:-1)

首先为标签创建IBOutlet:

@IBOutlet weak var worgnLoginLabel: UILabel!

然后在您的else语句中写:

self.worgnLoginLabel.text = “your message”