How do I see a view inside the Viewcontroller after pressing a button?

Asked

Viewed 239 times

1

I’m starting to program in Swift and would like to know how do I hide and show a view which is inserted in ViewController after pressing a button.

In the app, the user will pass some data and click the button, by clicking the button some calculations are made. I would like the results to be shown in this view that will appear.

2 answers

1

To hide/show a runtime view you use the property hidden of Uiview. Example:

  @IBAction func buttonClicked(sender: UIButton) {
    myView.hidden = true
  }

In this example, myView is the reference to the view that should appear when the button is clicked. A hidden view does not receive events and also hides its subviews.

UPDATE

swift version 3 of @rafael-Leao’s reply:

@IBAction func buttonClicked(_ sender: UIButton) {
    myView.isHidden = true
}
  • It worked, thank you very much

0

Eduardo, I’m also a beginner, but I would do it with a different approach, as suggested by Rafael.

I would use the Storeboard, to build the flow of views, so when you have a button that displays a new view, just click on the button in question, with the key CTRL pressed and drag to the new View, so whenever this button is pressed it will lead to the new View.

If you need some action beyond this to be performed, such as filling in some data or consulting some database, you can simply add an action of the type Touch Up Inside, for this just click the button with the key CTRL and drag the button to code on the function that will handle the event or create new specific function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.