0

I'm retrieving a bunch of ViewControllers programmatically. The source of those ViewController is in my StoryBoard and I'm creating them using

let page = storyboard?.instantiateViewController(withIdentifier: "features_view_controller")

What I want to do is, to access a UILabel I have inside that ViewController in my StoryBoard programmatically using preferably a string tag. Something like this maybe?

var label = page?.view("identifer")

How can I do this?

2
  • var label = page!.nameOfUrLabel Commented Jul 4, 2017 at 10:32
  • you can't access the label by identifier, easier option would be to create a @IBOutlet in your ViewController and then access it. Commented Jul 4, 2017 at 10:48

2 Answers 2

2

you can give tag to your subview and access it like:- YourController.view.viewWithTag(yoursubviewtag)

Sign up to request clarification or add additional context in comments.

Comments

1

You should add @IBOutlet property to your UIViewController class.

class YourViewController: UIViewController {
    @IBOutlet weak var label: UILabel! // link it with UILabel in the storyborad.
}

Then you will able to have an access to UILabel:

    if let page = UIStoryboard().instantiateViewController(withIdentifier: "features_view_controller") as? YourViewController {
        page.label //your label, which is currently nil, because the view controller still does not draw view.
    }

But if you need update, for example, the label text, then you should prepare "data transfer object" and pass it to your view controller. After view did load, you can update UILabel. Example below:

class YourViewController: UIViewController {
    @IBOutlet weak var label: UILabel!
    var text: String? //text for label.

    override func viewDidLoad() {
        super.viewDidLoad()
        label.text = text
    }
}

if let page = UIStoryboard().instantiateViewController(withIdentifier: "features_view_controller") as? YourViewController {
    page.text = "Test"
    //show page (present or push view controller) or manually send command for drawing view (page.view)
}

Comments

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.