Search code examples
iphonexcodexcode4storyboard

Updating UIImageView when a button is clicked


I am creating an app in XCode 4.2 using the storyboard feature. I have 4 views connected on the basis of Navigation controller. In one of the view I have a textbox and a button. I have implemented the keyboard hide on return by adding the textbox as the deligate. Once I am done entering on the textbox box i will press the button and it should update the UIImageView.

I understand i need to establish some kind of relation between the UIImageView on the Interface Builder and the UIImageView IBoutlet i have declared on the interface file. But how do i do this in storyboard situation ?

In the abcViewController.h i have

@interface abcViewController:UIViewController {   
   IBOutlet UITextField *code;    
   IBOutlet UIImageView *qrImage;  
}  
@property (noatomic, retain) UITextField *code;  
@property (noatomic, retain) UIImageView *qrImage  
-(IBAction)textFieldReturn:(id)sender;  
-(IBAction)goButton:(id)sender;

In the abcViewController.m i have

 @synthesize code; 
 @synthasize qrImage; 
-(IBAction)textFieldReturn:(id)sender{ 
   [sender resignFirstResponder]; 
} 
-(IBAction)goPressed:(id)sender{ 
   [qrImage setImage:[UIImage imageNamed:@"qr.png"]]; 
}

In the Interface Builder MainStoryboard.storyboard I have established the relationship in the view's First Responder for the textbox's textFieldReturn and button's goPress to corresponding events and I have linked textbox as the deligate of this view. Now do i have to connect UIImageViewer to the `First Responder as well ? If yes how ?


Solution

  • I think you may be using a few terms incorrectly, but there are two different types of connections you can make between a view in Interface Builder and the code. When you say you "established a relationship" between the 1st reponder and the button and textfield, it sounds like you connected their Actions. What that does is creates a path from the view (button, text field, etc), to the object you connected it to (in this case, the first responded, which is essentially the abcViewController). Think of this as a one-way path; all it is does is tell these controls (the button and text field) where they should send a message when it's time to, because and event happened. So when the button get's clicked, it has a built in behavior of sending a message somewhere, if it's connected. You've connected it to the goPressed: action on abcViewController, so that's what it does.

    The other side of the coin is making sure the view controller can send messages back to the controls. That's what outlets are for. It doesn't sound like you've set up any outlets, so the abcViewController has no way of sending a message to the image view that it should show up, or change it's image, or anything.

    First thing you should do is define your properties as IBOutlets also, so like:

    @property (noatomic, retain) IBOutlet UITextField *code;
    

    Then when you go back to Interface Builder, if you go to the connections tab of the inspector, when the First Responder is selected, you will see it list out all of it's outlets. You will want to connect (drag from the dot) the outlet on the first responder (abcViewController) to the corresponding view. This creates a path from the view controller to the view (the opposite of what you did with the action).

    Once that's done, if you tell the object to do something in your code, that message will get shuttled along to the object that gets created from the XIB when your view gets loaded up.