xcode 测试应用程序时,手机屏幕上未显示警报

stszievb  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(147)

我目前正在开发一个社交媒体应用程序,到目前为止,我已经能够制作一个LoginViewController和一个SignUpViewController,当用户点击LoginViewController上的Sihn Up按钮时,它会将用户带到SignUpViewController。现在,我正在尝试将它放在SignUpViewController上,如果没有填写所有必填字段,则应用程序将显示警报。然而,每次我运行应用程序时,警报都不会显示在模拟器屏幕上。
我的SignUpViewController代码如下所示:

import UIKit
import Parse

class SignUpViewController: UIViewController
{
  @IBOutlet weak var emailTextField: UITextField!
   
  @IBOutlet weak var usernameTextField: UITextField!
   
  @IBOutlet weak var passwordTextField: UITextField!
   
  @IBOutlet weak var confirmPasswordFieldText: UITextField!
   
  @IBOutlet weak var signUpButton: UIButton!
   
   
  override func viewDidLoad() {
    super.viewDidLoad()
  }
   
   
  @IBAction func signUpButtonTapped(_ sender: UIButton)
  {
    let username = usernameTextField.text
    let password = passwordTextField.text
    let email = emailTextField.text
    let confirmpw = confirmPasswordFieldText.text
     
    if username == "" || password == "" || email == "" || confirmpw == ""
    {
      let alert = UIAlertController(title: "Error", message: "Please fill out all required fields", preferredStyle: .alert)
      alert.addAction(UIAlertAction(title: "OK", style: .default,handler: nil))
      present(alert, animated: true, completion: nil)
      return
    }
     
    let newUser = User(username: username!, password: password!, email: email!)
  }
   
   
}
   
class User
{
  var username: String
  var password: String
  var email: String
   
  init(username: String, password: String, email: String) {
    self.username = username
    self.password = password
    self.email = email
  }
}

本质上,当我点击我的SignUpViewController上的注册按钮,而所有字段都为空时,它仍然会带我到下一个视图控制器(我计划一直是个人资料创建视图控制器),而不是显示警报。所有按钮都连接到正确的位置。如果有人能帮助我解决这个问题,我将非常感激。

t9aqgxwy

t9aqgxwy1#

看来(我在这里做一个假设,但是没有故事板的截图,很难确定)你可能已经通过一个segue把故事板中的按钮连接到了下一个视图控制器。如果是这样的话,它会自动移动到下一个视图控制器。所以,在这里,您应该删除故事板中的segue,并使用@IBAction方法以编程方式导航到下一个屏幕。
另外,与此无关,@INFINITE的评论也是一个很好的观察。你可能应该在这里使用guard语句,如下所示:

@IBAction func signUpButtonTapped(_ sender: UIButton) {
    guard let username = usernameTextField.text,
          let password = passwordTextField.text,
          let email = emailTextField.text,
          let confirmpw = confirmPasswordFieldText.text else {

        // It may be a good idea to refactor this bit into a separate method
        let alert = UIAlertController(title: "Error", message: "Please fill out all required fields", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        present(alert, animated: true, completion: nil)
        return
    }
     
    let newUser = User(username: username, password: password, email: email)

    // present the next view controller here
    let storyboardInstance = UIStoryboard.init(name: "YOUR_STORYBOARD_NAME", bundle: Bundle.main)

    if let profileViewController = storyboardInstance.instantiateViewController(withIdentifier: "YOUR_VIEWCONTROLLER_STORYBOARD_ID") {
        // set whatever your have to on the profile view controller here
        // profileViewController.user = newUser

        // then, 

        // either push the next view controller
        // navigationViewController?.pushViewController(profileViewController, animated: true)

        // or present it
        // present(profileViewController, animated: true)

        // or do whatever else is needed
    }
  }

相关问题