IBOutlet not connecting in Interface Builder- Xcode 4.2 - iphone

I have read other questions here, but they seem to be for Xcode 3.2 or earlier, but nothing for 4.2. :(
I started a simple project and was wanting to connect the File Owner's Outlets within my xib. The bummer is that my IBOutlet's from my ViewController.h aren't coming over.
I don't have a reputation of 10 or above, so here is a screenshot of my File's Owner not showing my IBOutlets.
Here is my ViewController.h code:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UITextField *txtName;
IBOutlet UILabel *lblMessage;
}
#property (nonatomic, retain) IBOutlet UITextField *txtName;
#property (nonatomic, retain) IBOutlet UILabel *lblMessage;
- (IBAction)doSomething;
#end
Here is my ViewController.m code:
#import "ViewController.h"
#implementation ViewController
#synthesize txtName;
#synthesize lblMessage;
- (IBAction) doSomething
{
NSString *msg = [[NSString alloc] initWithFormat:#"Hello, %#",txtName.text];
[lblMessage setText:msg];
}
#end
I am new to Objective-C and Xcode, so I could have made a mistake, but I've followed many tutorials and I can never get my IBOutlets to show. I have gone as far as to delete Xcode 4.2 and re-installed to try and fix this issue. Here is a screenshot of my Xcode Version 4.2, Build 4D199 info.
Anyone else run into this issue? Thanks anyone who can point out any mistakes I have made. Please let me know if more information is needed.

When you create your IBAction, in the .h file, there will be a connection indicator to the left of it. When it isn't connected it shows a empty circle.
Press and hold this and drag it to the item you want to connect it to. I usually open up the XIB in a new window by double clicking it.
If it wont connect you must set the File's Owner in the XIB file. Select File's Owner in the Placeholders panel. Move over to the Utilities panel and make sure the Custom class, in Identity Inspector, is set to what ever your viewcontroller is named.
I Hope this will help you.
Cheers!

Try to reassign your file owner class reference in xib file.
Then attach all your IBOutlet connections.
Hope this might be helpful to you.

Check if the Files Owner is set to "ViewController". Check the following link:
http://developer.apple.com/library/ios/#documentation/IDEs/Conceptual/Xcode4TransitionGuide/InterfaceBuilder/InterfaceBuilder.html

Two things need to be added, before Xcode will allow creation of IBOutlet for the text field from the Storyboard:
Assign the underlying ViewController as the delegate to the UITextField
Add the to the #interface declaration in the ViewController.h file:
#interface ViewController : UIViewController<UITextFieldDelegate>
Until both of these are completed, you can Ctrl-click and drag from the Storyboard to the .h file, but no IBOutlet connection will be enabled.

I've finally figured out the issue, hope this helps anyone else currently having the same problem.
It had nothing to do with the xib's file owner setting. What my issue was that I had the xib file in a different directory than the source files, thus it wasn't able to connect the outlets. Once I moved the files to the same directory, everything worked. FYI, I moved everything to the top directory. Not sure sub directories will work...
To move the files, be sure to update xcode to point to the new locations.

Related

Cannot connect Segmented Control to its IBOutlet

I have created a segmented control in the interface builder.
In my ViewController.h:
#interface ViewController : UIViewController <MKMapViewDelegate>
#property IBOutlet UISegmentedControl *Segment;
- (IBAction)switchMode:(id)sender;
#end
What I could do was to connect the Segmented Control with the IBAction but I cannot connect it with the IBOutlet!
Add a segmented control to the nib/Storyboard
Add the following code into the .h
#property(nonatomic,retain) IBOutlet UISegmentedControl *Segment;
In your storyboard or xib make sure that the files owner has the same classname as that of the class in which you written the outlet
Right click on the segmantControl and a window with outlets and actions appears
click and drag on the referencing outlet and drop it on the filesowner a new pop appears which includes your outlet written in code select it .
Connection established
you have forgotten to write parameters of property, correct it like below code
#property(nonatomic,retain) IBOutlet UISegmentedControl *Segment;
after this synthesize this property in .m file like this
#Synthesize Segment;
It appears that with some xCode update you can no longer connect certain outlets to your .h. You should be fine connecting it in your .m though:
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UISegmentedControl *mySegmentedController;
#end
I will read some docs and see when this changed. Also there really is no reason to connect this property to your public interface (hence why it is no longer allowed). Only the View Controller of that class should have control over it.

iOS - Connect UILabel from interface builder to code

I want to set a custom font on a UILabel, which I've figured out how to do just fine. My question is how do I access a UILabel that I created via the drag and drop interface builder in the code?
I've tried ctrl + clicking the UILabel and dragging it to the file's owner but it won't let me. I also tried opening the assistant editor and connecting the UILabel directly to the corresponding .h file, but that won't work either. How do I access the UILabel programatically?? I know this should be really easy.
// YourController.h
#interface YourController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *theLabel;
#end
// YourController.m
#implementation
#synthesize theLabel;
///....
#end
Now with the IBOutlet marker in your code you should be able to drag from File's Owner to the label (not the other way round) in IB.
Assuming you added the UILabel to your .xib file. ctrl click the UILabel and dragged it to your .h file. You will have to enter certain info including the name of the Label. Use the name of the label that you entered from y our .m file to access it.
By ctrl+click and dragging the UILabel from the .xib to the .h, you will basically add the UILabel as a property. It will automatically added implementation of it in the .m file so you'll be good to go.
If you want to modify the UILabel at any point, you need to declare it in the .h file, and the .m file where you plan to modify it.
When you declare it is should like roughly like:
#interface MyObject {
UILabel *_testLabel;
}
#property (nonatomic, strong) IBOutlet UILabel *testLabel
Then of course you would do,
#synthesize testObeject = _testLabel;
in your .m file. After you've done this, you can link the actual variable to your physical label in your .xib file the way you were attempting to before.

Do IBOutlets always require an instance variable in the .h file?

Sometimes I see the following code into two different formats:
Format 1:
#import <UIKit/UIKit.h>
#interface MyViewController : UIViewController {
IBOutlet UILabel *myText;
}
#property (retain, nonatomic) UILabel *myText;
-(IBAction)buttonPressed:(id)sender;
#end
Format 2:
#import <UIKit/UIKit.h>
#interface MyViewController : UIViewController {
}
#property (retain, nonatomic) IBOutlet UILabel *myText;
-(IBAction)buttonPressed:(id)sender;
#end
which is the correct format? Why?
To clarify what Hack Saw said, and more directly answer your question, it does not matter whether you put IBOutlet in your property declaration or your instance variable declaration.
What Hack Saw was trying to say is that IBOutlet and IBAction both mean nothing to the compiler (IBAction gets compiled into void). The only reason they are there is for Interface Builder to parse the file and make a list of all objects and methods that you the developer says it should care about.
IBOutlet is a marker for interface builder to find your declarations, and make them available in the drop downs in IB.
They are strictly only required if you want to have IB connect an IB object to a reference in your code, for instance, connecting a button to a UIButton * declaration.
So, the basic idea here is that Interface Builder has a list of objects it knows how to make. You could make those objects in code, but a lot of the time, you don't need more capability than what IB offers, which is actually quite a lot.
In those cases, IB takes care of that object entirely. It allocates it, and sets the various parameters, and takes care of displaying it.
However, you obviously need to be able to talk to it, as well, most of the time. In order to do this, your declare a pointer to the object, like UIButton *mybutton, but in order to let IB know you want to connect up with it, you add IBOutlet to the declaration.
IB lists the variable, you connect the button up to something in File's Owner, or sometimes firstresponder, and then IB saves that connection data, and sets everything up when the nib gets loaded.

Unable to link UITable delegate to File Owner in IB of xcode

I am facing an issue and that is -
I am using tabBarCpntroller in my application. In this there are just two views. Both the views have tables in that.
I am able to display and populate the table in the first view but as I try to connect the delegate of UITable in the IB for the second view and then run the code, it crashes.
PS: I am using the same code as in first view.
Kindly help me out with the suggestions, I would be highly grateful
Thanks in advance!!
By the looks of your question, you need UITableView, not UITable, if this is what you are using, then you will have problems.
in your header, you must define your UITableView as an IBOutlet.
.h
#interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *myTable;
}
#property (nonatomic, retain) UITableView *myTable;
#end
Once you have done this, build (only) your code, then Interface Builder will see that outlet and you can connect it.

Iphone SDK App UIImageView

I'm a beginner to Iphone Development :)
I am trying to make a button change an image. So I have my button
- (IBAction)myButton {
myUIImageView.image = [UIImage imageNamed:#"myPhoto.png"];
}
I created a UIImageView in IB and I named the label and the name of this 'myUIImageView' but XCode is telling me that it's undeclared. So my question is how do I associate this UIImageView with myUIImageView. Or perhaps how do I reference this UIImage in my myButton IBAction?
Any advice would help, thanks alot!
In your .h, you need this ivar between the curly braces
UIImageView* myUIImageView;
And after the close and before the #end, you need
#property(retain, nonatomic) IBOutlet UIImageView* myUIImageView;
and in the .m, after the #implementation line
#synthesize myUIImageView;
(release in your dealloc and viewDidUnload)
Now,
Open up Interface Builder for the .xib for this view controller
Click on File's Owner icon in the Document dialog
Bring up the Inspector
Go to the connections tab
You should see an outlet named myUIImageView with a circle next to it
Click and drag the circle to the UIImageView in your view (this connects the outlet to the view)
Save, close, rebuild
how to connect the UIImageView in interface builder with the outlet created in xcode named myUIImageView:
close interface builder and open up xcode. Heres what you need to write correctly in the following two files.
in XCode
.h file
#interface FirstViewController : UIViewController {
IBOutlet UIImageView *myUIImageView;
}
#property(retain, nonatomic) IBOutlet UIImageView *myUIImageView;
#end
.m file
after implementation you write
#synthesize myUIImageView;
release in your dealloc and viewDidUnload.
save the file in xcode then open up the xib file that is connected to the .h and .m files. For example i have firstViewcontroller.h and firstViewController.m then i have a .xib file called firstView.xib.
in Interface Builder
Now on the view drag a UIImageView and then in the document dialog you will see the file owners icon.
click on that and press CMD+2 to open up the inspector. Go to the connections tab and there will be an outlet named myUIImageView that we created in xcode. next to it is a circle which you click and drag to your UIImageView. This will connect the outlet in xcode with the imageview in interface builder.
Now save the file. close interface builder and rebuild your project.
Thats the first question answered once you ellaborated on question two i will help you with that.
Let me know if you need any more help.
PK