I am pretty new to iphone programming therefore I apologize if my question could result trivial or obscure.
In many examples and tutorials I have seen that in the declaration of the outlets within Xcode (interface section of the view controller) the keyword "IBOutlet" is used or it is not, in conjunction with the class the outlet belongs to, apparently without a relevant difference.
e.g.
IBOutlet UIButton * myButton;
or
UIButton *myButton;
I have seen by myself in my experiments that both ways seem to work the same (provided that in both cases I set the proper connections in IB, and declare the property and synthesize it)
Anyone could tell me if there is a relevant difference and different behavior between the two statements?
Thank you
IBOutlet is defined the following way:
#define IBOutlet
So it has no impact on the running code, and its only purpose is to allow Interface Builder automatically determine it while parsing class header. Without IBOutlet keyword in header you will need to declare it in IB Inspector's Identity tab to make it avaliable to be connected to interface elements.
Related
I am new to Xcode and I am wondering, what does IBAction and IBOutlet do? I've tried doing simple task like 'hello world' but it seems I stuff it up. I am making app that involves a questionnaire that links to a database.
IBAction is used for Methods which performs as a result of any Action for example Button Press.
-(IBAction) buttonPress : (id) sender;
IBOutlet is used for UI related objects like Button,label, View etc.
IBOutlet UILabel *nameLabel;
Note: If you are using XIB for development, you should make use of IBAction and IBOutlet. Otherwise you will not able to map objects and methods on XIB. If you are developing everything by coding then IBAction and IBOutlet are optional.
As mentioned in the answer linked above: "IBAction and IBOutlet are macros defined to denote variables and methods that can be referred to in Interface Builder."
However in layman's terms and a simple way to think of them -
IBActions mark the methods that will be called when an event (e.g. touch down) is triggered on one of your interface builder controls (e.g. button, switch etc).
IBOutlets mark the variable references for your interface builder controls.
Outlets allow you to programatically interact with controls you layout on interface builder.
I have two classes in my app, GHHaiku (a subclass of NSObject) and GHViewController, which imports GHHaiku. In GHViewController I've instantiated GHHaiku like so:
#property (nonatomic, strong) GHHaiku *ghhaiku;
So when I need to call a method from GHHaiku in GHViewController, I can do [self.ghhaiku callThisMethod];
However, I can't figure out what to do if GHHaiku needs information from GHViewController. There's a UISegmentedControlin GHViewController, for example, and there's a method in GHHaiku that needs to know which segment of the UISegmentedControl has been chosen. How do I get it this information? I suppose I could create an int property in GHHaiku and then, in GHViewController, assign the value of the UISegmentedControl to that property, but that seems cumbersome and I can't imagine there's not a more elegant way to do it.
So what should I do?
(I suspect that Refer to a main view controller property by another class might answer my question but I'm not quite advanced enough to understand the answer given.)
This is an architectural issue that could benefit from think about separation of concerns and more clearly breaking things down roughly along MVC (model/view/controller) lines. See this on MVC competency in Cocoa.
Is GHHaiku a model object? If so, then it shouldn't really 'know' about things going on in the view layer.
I have created the outlet and property. Now can One please help me how can I connect outlet to file owner programmatically by giving some example or tutorial.Thnks in Advance.
Following is my outlet.
IBOutlet UIButton *btnStatus;
#property(nonatomic ,retain)IBOutlet UIButton *btnStatus;
IBOutlet is not at all special. In fact, it's just a macro that evaluates to nothing. It is just a signal to Interface Builder when it reads the source file. Just set it to whatever object you want, as you would with any property assignment.
Take a look at http://www.cocoadev.com/index.pl?NSButton (in general a fairly useful site) for examples.
We create property for a variable for using it in some other view. Same we do for IBOutlets. But not always for using them. Is it necessary to create property for each IBOutlet we just created it our xib? Or is it only a good practice to do so?
I like to look at it is in terms of ease of memory management, and external access. If you need to access it externally, obviously make a property. (Yes ease of memory management, if it's easy you won't mess it up, if you don't mess it up it doesn't become a bug later)
80% of the time my view controller has the IBOutlets and nobody else accesses them, therefore ivars work.
The problem is that when you don't use #property, the assigned value is still retained. Then you need to remember to release it even though you didn't retain it yourself, which I found counter-intuitive.
For that reason I usually use #property (assign) for the ones I won't be changing, and #property (retain) for everything else, and never declare IBOutlets directly as ivars.
Example:
#interface something : NSObject {
//This one needs to be RELEASED then set to nil in both viewDidUnload, and dealloc.
IBOutlet UILabel * myLabel;
//also cannot be accessed outside of "something" class (technically it can, but don't do that)
//I NEVER declare my outlets this way.
}
//This one can just be set to nil in viewDidUnload and dealloc
#property (nonatomic, retain) UILabel * myOtherLabel;
//it can also be accessed from mySomething.myOtherLabel by any other class.
//This one just works. I don't own it, the view owns it, so I don't retain/release.
#property (nonatomic, assign) UILabel * myOtherOtherLabel;
//It also provides access to outsiders.
//I wouldn't recommend using this type if you want to change the value though.
It is not necessary to create a property for each IBOutlet.
Specifically, if you access the outlet only from the class where it is declared, you do not strictly need the property. If you have a property, you get the advantages that properties offer, but you could always directly refer the outlet directly.
If you plan to access the outlet from another class, then a property is useful so you don't have to define yourself setter and getter methods.
If you want to use the IBOutlet for only the view for which you created the XIB then no need to set the property here. But yes, it is good practice to use but not mandatory to use everytime we create IBOutlet for the view.
If what you are going to display is not going to change, you can skip creating a property or a IBOutlet for that widget.
For example in a screen where you have a label and a textfield and the label always has the string "Name:" and textfield is used for getting input from the user, you have to just create a referencing outlet for the textfield to access the data input from the user. Creating a referencing outlet for label doesn't make any sense here.
I hope you get the point.
Properties are a feature in Objective-C that allow us to automatically generate accessors, and also have some other side benefits. so as Praveen S gave you an example with Label and UIText i just little explore.
Lets say you have nothing to do with UILabel you do not exactly need to set the properites but if are asking from user to give some text to your UITextField you have to set the properties. and if you setting the properties with retain, you must release it in viewDidUnload.
If you want to change the Contents of your display then you should add the property..for an example if we want to change the label text then we need to use IBOutlet nd Property so that we need to able to get to the label control that the framework will build from our nib..
i am facing a problem with IBOutlet,please Respected developers help me out, as i am fresher in iphone
for eg.
i declared buttons or controls with IBOutlet in .h file ,but many times interface builder is not showing the controls so i use to exit the application and reopen it again and i found it
so is there any thing i am missing.
thanks in advance...
Did you save your files? A mistake a lot of people new to iOS/Mac development don't realize is that Interface Builder will not show the IBOutlets unless you have saved the files first. So anytime you add an IBOutlet, make sure you save your header file before diving into Interface Builder.
How are you declaring your outlets? Also, what do you mean by "interface builder is not showing the controls"? If you have the following code:
#interface MyViewController : UIViewController {
IBOutlet UIButton *myButton;
IBOutlet UISlider *mySlider;
}
// Properties
// Methods
#end
In IB's connection inspector the outlets should show up and allow you to connect them to valid UI objects. Could you please clarify your issue?