Reputation: 431
I have a MapKit view, which initially loads with the map of North America.
I have a button on this MapKit, on my storyboard as an image. I've hooked it to my ViewController with an @IBAction
.
But, I want to hide this button when my view loads initially, and show it only when the user has zoomed in to the map.
Is it possible to somehow hide this button on the initial load? Do we need to reference the action to a UIButton or vice-versa ?
Upvotes: 1
Views: 809
Reputation: 131398
To expand on @dashblinkenlight's answer:
An IBAction is a link from a control to a piece of code that gets run when the user triggers that control.
An IBOutlet is a link in the other direction, from your code to the control.
You need to define an action and link from your control to your source if you want a control to invoke your code when the user interacts with it.
You need to define an outlet and link from your source code to your control if you want your code to interact with the control.
So you sometimes need both, as in this case.
Interface Builder has various ways to define and link outlets. The simplest, I find, is to put Interface Builder in the main editor pane and set up the assistant editor in "automatic" mode so it shows the header file for the view controller that defines the view controller you've selected.
Then I control-drag from the control (or other view object) into the header file, and select "outlet". Xcode both adds an IBOutlet
declaration to the source code, and makes the IBOutlet
connection.
I do the same thing for defining actions, only this time I tell IB to create an action, not an outlet. in this case Xcode creates the declaration for the IBAction enter code here
in the header file and also an empty "stub" action method in the .m file.
(IF you're using Swift then you don't have header and implementation files so there are not 2 places to define the action.)
Upvotes: 0
Reputation: 726479
Since @IBAction
designates a function, not an object, you cannot use it to access your UIButton
until it is called.
In order to access a button at will you need to make an @IBOutlet
in addition to @IBAction
that you have, and tie your button to it. This would let you set hidden
property as needed:
button.hidden = YES;
Upvotes: 2