英文:
Alerts Not Showing Up On Phone Screen when Testing App
问题
我目前正在开发一个社交媒体应用,到目前为止我已经成功创建了一个 LoginViewController 和一个 SignUpViewController,当用户在 LoginViewController 上点击注册按钮时,会跳转到 SignUpViewController。目前我正在努力让 SignUpViewController 在用户没有填写所有必填字段时显示警告。然而,每次我运行应用时,在模拟器屏幕上并没有显示警告框。
我的 SignUpViewController 代码如下:
```swift
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 上点击注册按钮而所有字段都留空时,仍然会跳转到下一个视图控制器(我计划是创建个人资料的视图控制器),而不是显示警告。所有按钮都连接到了正确的位置。如果有人能帮我解决这个问题,我将不胜感激。
<details>
<summary>英文:</summary>
I am currently working on a social media app and so far I have been able to make a LoginViewController & a SignUpVIewController, and have it where when the user taps on the Sihn Up Button on the LoginViewController it takes them to the SignUpViewCOntroller. Right now I am currently trying to have it where on the SignUpViewController if not all required fields are filled out then the app will display an alert. However, every time I run the app the alert doesn't show up on the simulator screen.
My code for the SignUpViewController is as follows:
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
}
}
Essentially when I tap the SignUp button on my SignUpViewController while leaving all the fields blank it still takes me to the next view controller (which I plan to have been the profile creation view controller), instead of showing the alerts. All the buttons are connected to the correct spots. Would greatly appreciate it if anyone can help me fix this.
</details>
# 答案1
**得分**: 0
It appears you may have connected the button in your storyboard to the next view controller via a segue. If that's the case, then you should remove your segue inside the storyboard and programmatically navigate to the next screen using your `@IBAction` method.
Also, unrelated to this, but @INFINITE's comment is also a good observation. You should probably use a `guard` statement like this:
```swift
@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 you 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
}
}
英文:
It appears (I'm making an assumption here but without a screenshot of your storyboard it is hard to be certain) you may have connected the button in your storyboard to the next view controller via a segue. If that's the case then it would automatically move to the next view controller. So, here you should remove your segue inside the storyboard and programatically navigate to the next screen using your @IBAction
method.
Also, unrelated to this, but @INFINITE's comment is also a good observation. You should probably use a guard
statement here like below:
@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
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论