简体   繁体   中英

How to add parameters to an @objc function in Swift 4 when creating an application programatically?

I have always found my answers in previous posts, though not found any answer to this question yet.

After creating multiple applications with help of the storyboard, I am now making my first application with code only.

My problem, I am just trying to make a simple function (create user in Firebase and execute segue to next screen) that will be executed after a button has been pressed.

I can create a function that triggers the segue, though for some reason it seems impossible to create the user (Firebase function, needing users input from textFields) in the same function. In the below defined function I cannot use the current user input of textFields that I defined programatically in the ViewDidLoad function, nor can I create parameters in my @objc function.

@objc func registerPressed(email: String, password: String) {
    performSegue(withIdentifier: "MainVC", sender: nil)
}

What can I do? Is there a way to create parameters in the function above? Is there an easier/better way than using addTarget? Can I somehow use an @IBAction call? Should I define my textFields before the ViewDidLoad function?

No , you can't do this

@objc func registerPressed(email: String, password: String) {

for a button target selector , as the only paramter that's passed is

@objc func registerPressed(_ sender:UIButton) {

and it's logical as from where do you think the button will get those parameters for you , so you have to create the outlets for the textfields and validate them befoe sending to firebase

BTW there is no difference between creating addTarget method for the button or linking it with @IBAction

There is a perfectly standard pattern. The email and password info must already exist in instance properties. You trigger the segue and prepare(for:) will be called; it picks up the info from the properties and passes them into the destination view controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM