I am new to ios, coming from heavy UI work in C#.
I have an app that I wanna control which button are enabled disabled based on some logic in my code.
to do this I created a small method to handle the state of the buttons, like so :
-(void)activateUI:(BOOL *)activate {
[ validateDataBtn setEnabled: *activate ] ;
[ modifyCompDataBtn setEnabled: *activate ] ;
[ saveCompDataBtn setEnabled: *activate ] ;
}
Where all of those IBOutlets are bound to UI buttons like so :
__weak IBOutlet UIButton *saveCompDataBtn;
__weak IBOutlet UIButton *modifyCompDataBtn;
__weak IBOutlet UIButton *validateDataBtn;
when I run the app I get an exception on the first line of my method :
[ validateDataBtn setEnabled: *activate ] ;
the error is EXC_BAD_ACCESS (code = 2, address =0X0)
what am I doing wrong?
EDIT: My, my. I just realized how badly I messed this one up.
Incorrect:
First off, you shouldn’t be usingBOOL *, and if you ever did useBOOL *you would reference it as&activate, not*activate. The*in C/Objective-C means the variable is a pointer; it contains an address to a block of memory.&dereferences pointers to access the value on the other side.Correct: An
*indicates that the argument is a pointer, and is also used to deference a pointer. An&in front of a variable indicates that you want to use the address of the variable in memory. I mixed them up. This means your original code is correct; the problem is that you probably call it using[self activateUI:someBOOL], which passes a BOOL, then tries to deference it like a pointer due to the*.You might be getting confused since objects are passed as
<object type> *because all variables used to handle instances of objects are pointers. BOOL, however, istypedefed toint, which is primitive, thus the*is not needed. Your method should be:If you really DID mean to pass a pointer to a
BOOLas an argument use what you originally wrote, but you need to call it like this:If you declare the method to take a pointer to a
BOOL(BOOL *), and dereference it inside the method body (*someBOOL), you need to pass the address to theBOOL(&someBOOL), not theBOOLitself. If you declare the method to take aBOOLitself (justBOOL), and use it directly in the method body (someBOOL), you need to pass theBOOLitself (someBOOL).