George Coder
George Coder

Reputation: 133

Prevent a view from being loaded or appearing

I have a tab bar controller as the starting point of my app, where one of the tab and what happens subsequently is meant for admins only. So I was password protecting a tab. I thought of adding a little modal dialogue in the viewDidLoad function of my view controller (which by the way is a UITableViewController),

Suppose I can get the text that the user entered in the dialogue box in the variable inputTextField.

The relevant section of the code from viewDidLoad():

if inputTextField?.text != "secret" {
    return
} 
super.viewDidLoad()

But it does not work. Any hint appreciated. Sorry if it is too basic, I am completely new to iOS and Swift programming, so pardon my ignorance folks.

Upvotes: 1

Views: 1291

Answers (1)

Steve Rosenberg
Steve Rosenberg

Reputation: 19524

Here is a simple example. Lots of ways. I dropped two UIViews in Storyboard on the first tab's VC. The one in the back I gave a dark color to simulate the hidden secret view (secretView). Inside of the view on top (entryView) I dragged a label "Enter Passcode" and a text field (passCode). I just hid the back view unless the secret code was correct.

import UIKit

class FirstViewController: UIViewController, UITextFieldDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.passCode.delegate = self
        entryView.hidden = false
        secretView.hidden = true
    }


    @IBOutlet weak var entryView: UIView!
    @IBOutlet weak var secretView: UIView!
    let secretCode = "X"

    @IBOutlet weak var passCode: UITextField!


    func textFieldShouldReturn(textField: UITextField!) -> Bool {

        if textField.text == secretCode {

            entryView.hidden = true
            secretView.hidden = false

        } else {

            self.passCode.text = "Try Again!"
        }

        textField.resignFirstResponder()
        return true
    }
}

Upvotes: 1

Related Questions