self-calling initializer for UI-graphic elements - iphone

I have multiple UIViews, UIButtons etc. and would like to execute some specific code for each when i first use them.
For example for my UIButton *title, when i call title.frame for the first time, i would like first to call a setter that will verify that my button isn't nill and if it is then alloc and init my button, set my title to #"TITLE" etc.
How can i do this ?

I am guessing you are talking about lazy initialization but that will require you to use the accessor method self.title.frame instead of title.frame. You can implement the getter method like this.
- (UIButton *)title {
if ( !title ) {
// Allocate the button
// Set the button title
}
return title;
}

Related

How to Pass parameters in IBAction?

I am accessing IBAction programatically & want to pass two parameter with this IBAction call.
Can any one suggest easy way...
IBActions are usually called by user interface elements, and they can't have an arbitrary number of parameters.
If you want to call the action method programmatically, you could abuse the sender parameter by passing a dictionary as an argument, holding the actual arguments you want to pass, like so:
- (void) foo
{
[self myAction: [NSDictionary dictionaryWithObject: #"bar" forKey: #"baz"]];
}
However, I would recommend creating an additional method with two parameters; the IBAction can call it with arguments appropriate to the sender, and programmatically you can call it using whatever arguments you need. This would be a possible outline for the code:
// The atual "logic" method, doing sth interesting
- (void) foo: (NSString *) s bar: (NSInteger) i
{
// some code
}
- (IBAction) myAction: (id) sender
{
// can be invoked by a button, or any view action
if (sender == self.buttonX) {
[self foo: #"x" bar: 42];
}
if (sender == self.buttonY) {
[self foo: #"y" bar: 4];
}
}
- (void) methodCallingFooBarProgrammatically
{
[self foo: #"s" bar: 17];
}
You can pass an array in the IBAction method like this:
-(IBAction)method:(id)sender
{
[sender objectAtIndex:0];
}
or you can do it like this:
-(IBAction)methodName:(NSString *)stringName:(NSString*)stringName2
{
// You can pass an array and even a dictionary
}
IBAction method follow a spesific pattern either
- (IBAction)action:(id)sender;
or
- (IBAction)action:(id)sender forEvent:(UIEvent *)event;
where sender is the UI object that sends the event, and event being the UIEvent itself.
If you are not sending these arguments then you don't want an IBAction method. Define a normal method that takes the two arguments you want and if you IBAction methods need to call it as well then do that. IBAction methods are defined as IBAction so that interface builder can find them in your code, so there is no reason to define an IBAction method that does not follow the pattern above.
the IBAction methods could receive two parameters about the sender object and the touch event, you cannot "pass" anything, you can receive only these via:
- (IBAction)action
- (IBAction)action:(id)sender
- (IBAction)action:(id)sender forEvent:(UIEvent *)event
you could use only the sender's tag property to pass a custom identifier as NSInteger.
HERE IS THE POINT
everything else what you would like to "pass" must be exists on your Model layer already! if you know what it is...
therefore, you can reach your datas from the Model layer after you receive the action.

how to pass parameter when different button call same class?

I am developing for iPhone the app I am developing has many buttons and I want all buttons to call the same class but with different parameter
for example I want button1 to call the displayimageclass
and button2 should call the same class displayimageclass so, how to different method call same class and in that same class how to fetch different button from where it is call to particular class.
That is pretty much standard. Just define as many IBAction methods as you need.
In the viewController's .h file:
- (IBAction)actionButton1:(UIButton *)sender;
- (IBAction)actionButton2:(UIButton *)sender;
- (IBAction)actionButton3:(UIButton *)sender;
And in its .m file:
{
//react here to button1 pressed
}
- (IBAction)actionButton1:(UIButton *)sender
{
//react here to button1 pressed
}
- (IBAction)actionButton2:(UIButton *)sender
{
//react here to button2 pressed
}
- (IBAction)actionButton3:(UIButton *)sender
{
//react here to button3 pressed
}
Then associate the action with the Button's events (TouchUpInside would be most appropriate, I guess).
You should add tag property to your buttons.
Suppose your buttons are
button1.tag =1;
button2.tag =2;
button3.tag =3;
When you click any button, NSLog shows related button tag number. i.e You will get your result what you want.
- (IBAction)buttonClicked:(id)sender {
UIButton* button = (UIButton *)sender;
NSLog(#"The number of tag:%d",button.tag);
}
I think it will be helpful to you.
you can implement your action method like this:
- (IBAction)action:(UIButton *)sender
{
//sender will be different for different button
}
Set tag value for buttons and get those buttons like
- (IBAction)action:(UIButton *)sender
{
UIButton *btn = (UIButton*)sender;
}
Are you looking for
1) several buttons performing the same method with the same behavior
2) several buttons performing the same method but having different behavior based on the button characteristics
3) each button performing its own method.
1) You can register several buttons on the same IBAction, they all will call it and perform the same code
2) In the method were all the buttons are registered (cf: 1) it is possible to sort the buttons by subclassing the UIButton class, giving an enum value that represents what the button should perform and then in the called method check the enum value with a switch in order to trigger the wanted behavior
The UIBarButtonItem already has this tag property so you could use them by giving a value to each of your UIBarButtonItem in the Interface Builder and set them to call the following method
- (IBAction) myButtonsMethod:(id)object
if ([object isKindOfClass:[UIBarButtonItem class]])
{
switch ([(UIBarButtonItem *)object tag])
{
case myFirstTagValue
...
}
3) #Hermann Klecker solution is the right one.

Return customized UIButton in method?

I have a method that takes a UIButton, modifies its properties and returns a UIButton. However, it doesn't ever seem to be initialized. I'm sure I'm doing something wrong with the memory management here, but don't exactly know how to fix it. No runtime errors occur.
It is called like so...
newGameBtn = [self customButtonFromButton:newGameBtn
withText:[NSString stringWithFormat:#"NEW GAME"]
withFontSize:22
withBorderColor:[UIColor orangeColor]
isSilent:YES];
[dashboardContainer addSubview:newGameBtn];
The method is defined as follows...
- (UIButton*) customButtonFromButton:(UIButton*)button withText:(NSString*)text withFontSize:(int)fontSize withBorderColor:(UIColor*)borderColor isSilent:(BOOL)isSilent {
button = [[[UIButton alloc] init] autorelease];
// Set properties from parameters
// Other conditional custom stuff here
return button;
}
NOTE: newGameBtn is of type UIButton* and is initialized with:
newGameBtn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
Another option might be to subclass UIButton, but I figured I'd try to fix this since I've already walked down this path.
You should use +[UIButton buttonWithType:] when creating buttons to get a properly initialized button.
Most classes are not properly initialized by the default -[NSObject init] method. So please look at the class reference, or superclass reference, for a usable initialization method.
In this case you should also set a frame.
You don't modify this button with your method, you're creating a completely new one with alloc-init!
If you want to change the button in your first argument just remove the first line of your method

How to know or retrieve the sender id

I have the following method
-(IBAction)back:(id)sender {
}
and would like to be able to know the sender id.
e.g. if there are multiple buttons linked to this method, I would like to know which button was pressed.
Simply use the tag property, inherited from UIView, which is a NSInteger, in a switch statement, or using if conditions.
The tag property can be set in your code, or through InterfaceBuilder.
[sender tag]
I don't know what you mean by "id" (the "sender" is an id, effectively an NSObject *), but you could use tags. You have to set the tag beforehand in Interface Builder or programmatically.
If you have set up IBOutlets for the buttons in your interface then you can simply compare the sender to those.
That is in your interface definition if you have
...
(IBOutlet) UIButton *button1;
(IBOutlet) UIButton *button2;
...
and in your implementation you have:
- (IBAction) buttonPressed: (id) sender
{
if (sender == button1) {
....
}
else if (sender == button2) {
...
}
}
Personally, I'd prefer to use different action methods for each button and then they can all call a common routine for the things that are common. However, for simple projects the above will work.
-J
Set the tag property of each button to a unique integer (either in IB or programmatically) and switch on it inside your action method.

How to add (id) sender to the following -(IBAction)?

How do you add a (id) sender to the following code?
- (IBAction) gobutton: (UIButton *) button5 {
Everything I try fails, any help would be appreciated. Thanks.
EDIT: I need to keep the (UIButton *) button 5 reference in the (IBAction)
If I recall correctly, and if you are using this in the way I think you are,
- (IBAction) gobutton: (id) sender {
if(sender == button5)
//do something...
else
//do something else...
}
Assuming that you specified button5 as a parameter to indicate that this executes in response to button5 being pressed.
Ok, first.... IBAction doesn't really mean anything special except to Interface Builder. Basically:
#define IBAction void
So whenever you see IBAction, think "void". The only reason it's there is as a flag to tell Interface Builder that a method is a valid method to connect control actions to. The Objective-C compiler doesn't need to know about it and so it's defined to void since all "action" methods return void.
Second, action methods also have one argument which could be an object of any number of types. Because of this, action methods are supposed to use type id as the type for their argument. That way they can be passed a pointer to any Objective-C object without causing the compiler to generate a type checking error.
So usually actions should work something like this:
- (IBAction)myAction:(id)sender {
if (sender == self.someButton) {
UIButton *button = (UIButton *)sender;
...
return;
} else if (sender == self.someControl) {
UIControl *control = (UIControl *)sender;
...
return;
}
}
In other words, an id is almost like an untyped pointer like a void * is routinely used in C when some function needs to take a pointer to something of unknown type. sender could be different types of control, so something generic like id is used then sender is cast to something more specific once the code knows what it is.
Anyway, there is absolutely no reason to define something as having a return type of IBAction unless you are going to use that method as a target action in Interface Builder. Having an IBAction in your app delegate seems kind of unusual....
It's not clear what you are trying to do but most actions look like:
- (IBAction) gobutton: (id)sender;
The first parameter to an action is always the sender (you can specify the type and name as appropriate).
If a method is the action for a button, then the first parameter will be the button. If that method is the action for several buttons, then the first parameter will allow you to determine which button was tapped (as Leper describes).
What problem are you actually trying to solve?
There are techniques for passing information to the action method. For example, if you have a button that appears on a table view cell and performs the same action for every cell, then in the action method, you would want to be able to determine which cell's button was tapped.
How can I get the id of the sender before the user touches the control?
Found it! Set a tag and the use viewWithTag.
Can you create a simple structure that contains both the UIButton and the sender and use that?
struct myObject
{
UIButton* button5;
id sender;
}
...or, you could create your own NSObject (probably more cocoa-y):
#instance myObject : NSObject
{
...
}