【发布时间】:2016-02-15 13:24:40
【问题描述】:
这可能是个愚蠢的问题,但在我的应用程序中,需要将 bool 变量传递给方法。
假设我有 10 个 BOOL 变量声明为 b1,b2.....b10。
我可以通过以下代码发送BOOL 值作为参数:
[self sendBoolValue:YES];
- (void)sendBoolValue:(BOOL)value
{
b1 = value;
// now b1 will be YES.
}
现在我需要的是这样的东西:
[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.
}
我无法发送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
}
如您所见,我需要调用相同的方法但需要发送三个不同的布尔变量!!!
【问题讨论】:
-
您为什么要尝试使用 s 方法简单地将 BOOL 变量设置为 YES 或 NO?
-
有一种情况是我经常将bool值从b1修改为b10。所以我写了一个方法来根据 b1...b10 的值进行一些改变。它包含一个有 10 个 case 的 switch statemnt。每个案例执行的工作都与触摸CGPOINT有关!!!!我需要在不同的点用不同的变量调用相同的方法!!!
-
所以我没有使用多个方法,而是放置了一个方法,并且我想将变量传递给该方法
-
您应该使用更相关的代码更新您的问题,并更好地解释您要完成的工作。
-
我一定会的。是4!!我现在正赶着搭中转回家!!!我一到家就会这样做!!!
标签: ios objective-c boolean