-1

Apologies beforehand if this already has an answer but I wasn't able to find the answer that I was looking for.

I have been stuck on the best way to approach passing data from the UIView to the UIViewController. Let's suppose I have this form data with information the user has filled out. The data exists in the view via the individual UITextFields. How should I pass these information to the controller to perform validation and to create a post request with this data?

Does it make sense to do this via a closure? Like the following:

@objc func submitFormData() {
    // call function passed via the view controller
}

What is the best practises for passing data between the view and the controller? For your information, I am not using StoryBoard and I am creating everything programmatically.

Answers would be greatly appreciated, thanks!

AlwaysNull
  • 348
  • 2
  • 4
  • 15
  • 1
    First off - please show us some code. ie. what have you tried. Also are you using just Apple's APIs or are you using 3rd party APIs as well. Secondly, getting data from a view back to a controller is well documented in many tutorials and posts. Delegates being the most common method supported in Apple's APIs. – drekka Jan 20 '20 at 02:08
  • @drekka thanks for replying, this is a question regarding best practises. Can you point me towards some of these tutorials that you mentioned? I am trying to do this without the usage of story boards. – AlwaysNull Jan 20 '20 at 02:24
  • I don't have any specific tutorials in mind. I'd suggest just googling for some. As far as storyboards go. I'd recommend to start with them if you are learning to write iOS apps. They will save you a lot of time and code. Coding UIs by hand is a time/code consuming thing that requires a deep understanding of how they work. It's great to have up your sleeve, but mostly it's not needed so starting out, I'd suggest using storyboards until you're comfortable writing apps. Then you'll have a better idea of when and if you need to hand code. – drekka Jan 20 '20 at 02:32
  • Does this answer your question? [Delegates in swift?](https://stackoverflow.com/questions/24099230/delegates-in-swift) – dahiya_boy Jan 20 '20 at 04:53

2 Answers2

0

In my opinion the best way to pass data from the View to the ViewController would be via Delegates and Protocols.

  • Create a protocol in the ViewController with the submitFormData() function.
  • Declare a delegate variable in the view
  • Set the ViewController as the View's delegate
  • Then in your "submitFormData" function, call the delegate.submitFormData().

There are also other ways to pass data, this is just my personal preference. Hope this helped !

Chris
  • 70
  • 8
0

First, make a structure/Model of your data and do needfull validation.

class UserDataSpecifier {

    var fields = [UserField]()

    struct UserField {
        var title: String?
        var placeHolder: String = ""
        var inputTxt: String = ""
        var image: String = ""

        init(titleStr: String, inputStr: String? = "", img: String = "") {
            title = titleStr
            placeHolder = titleStr
            inputTxt = inputStr ?? ""
            image = img
        }
    }

    init() {
        prepareForSignup()
    }

    func prepareForSignup() {
        fields.append(UserField(titleStr: "First Name")
            fields.append(UserField(titleStr: "Email"))
            fields.append(UserField(titleStr: "Password"))
    }

func isValidData(type: FormType) -> (isValid: Bool, error: String) {
     if fields[0].inputTxt.isEmpty {
       return (false, "Enter your message")
     } else if fields[1].inputTxt.isEmpty {
       return (false, "Enter your email")
     }
   return (true, "")
}

}

In your ViewController class make an instance of data. Fill the value of that object and then validate it.

   var userData: UserDataSpecifier = UserDataSpecifier()


   userData.fields[0].inputTxt = "name"
   userData.fields[1].inputTxt = "email@gmail.com

     let result = userData.isValidData()
        if result.isValid {
           print("Valid data")
        } else {
           print(result.error)
        }

You can also pass this userData instance to your view and fill your data from your View. After filling data validate it in ViewController.

shraddha11
  • 783
  • 4
  • 17