Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass BOOL variable as parameter in Objective-C?

This might be a stupid question, but in my app there will be a need to pass a bool variable to a method.

Let's say I have 10 BOOL variables declared as b1,b2.....b10.

I can send BOOL value as a parameter by simply with the following code:

[self sendBoolValue:YES];      

- (void)sendBoolValue:(BOOL)value 
{
    b1 = value;
    // now b1 will be YES.
}

Now what I need is something which does this:

[self sendBoolVariable:b1];  // I tried sending &b1, but it didnt work out. 

- (void)sendBoolVariable:(BOOL)value
{
    value = YES; // trying to set b1 to YES.
    // b1 is still NO.
}

I couldnt get to send the BOOL variable. IS this even possible ?

Why I am doing this?:

I have a UIView which has 9 subviews(I call these as tiles) in a 3x3 grid layout.

I have two BOOL values startTile and endTile. I need to set these values based on the touch!!!

I am using touches-Began/Moved/Ended to detect touches on these views

When the touches started, I need to calculate if the touch is in the tile1 or tile2.....

So the actual code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// calculate touch point and based on it set the bool value
    [self sendBoolVariable:startTile];
  //startTile is selected, so change its color
  // lock other tiles    

}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

  //if touches came to tile 2 region
  [self sendBoolVariable:b2];   //b2 is BOOL variable for tile2 



}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self sendBoolVariable:endTile]; 

    //end tile is selcted too
     //at this point both start tile and tile are selected
     //now do the animation for the start tile and end tile
     //other tiles are still in locked state

}

As you can see, I need to call same method but need to send three different bool variables!!!!

like image 670
Teja Nandamuri Avatar asked Mar 14 '23 04:03

Teja Nandamuri


1 Answers

Not 100% sure if this is what you want but you can do:

[self sendBoolVariable:&b1];

- (void)sendBoolVariable:(BOOL *)value {
    *value = YES; //b1 is now YES        
}
like image 88
dan Avatar answered Apr 26 '23 07:04

dan