views:

35

answers:

1

I am using two buttons. I want to access these two buttons object from two different function.-

 (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
        button1= [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
        button2= [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
        button1.frame = CGRectMake(50, 50, 100, 30);
        button2.frame = CGRectMake(160, 50, 100, 30);
    }
    return self;
}

And My functions are foo() and bar(). In these functions i am using button1 and button2. But it is not working.

+1  A: 

First of all, I would suggest creating your buttons using Interface Builder and connecting them to IBOutlets in your controller. Then you can define accessor methods to allow you to access those buttons from functions/methods outside the controller (or use @synthesize to have them defined automatically). You'll want to be careful not to break encapsulation too much. What exactly are you trying to do with your functions?

EDIT: As I mentioned in the comments below, I've now figured out what the problem is. You need to retain the buttons, like this:

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
        button1= [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
        button2= [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
        button1.frame = CGRectMake(50, 50, 100, 30);
        button2.frame = CGRectMake(160, 50, 100, 30);
    }
    return self;
}
David
David, I don't want to use Interface Builder. Then,is there any solution? IS Above code right to initialize buttons and use them in different function?
Tauquir
@Tauquir: You don't have to use Interface Builder in order to follow the rest of my advice (although I do recommend it). What are your functions supposed to be doing? Are you sure they're functions rather than methods?
David
@David: Actually I am generating two buttons by click on a button "CLick me". For this I created "click me" button in "loadView". Again i am calling a function "pressed" to generate these 2 buttons. And I create a third button "swap" in function "pressed". Now when i click this "swap" button then these buttons should get swapped.
Tauquir
@Tauquir: So, when you click on "Click me", it calls a **function** called `pressed`? I'm not sure how you're doing that, but if you makes `pressed` a method instead, you should be able to access `button1` and `button2`, assuming they're instance variables. If this doesn't help, I think you'll need to post some more code to give more of an idea of what's going on.
David
@David - I am calling a function using ----- button addTarget:self action:@selector(buttonPressed)
Tauquir
@TauquirL `buttonPressed` is called a **method** (or a **selector** in Objective-C parlance). Anyway, I just realised that you're not retaining `button1` and `button2`, so they're being autoreleased before `buttonPressed` is called.
David
Just a side note: The code you show for generating the buttons is not correct, as the buttons returned are autoreleased and will possibly be deallocated too soon. You need to retain those two buttons if you want to use them further.
frenetisch applaudierend
@David and @frenetisch applaudierend: Can you tell me how to do it?Because the above suggestion is not working.
Tauquir
@Tauquir: Just use the code I posted in my answer. (And have a good long read of the Objective-C documentation - it's very friendly!)
David
@Tauquir: Oops, I didn't notice you said it wasn't working. Can you explain how it isn't working? Try using the debugger to find what line it gets to before failing.
David
@David - Debugger shows nothing. But two buttons still not showing up in the application. I read the Documentation and our approach is same as in documentation.
Tauquir
@Tauquir: You have to actually set a breakpoint in the `buttonPressed` method.
David
@David I got the problem. Thanks For your help.
Tauquir