Search code examples
xcodetwitterios6social-framework

Can't wire code to UI Objects with storyboards? Xcode 4.5


I'm trying to add Twitter integration to my iOS 6 iPhone app. I have the Social framework installed, I have all the code, but I can't wire the code to a UIButton on one of my screens. I'm using Storyboards so I can easily have multiple screens. It won't let me post screenshots so I hope you can understand what I'm talking about.


Solution

  • In order to have a reference of a UIButton created inside your Storyboard, you need to:

    1) Link the ViewController's class name (the one that will handle the behavior of the UIButton) with your ViewController in your StoryBoard. To do that go to the storyboard, select the ViewController that contains the UI element you want to connect. Then select the "Identity Inspector" in the left hand corner (it is the third option). Then you have to enter your ViewController's class name inside the "class" field.

    2) Then you can use the Assistant Editor that Xcode 4.5 provides to easily connect your UI elements to your class. To show the Assistant Editor press option + command + return (Or go to View / Assistant Editor / Show Assistant Editor). Once the Assistant Editor is shown, you have to select your ViewController in the StoryBoard and automatically it will show you the @interface of your ViewController's class.

    3) Now all you have to do is to hold Control + Click on the UI element, (for this example we can use a UIButton) and drag inside the Assistant Editor under the @interface of your ViewController's class (you will notice that a blue line is projected from the button to the position of your mouse). After you drop, it will present you a dialog to let you choose the type of connection that you want, just leave the default and insert a name for your property. It should have added something like:

    @property (weak, nonatomic) IBOutlet UIButton *myButton;
    

    By default it will present you the option to create an Outlet (IBOutlet) this is just a way to tell Xcode that the property you are creating links to an UI element created in Interface Builder (in this case using the storyboard). Having that IBOutlet property of your UIButton you can access any of it's public properties, ask for it's state or change it, for instance, you can change the text of the button.

    4) But in order to add behavior to your button you have to create an IBAction. You can do this also with the help of the Assistant editor. Again, you have to Control + Click and drag another connection from your UIButton to your @interface file. But this time you have to choose "Action" in the connection type and then provide a name for it.

    One thing to note is that by default when you create an IBAction of a UI element it will selects the most common event for that element, in the case of the UIButton it is the "Touch Up Inside" event. You can create different IBActions to respond to different events of a same UI element.

    The resulting code of your @interface file should look like this:

    //  MyViewController.h
    
    #import <UIKit/UIKit.h>
    
    @interface MyViewController : UIViewController
    
    @property (weak, nonatomic) IBOutlet UIButton *myButton;
    
    - (IBAction)myButtonTouchUpInsideAction:(id)sender;
    
    @end
    

    If you take a look at the bottom of your @implementation file you should be able to see a new method that returns an IBAction like this one:

    - (IBAction)myButtonTouchUpInsideAction:(id)sender
    {
        // insert your behaviour here
    }
    

    Inside the scope of that method you can add the behavior you want your UIButton to perform when the registered event is fired.

    To use the IBOutlet property, you just need to add an @synthesize statement with your IBOutlet name after the @implementation statement like:

    @implementation MyViewController
    
    @synthesize myButton;
    

    Now you are able to use your UIButton property by calling:

    self.myButton
    

    Hope this helps! If it is not clear enough just let me know.