to connect button outlet to file owner programmatically - iphone

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.

Related

IBAction and IBOutlet Clarification

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.

Can't get the methods at interface builder

I declared one method in sample.h and implemented in sample.m . Now i want to perform one action through connecting to a button and that method. but the files owner doesn't show any of my methods. Whats the problem.. Can anyone help me. i am new to this.
Thanks in Advance.
Have you specified that the File's Owner is of type sample? You can do that on the Identity tab in Interface Builder.
If sample is not a subclass of UIViewController you would most likely add an NSObject in Interface Builder and then set the Type to sample. You would also most likely have to add an IBOutlet to your view controller/File's Owner of type sample and connect it to the sample object in IB.
You want to make a function called on button press event.
so declare method in .h file as
-(IBAction) click:(id)sender
then it shows in file's owner inspector.Make connection with button for touchUpInside.

Weird behavior of UILabel

I have a UILabel Outlet, and everytime I access its text property with multiple characters, then it will crash and gives me EXC_BAD_ACCESS. Its very weird and I can't find any solution for this issue.
Thanks.
sasayins
When it got EXC _BAD _ACCESS, would you mind get the backtrace and post it here?
Or better: post the code where you access/modify the UILabel.
Did you synthesize the property? are you releasing it somewhere by mistake? gotta put some code up in order for us to help you...
You probably forgot to link the outlet in Interface Builder to the desired UILabel. This means that the UILabel is currently nil (non existent). Drag a line from the file owner to the UILabel to connect the outlet in Interface Builder.
Make sure that your outlet does not have the same name as a property of one of the super classes of your view controller. For example, UIViewController has a title property, and if you create an outlet with the name of "title" you are overriding this property and will most likely have problems.

IBOutlet keyword strictly required?

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.

Arbitrary objects to handle actions?

My question may be a bit stupid, but why can't any object instantiated in IB handle, say, button click? I mean I'm able to add my own object to a xib, and link outlets to cotrols and control actions to object's method, but once I press the button everything just crashes (uknown selector).
Do you guys have a hint around that?
EDIT: The code, as requested:
#interface TextController {
IBOutlet UILabel * textLabel;
IBOutlet UITextField * textField;
}
-(IBAction)buttonClicked:(id)sender;
#end
#implementation TextController
-(IBAction)buttonClicked:(id)sender {
textLabel.text = #"Ololo";
}
#end
Connections in IB are ok, just believe me. It's really hard to get them wrong with all this drag'n'drop stuff :)
EDIT 2: TextController is not a file owner (in this case it works fine). However, I just want to understand why I can't wire up an action to some object (may be even not a subclass of UIViewController).
You can wire outlets and actions to any object in the nib-file. Drag an NSObject form the library palette onto your nib-file, in Interface Builder. Then go to the Identity tab of the information palette and set the Class of your object.
This way you can instantiate any object of any class from your nib. If the target you want to hook to is statically created from the nib-file. Make sure that the file's owner have at least one reference to your object, or else it will be deallocated as soon as it has been created. Targets are not retained by the sender.
If the object you want to hook up should not be statically created from your nib, then implement awakeFromNib in a class that is instantiated from the nib-file and hook up the targets in code.
Last option is if you do not have any sub-class of your own in the nib-file at all. Then implement initWithNibName:bundle: in your UIViewController subclass, and hook up your targets in code after calling the super implementation.
post code, this usually means you dont have your connections wired up correctly. Is file's owner TextController in IB?