Connect a UILabel in Interface Builder and XCode? - iphone

I am trying to do something as simple as add a Label to a View in XCode and IB and I can't figure out how to do it. All the samples I find online are for older versions of IB so the directions aren't correct.
I have a label on my .xib file, in my Controller.h file I have an IBOutlet UILabel declared with a Property set up.
In my Controller.m file I synthesized that Property.
In Interface Builder, I cannot for the LIFE of me figure out how to associate my label in my code with the Label on the .xib.
Whenever I try to drag the Connection to File's Owner, the only option that shows up is "View".
If I look at the Controller under the Library Window of Interface Builder, the Label shows up as a UILabel under Outlets. I am pretty sure that it used to be a type "id", but it automatically shows up as UILabel and if I try to add an "id" one, it doesn't work either.
Can someone point me to somewhere to explain this stupid thing? It should not be this difficult to make a label have text.

Assuming your view is called ExampleView. Click on the file owner and then press ⌘+4. This will highlight the identity box. Make sure that the class name is the same as the name of your class.
Save and close Interface Builder and then go into Xcode and verify:
// ExampleViewController.h
#import <UIKit/UIKit.h>
#class ExampleViewController;
#interface ExampleViewController : UIViewController {
IBOutlet UILabel *label;
}
#property (retain, nonatomic) IBOutlet UILabel *label;
#end
In your .m file:
// ExampleViewController.m
#import "ExampleViewController.h"
#implementation ExampleViewController
#synthesize label;
Then save the xcode files and open up your ExampleView. Drag a label onto the view. You are not supposed to connect that label to the Files owner.
INSTEAD YOU CLICK THE FILEOWNER. HIT ⌘+2 this will open the connections box. then you will see your outlet. Click and connect that to your label.

Make sure your property line looks like this:
#property (nonatomic, retain) IBOutlet UILabel *label;
Leave (or set) the type of the label as UILabel in Interface Builder. If that doesn't work, try File -> Reload All Class Files in Interface Builder. Your code looks good, but CardNameLabel should start with a lower-case 'c'.

Try this: click on the File's Owner icon to select it, and go to the Inspector's Identity tab (the 4th tab) and check the value of the Class setting. My guess is that's it's currently set to UIViewController.
Since the class that has the IBOutlet you declared is (or should be) a subclass of UIViewController, you'll need to change the class name to the name of your subclass (e.g., MyController, or whatever it's currently named).

Here is how to connect a UILabel to your storyboard in swift:
Click the icon in Xcode. If you are using an older version of Xcode, use the Venn Diagram at the top right of the window.
Using the bar at the top, choose your storyboard on one half of the file viewer, and your view controller on the other side.
Press control, click the UI Element you wish to create an IB Outlet/Action for, and drag it to the View Controller file:
Choose your preferences for your IBOutlet/Action:
You have successfully linked your storyboard element to your code.
You can follow this tutorial to see a video on how to connect your storyboard element to your code.

Assume that you have a viewController and a xib file which has a UILabel on this. The steps to connection the UiLabel (also the xib file) your viewController are:
1) In the header file, create UiLabel object and property for it
IBOutlet UILabel *label;
#Property (Nonatomic, retain) IBOutlet UILabel *label;
and synchthesize it in implement file
2) Open your xib file in Interface Builder
Double click on "File's Owner" then select the viewController in dropdownlist of pop-up windows to connect your xib file to controller
3) right-click on file's owner, on the pop-up dialog:
point and drag the plush (+) next to "View" and drop it on View row
point and drag the plush (+) next to "label" and drop it on the label on the view
=> now the label and the view on xib file is connected to you controller

Related

xcode 5 ios7 drag drop connections from multiple xibs to same function

I have made an iPhone application and currently it loads a xib specific for iPhone. Currently I drag drop the connections from xib to xcode for ibOutlet and ibAction.
Currently I have iphone.xib and it has MyLabel which is linked to an IBOutlet to a MyViewClass.
My question is how can I create iPad.xib, add MyLabel to it and link it to the same IBOutlet of MyViewClass.
Create a xib with your view, sized for the iPad. Set the class in the InterfaceBuilder to your class where the IBOutlet is. From there you can ctrl + drag to the IBOutlet.
I do this by selecting assistant editor (tuxedo looking icon in the top right of xcode). I then have my storyboard / xib on the left side and my class header file on the right. I then ctrl drag the element from the storyboard / xib to the class header file and it links them for me.
You can then use code to determine which xib you need to load.

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 ?
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.
First you need to make sure that your view controller class is linked to the view controller you have created in your storyboard.
Then linked the objects you declared in code to the objects in your storyboard is drag and drop as it was with xibs.

MKMapView not updating when i call setCoordinate

This is my second question today and the first was an incredibly stupid question so I'm fully expecting this one to be as well.
I have a view with an embedded MKMapView.
I want to some how be able to get at this MKMapView? Can I connect it up someway in the xib file? Or failing that ... can I just extract it directly from the parent view controller?
I want to do things like use setCoordinates but I can't figure out how to get at the MKMapView instance to call the function on.
How do I get at the MKMapView object.
In the place that you want to get at it from, set up an outlet. In the .h file, declare a mapview property
MKMapView *mapView;
then declare its property with an IBOutlet
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
You should be able to control-drag from File's Owner in your IB file to the mapview in your view to make a connection. Select "mapView" in the popup that appears and your file will be connected to the mapview object. Then you can use the property mapView in your .m file to set things on the mapview.
[self.mapView setCoordinates:myCoordinates];
Make sure you import your framework properly too.
It sounds like you are using Interface Builder to display this MKMapView. To be able to get the instance of MKMapView that is onscreen, follow these steps.
1) Open the .h file of the view controller for the view that contains the MKMapView.
2) In the instance variables section, add the line
IBOutlet MKMapView *mapView;
3) Go back to interface builder, and click on the map view you added to the XIB
4) Press Control, and while holding it down click and drag on the map view. This should make a blue line follow your mouse from the spot on the map view.
5) Drag the blue line over to the list of objects in your XIB. For a normal view controller xib, this list should be something like File's Owner. First Responder, and View. Unclick on the 'Files Owner' object.
6) This should bring up a list of connections. Click on the one that says 'mapView'
7) Profit! Now inside of your view controller, you can access the instance of the map view by using the mapView instance variable.
Make sure you set your delegate methods for the MKMapView and the methods should fire on your delegate class.

Why must I hookup an IBOutlet twice in a custom NIB / XIB?

I have created a custom myViewController class and it has the default view, as well as an IBOutlet (topleftView) to a subview.
I have created a custom NIB/XIB file to load this myViewController.xib. Inside the XIB file I have set the file owner to myViewController and set the UIViewController identity class to myViewController as well.
My question is why do I have to hook up and draw a reference from the IBOutlet in the subview to both the file owner AND the UIViewController in interface builder?
Just trying to get my head around it since this is the first time I'm creating a custom NIB/XIB. I usually just did everything in MainWindow but my application is getting too large so I want to spread things out.
If I don't have these multiple connections for the one IBOutlet to both the UIViewController and File Owner in the same nib file I crash with _EXC_BAD_ACCESS_ errors.
"Inside the XIB file I have set the
file owner to myViewController ..."
I'm not sure what you are doing here. I think this is where the problem is. How many items do you have on the top level of your XIB? It should just be File's Owner, First Responser and a View. If there is another controller object in here, that's your problem. Get rid of it.
"...and set
the UIViewController identity class to
myViewController as well"
This part is correct. To connect your custom UIViewController to the XIB, clock on your "File's Owner", go to the "Identity Inspector" then look under "Class Identity" at the "Class" field. Set this to 'myViewController' (or whatever you named it).
At this point you should do 1 ctrl-drag from your File's Owner for each outlet you have setup.
My question is why do I have to hook
up and draw a reference from the
IBOutlet in the subview to both the
file owner AND the UIViewController in
interface builder?
Short answer: You don't. Just set your File's Owner's class to the class name of your UIViewController subclass and you're set.

Very basic problem with Tab Bar Application and Interface Builder

OK, here is how to re-create the problem had:
Create a new project, using the
Tab Bar Application
Add a UILabel within SecondView.xib
Add IBOutlet UILabel* myLabel; to FirstViewController.h
Connect up myLabel in IB.
Build and run.
When I click the second tab the app crashes with:
__TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__
In IB, when using UITabBarController which has multiple UIViewControllers which all separately have their own NIB file assigned, there are two places where you need to set your UIViewController class file.
First, the obvious one was within the NIB file for each UIViewController.
Secondly, where I'd missed it, is withing the NIB file for your UITabBarController. In each UIViewController, not only do you set your NIB file, but also the Class.