Tej*_*uri 2 boolean objective-c ios
这可能是一个愚蠢的问题,但是在我的应用程序中,需要将bool变量传递给方法。
假设我有10个BOOL变量声明为b1,b2.....b10。
我可以BOOL简单地使用以下代码将值作为参数发送:
[self sendBoolValue:YES];
- (void)sendBoolValue:(BOOL)value
{
b1 = value;
// now b1 will be YES.
}
Run Code Online (Sandbox Code Playgroud)
现在,我需要执行以下操作:
[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.
}
Run Code Online (Sandbox Code Playgroud)
我无法发送BOOL变量。这有可能吗?
为什么我要这样做?:
我有一个UIView在3x3的网格布局中有9个子视图(我称它们为图块)。
我有两个BOOL值startTile和endTile。我需要基于触摸设置这些值!
我正在touches-Began/Moved/Ended检测这些视图上的触摸
触摸开始时,我需要计算触摸是在tile1还是tile2中.....
所以实际的代码:
- (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
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我需要调用相同的方法,但需要发送三个不同的布尔变量!
不能100%确定这是否是您想要的,但是您可以这样做:
[self sendBoolVariable:&b1];
- (void)sendBoolVariable:(BOOL *)value {
*value = YES; //b1 is now YES
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5477 次 |
| 最近记录: |