0

I'm making login flow right now.
There is two view controllers for email input and password input.
After validating email account, password input view controller pops up.

The email VC and password VC is very similar but slightly different.
just like "Enthr email" & "Enter password" (UILabel text)

I think that making custom view and reusing it is efficient.
But the two view controllers need different value(email, password string).
How can I pass different value to same custom view?

Or how can I reuse view for similar views programmatically?

1
  • You have "Email Input" and "Password Input" view controllers. If it's working, stick with that. If you need to display a text input view with 10 different possible "Prompt" strings, then it would make sense to re-use a single controller. Commented Jul 28, 2022 at 16:18

2 Answers 2

1

For UIKit You can create custom textField component that takes inputType and specific customization related to that.

 enum InputType {
        case email
        case password
    }

class InputViewController: UIViewController { 
    let customTextField: CustomTextField!
    var inputType: InputType!
    
    convenience init(inputType: InputType) {
        self.init()
        self.inputType = inputType
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        customTextField = CustomTextField(inputType: self.inputType)
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, Can Yoldas. I had tried the same way before(in my case, inputType was optional type). But when the view appeared, I got nil for inputType🥲 I couldn't figure it out how to solve this issue.
Aha, the key is making textfield instance in viewDidLoad. I understood. I'll try again this way. Thank you so much. Have a nice day!!
You could make the instance in viewDidLoad or lazy loading, depends on how you want to initialize the view. Hope you have found the solution.
0

You can create Views and reuse them by using different parameters when calling them. Something like this :

struct ContentView: View {
    var body: some View {
        ZStack {
            CustomView("text1")
            CustomView("text2")
        }
    }
}

// Custom reusable view
struct CustomView: View {
    var value:String

    var body: some View{
         Text(value)
    }
}

2 Comments

Thanks, Larvouu! But I am using UIKit, so to initialize a value convenience init is needed. But it is not working 😭
The OP is not asking about SwiftUI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.