2017-04-02 69 views
-4

我不断收到15日,并从底部16行错误它告诉我:“未解决的标识符使用‘myAlert&okAction’

‘利用未解决的标识符‘myAlert & okAction’’

如果有人可以帮助,这将是非常赞赏

代码:

import UIKit 

class RegisterPageViewController: UIViewController { 
    @IBOutlet weak var userNameTextField: UITextField! 
    @IBOutlet weak var userPhoneTextField: UITextField!; 
    @IBOutlet weak var userPasswordTextField: UITextField!; 
    @IBOutlet weak var userConfirmPasswordTextField: UITextField!; 
    @IBOutlet weak var userPlugTextField: UITextField!; 

    @IBAction func RegisterButtonTapped(_ sender: Any) { 
     let userName = userNameTextField.text; 
     let userPhone = userPhoneTextField.text; 
     let userPassword = userPasswordTextField.text; 
     let userConfirmPassword = userConfirmPasswordTextField.text; 
     let userPlug = userPlugTextField.text; 

     // Check for empty fields 
     if(userName!.isEmpty || userPhone!.isEmpty || userPassword!.isEmpty || userConfirmPassword!.isEmpty || userPlug!.isEmpty) 
     { 
      displayMyAlertMessage(userMessage: "All fields are required") 

      return; 
     } 

     // check if passwords match 
     if(userPassword != userConfirmPassword) 
     { 
     // Display an alert message 
      displayMyAlertMessage(userMessage: "Passwords do not match") 
      return; 
     } 

     // Store data 
     UserDefaults.standard.set(userName, forKey: "userName") 
     UserDefaults.standard.set(userName, forKey: "userPhone") 
     UserDefaults.standard.set(userName, forKey: "userPassword") 
     UserDefaults.standard.set(userName, forKey: "userPlug") 
     UserDefaults.standard.synchronize(); 

     // Display alert message with confirmation 
     _ = UIAlertController(title:"Alert", message:"Registration is successful. Thank you!", preferredStyle: UIAlertControllerStyle.alert); 

     _ = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default){ action in 
      self.dismiss(animated: true, completion:nil) 
     } 

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

    func displayMyAlertMessage(userMessage:String) 
    { 
     let myAlert = UIAlertController(title:"Alert", message:userMessage, preferredStyle: UIAlertControllerStyle.alert); 

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

     myAlert.addAction(okAction); 

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

只是一个建议:不要使用' ;'快速,它们是可选的 –

+0

而且''if'语句中不需要括号。 – rmaddy

+0

为什么不用你对'displayMyAlertMessage'方法的调用来替换'RegisterButtonTapped'方法中的警告代码? – rmaddy

回答

0

你不能'看到'这两个对象,因为你试图在范围之外引用它们。它们的范围是它们被定义的功能。

不知道为什么你有这两条线所有,不过,如果你有

displayMyAlertMessage(userMessage:) 
0

更换

_ = UIAlertController(title:"Alert", message:"Registration is successful. Thank you!", preferredStyle: UIAlertControllerStyle.alert); 

    _ = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default){ action in 
     self.dismiss(animated: true, completion:nil) 
    } 

let myAlert = UIAlertController(title:"Alert", message:"Registration is successful. Thank you!", preferredStyle: UIAlertControllerStyle.alert); 

    let okAction = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default){ action in 
     self.dismiss(animated: true, completion:nil) 
    } 
相关问题