How does a view appear inside the ViewController after pressing a button?

1

I'm starting to program in Swift and would like to know how to hide and show a view that is inserted in ViewController after a button is pressed.

>

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

    
asked by anonymous 15.07.2015 / 14:16

2 answers

1

To hide / show a view at runtime you use the hidden property 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 3 version of @ rafael-leao's answer:

@IBAction func buttonClicked(_ sender: UIButton) {
    myView.isHidden = true
}
    
15.07.2015 / 16:32
0

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

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

In case you need any action beyond this one to be done, such as fill in some data or consult a database, you can simply add an action of type Touch Up Inside , by clicking on the button CTRL and drag the button to the code on the function that will handle the event or create new specific function.

    
18.09.2015 / 23:09