1

How can I make that an user can only access a View only after he/she has completed an action? For example, only access a view if the user has responded correctly to a question in a previous view in Swift 3 for IOS development

Francisco Ruiz
  • 209
  • 3
  • 12
  • 1
    That's a very broad question without code. What is your viewController doing? Is it a quiz app where the same stuff's being shown over and over or are you doing some sort of authentication where you're segueing to a new viewController once your question is answered? – Adrian Apr 06 '17 at 03:03

1 Answers1

0

This is a very broad question, but the physics for poets version is you'd want to add some sort of target action method to monitor your textField. One the question is answered, the user can segue.

One approach would be to have a "next" button that's initially disabled and enabled once the question is correctly answered.

In FirstViewController, you could have an IBOutlet to a nextButton and a textField, like so:

@IBOutlet weak var textField: UITextField!
@IBOutlet weak var nextButton: UIButton!

You'd also control+drag a segue to SecondViewController

Then, in viewDidLoad, you could disable the button initially:

nextButton.isEnabled = false

You can't segue when the button is disabled. The moment it's enabled, you can segue by pressing it.

Then, you could have a method you create to monitor your textField for a correct answer:

// this is the method that monitors your textField
func textFieldDidChange(textField: UITextField) {

    if textField.text != "42" {
        nextButton.isEnabled = false
    }

    else {
        nextButton.isEnabled = true
    }
}

In viewDidLoad, you'd add a target action after your disable nextButton. Here's what viewDidLoad looks like:

override func viewDidLoad() {
    super.viewDidLoad()
    // this sets the button to disabled...
    nextButton.isEnabled = false

    // this monitors the textField for the correct answer
    textField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
}

Here's a repo I created for you.

Community
  • 1
  • 1
Adrian
  • 16,233
  • 18
  • 112
  • 180