我正在尝试为学校项目的ios构建一个社交网络应用程序。我的@IBAction
登录功能无效,我也不知道为什么。我得到的错误是:“只能将实例方法声明为@IBAction”
这是我的代码:
class ViewController: UIViewController {
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!`
var userUid: String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool){
if let _ = KeychainWrapper.standard.string(forKey: "uid") {
performSegue(withIdentifier: "Messages", sender: nil)
}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SignUp" {
if let destination = segue.destination as? SignUpVC {
if self.userUid != nil{
destination.userUid = userUid
}
if self.emailField.text != nil {
destination.emailField = emailField.text
}
if self.passwordField.text != nil {
destination.passwordField = passwordField.text
}
}
}
}
@IBAction func SignIn (_ sender: AnyObject) {
if let email = emailField.text, let password = passwordField.text{
Auth.auth().signIn(withEmail: email, password: password, completion:{
(user, error) in
if error == nil{
self.userUid = user?.user.uid
KeychainWrapper.standard.set(self.userUid, forKey: "uid")
self.performSegue(withIdentifier: "Messages", sender: nil)
} else {
self.performSegue(withIdentifier: "SignUp", sender: nil)
}
})
}
}
}
}
答案 0 :(得分:1)
其原因是您的SignIn
不是实例方法,而是viewDidAppear
中的嵌套函数。在viewDidAppear
override func viewDidAppear(_ animated: Bool){
if let _ = KeychainWrapper.standard.string(forKey: "uid") {
performSegue(withIdentifier: "Messages", sender: nil)
}
}
并在文件末尾删除一个
答案 1 :(得分:0)
函数func prepare(for segue: UIStoryboardSegue, sender: Any?)
应该在类范围内,而不是嵌套在viewDidAppear
内
override func viewDidAppear(_ animated: Bool){
super.viewDidAppear(animated)
if let _ = KeychainWrapper.standard.string(forKey: "uid"){
performSegue(withIdentifier: "Messages", sender: nil)
}
}
@IBAction func SignIn (_ sender: AnyObject) {
if let email = emailField.text, let password = passwordField.text{
Auth.auth().signIn(withEmail: email, password: password, completion:{
(user, error) in
if error == nil{
self.userUid = user?.user.uid
KeychainWrapper.standard.set(self.userUid, forKey: "uid")
self.performSegue(withIdentifier: "Messages", sender: nil)
} else {
self.performSegue(withIdentifier: "SignUp", sender: nil)
}
})
}
}
func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "SignUp"{
if let destination = segue.destination as? SignUpVC {
if self.userUid != nil {
destination.userUid = userUid
}
destination.emailField = emailField.text
destination.passwordField = passwordField.text
}
}
}
答案 2 :(得分:0)
您准备进行segue,并且登录功能都嵌套在viewDidAppear
方法内。使用注释可以帮助可视化地拆分功能。
class ViewController: UIViewController {
// MARK: Outlets
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!`
// MARK: Properties
var userUid: String!
// MARK: Lifecycle
override func viewDidAppear(_ animated: Bool) {
if let _ = KeychainWrapper.standard.string(forKey: "uid") {
performSegue(withIdentifier: "Messages", sender: nil)
}
}
// MARK: Navigation
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SignUp" {
if let destination = segue.destination as? SignUpVC {
if self.userUid != nil{
destination.userUid = userUid
}
if self.emailField.text != nil {
destination.emailField = emailField.text
}
if self.passwordField.text != nil {
destination.passwordField = passwordField.text
}
}
}
}
// MARK: Actions
@IBAction func SignIn (_ sender: AnyObject) {
if let email = emailField.text, let password = passwordField.text{
Auth.auth().signIn(withEmail: email, password: password, completion:{
(user, error) in
if error == nil{
self.userUid = user?.user.uid
KeychainWrapper.standard.set(self.userUid, forKey: "uid")
self.performSegue(withIdentifier: "Messages", sender: nil)
} else {
self.performSegue(withIdentifier: "SignUp", sender: nil)
}
})
}
}
}
答案 3 :(得分:0)
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SignUp" {
if let destination = segue.destination as? SignUpVC {
if self.userUid != nil{
destination.userUid = userUid
}
if self.emailField.text != nil {
destination.emailField = emailField.text
}
if self.passwordField.text != nil {
destination.passwordField = passwordField.text
}
}
}
}
@IBAction func SignIn (_ sender: AnyObject) {
if let email = emailField.text, let password = passwordField.text{
Auth.auth().signIn(withEmail: email, password: password, completion:{
(user, error) in
if error == nil{
self.userUid = user?.user.uid
KeychainWrapper.standard.set(self.userUid, forKey: "uid")
self.performSegue(withIdentifier: "Messages", sender: nil)
} else {
self.performSegue(withIdentifier: "SignUp", sender: nil)
}
})
}
应该在viewDidAppear
之外