如何在swift中使用ID进行简单登录

如何在swift中使用ID进行简单登录,swift,xcode,Swift,Xcode,我想使用简单的验证在Swift中创建登录函数,但这不起作用。 我该怎么办?当我在文本字段中写入123456,然后单击登录按钮时,应该转到下一页: @IBOutlet weak var IDtext: UITextField! var ID = self.IDtext.text @IBAction func LogButton(_ sender: Any) { If( ID == "123456" ) {

我想使用简单的验证在Swift中创建登录函数,但这不起作用。 我该怎么办?当我在文本字段中写入123456,然后单击登录按钮时,应该转到下一页:

   @IBOutlet weak var IDtext: UITextField!

        var ID = self.IDtext.text 

            @IBAction func LogButton(_ sender: Any) {

            If( ID == "123456" )
        {
    dispatch_async(dispatch_get_main_queue(), 
{ 
() -> Void in let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Home") as! UIViewController
                        self.presentViewController(viewController, animated: true, completion: nil)
                    }

         }
        else 
        {
       var alert = UIAlertView(title: "Invalid", message: "Invalid ID", delegate: self, cancelButtonTitle: "OK")
            alert.show()
         }
            }

谢谢大家!

在按钮单击事件中,首先检查您的
文本字段是否为空,如果为空,则以其他方式显示错误使用
标识符从
情节提要
获取下一个视图控制器,然后按下导航堆栈上的下一个
控制器

    @IBAction func btnLogin(_ sender: UIButton) {

        if (self.IDtext.text?.isEmpty)! {

            // Show Error
        }

        else if (self.IDtext.text? == "12456"){

            let storyboard = UIStoryboard(name: storyboard, bundle: nil)
            let viewController = storyboard.instantiateViewController(withIdentifier: "nextController") 
self.navigationController?.pushViewController(viewController, animated: true)
        }
    }

将变量初始化放在函数体中,并将函数更改为:

@IBAction func LogButton(_ sender: UIButton) {

        if self.IDtext.text?.isEmpty == false{
            if (self.IDtext.text? == "123456"){
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let viewController = storyboard.instantiateViewController(withIdentifier: "Home") 
self.presentViewController(viewController, animated: true, completion: nil)
           }
    }
}

您应该使用textfield委托属性,并使用 func textField(UITextField,shouldChangeCharactersIn:NSRange,replacementString:String)实时检查字符串{

此外,如果您的家庭类名称是“HomeVC”,则按如下方式替换您的行:-

let pushVC:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Home") as! HomeVC
  var ID = self.IDtext.text 
  if ID = "123456" {
    let pushVC:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Home") as! UIViewController
    self.navigationController?.pushViewController(pushVC, animated: true)
  } else {
     var alert = UIAlertView(title: "Invalid", message: "Invalid ID", delegate: self, cancelButtonTitle: "OK")
        alert.show()
  }
}
let pushVC:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Home") as! HomeVC