gpt4 book ai didi

条件下的 Swift 自定义 segue

转载 作者:搜寻专家 更新时间:2023-10-31 19:39:58 25 4
gpt4 key购买 nike

我正在尝试根据用户登录信息是否正确有条件地执行 segue。

我有一个从我的登录 View Controller 到一个新的 Navigation Controller 的模态转场。

我几乎尝试了所有遇到的建议,但似乎没有任何效果。使用 Sift 和 Xcode 6。

import UIKit

import AudioToolbox

class ViewController: UIViewController {

@IBOutlet weak var usernameTextField: UITextField!

@IBOutlet weak var passwordTextField: UITextField!

@IBOutlet weak var incorrectCredentialLabel: UILabel!

@IBAction func loginAction(sender: UIButton) {

var username = "test"
var password = "code"

println("Username: " + usernameTextField.text)
println("Password: " + passwordTextField.text)

if usernameTextField.text == username &&
passwordTextField.text == password {

usernameTextField.resignFirstResponder()
passwordTextField.resignFirstResponder()

println("Login Status: success")

self.shouldPerformSegueWithIdentifier("loginSegue", sender: nil)

} else {

usernameTextField.resignFirstResponder()
passwordTextField.resignFirstResponder()

AudioServicesPlayAlertSound(1352)
/*AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)*/

incorrectCredentialLabel.text = "username or password is incorrect"
incorrectCredentialLabel.textColor = UIColor.redColor()

println("Login Status: failed")
}
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

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

}

最佳答案

这对我有用。两个 UITextFields 和一个 UIButton 以及从 VC 到 VC2 的模式转场:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

let userName = "John"
let passCode = "123"

@IBOutlet weak var Name: UITextField!

@IBOutlet weak var Pass: UITextField!
@IBAction func tapButton(sender: UIButton) {

if self.Name.text == "John" && self.Pass.text == "123" {

performSegueWithIdentifier("nextView", sender: self)
}
}

//ViewController lifecycle
override func viewDidLoad() {
super.viewDidLoad()

self.Name.delegate = self
self.Pass.delegate = self

}
}

关于条件下的 Swift 自定义 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26415509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com