Declaration of a button process - iphone

I would like to understand the process of the declaration of a button.
#interface MerdaViewController : UIViewController{
IBOutlet UIButton *button;
}
#property (retain, nonatomic) IBOutlet UIButton *button;
#end
Why do we need those steps?
Is it always required?
Thank you so much.
Alex.

To create a button outlet you just need this line.
#property (retain, nonatomic) IBOutlet UIButton *button;
To create button action
-(IBAction)doSomething:(id)sender;
Just like to add another point:
Outlets should generally be weak/assign, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong/retain

at least in relatively new versions of XCode, you don't need to declare that instance variable. It's automatically taken care of.
you need the #property so you can call self.button and do whatever you like to it. IBOutlet tells XCode it's an element in your interface builder, so you can hook it up with that visual button you added to IB.
more often than not I find myself actually declaring an -(IBAction)buttonAction:(id)sender; rather than an IBOutlet. This way when you tap that button, the IBAction method is automatically triggered.

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.

create a UIViewController's iPad version xib

In xcode4.2, I have a UiViewController bundled with a same name xib file. In the UiViewController, I have some IBOutlets as below connect to the xib.
#property (retain, nonatomic) IBOutlet UIButton *topButton;
#property (retain, nonatomic) IBOutlet UIButton *hotButton;
#property (retain, nonatomic) IBOutlet UIButton *newButton;
#property (retain, nonatomic) IBOutlet UIButton *starredButton;
Now, I want to create a iPad version xib file of the UIViewController, what shall I do? i.e. how to connect one IBOutlet to two version's xib.
Any help will be appreciated! thanks advanced!
You just need two nibs with the appropriate naming
MyView~iphone.xib
MyView~ipad.xib
Then just make sure they both have the same file's owner and hook up the connections you want.
Go to New File -> Choose User Interface from left hand side -> Select Empty -> Select ipad in device Family drop down.
In xcode 4 we can't do that for a single xib,but we can create respective ipad versions all at once.Right click on target and click on duplicate.Then select "duplicate and transition to ipad".A new target and new set of xibs are created. This might help you.

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.

iPhone Development - multiple views in one window.

I want to have a UIView at the top half of my app, and a UITable at the bottom half. If I create a basic window application I can do this using IB. My main problem is that I seem to be only to do this in the delegate file which I would like to avoid.
I repeat the exact same steps I did in delegate in a separate controller and all I get is a blank screen.
0#interface RootViewController : UIWindow {
UIWindow *myWindow;
UIView *headerView;
UITableView *tableView;
}
#property (nonatomic, retain) IBOutlet UIView *headerView;
#property (nonatomic, retain) IBOutlet UITableView *tableView;
#property (nonatomic, retain) IBOutlet UIWindow *myWindow;
#end
I have each of these connect to the right view in rootviewcontroller.xib
I was wondering if anyone had any advice / snippets / examples I could use?
You need to use both the delegate file and Interface Builder. Interface Builder makes a view that binds to your delegate through IBOutlets and IBActions. I would run through several iPhone tutorials found on google before continuing.
Your RootViewController certainly shouldn't be a subclass of UIWindow. You probably want UIViewController for that.