goyo
goyo

Reputation: 57

How do I get a handle to a control that was added to a view via storyboard?

Apologies for the newbie question but I couldn't find an answer to this. I would like to programmatically change the content of the image of a UIImageView. The UIImageView was added to the ViewController via storyboards. I cannot find where this UIImageView is declared so I do not know how to access it.

Upvotes: 0

Views: 50

Answers (3)

Tsvi Tannin
Tsvi Tannin

Reputation: 407

You're going to want to create a UIImageView property in whatever view controller class is responsible for the view that you added via storyboard. In order to make the connection you can hold down the control key and drag from the UI element to the corresponding property. Then in that view controller class you will be able to alter that element. See this video for reference.

Upvotes: 1

scollaco
scollaco

Reputation: 922

First you have to create a property to your UIImageView on your ".h" and link it to your UIImageView on StoryBoard

You can do that as follow: 1- Go to your storyboard, select your ImageView and press Option + Command + Enter and Xcode will show the ".h" of the class of your UIImageView. 2- With the mouse cursor on your UIImageView, hold Control, simple click and drag until your .h and create the property.

After that, on your ".m", you just need to write whatever you need:

self.myImageView.image = [UIImage imageNamed:@"myImage"];

I hope it helps.

Upvotes: 1

galrito
galrito

Reputation: 352

First, you have to declare the property for UIImageView as an outlet on the ViewController's header, like:

@property (weak, nonatomic) IBOutlet UIImageView *imageView

Second, on the storyboard, right-drag from the view controller's icon to the image view, and select the "imageView" property on the menu that appears.

Now you can reference that instance of the image view programmatically, like so:

- (void)viewDidLoad {
   [self.imageView setImage:[UIImage imageNamed:@"image"]];
}

Upvotes: 2

Related Questions