是否可以手动遍历嵌套的if语句?

时间:2011-01-25 15:48:44

标签: objective-c c if-statement

二叉树让我陷入不活跃的困境,所以我想我会尝试一种更简单(如果非常混乱)的方法。
例如......

if (a) {
    do something
    // wait for a button press before checking the next 'if'
    if (aa) {
        do something
    } else if (ab) {
        do something
    }
} else if (b) {
    do something else
}

等等 在询问“aa”是否返回true之前,如何强制我的应用程序等待按下按钮? (依此类推等等。)

开关似乎是一个更清洁的替代方案,所以如果有人对该方法有答案,我会乐意尝试一下。但这是同样的问题。我无法弄清楚如何一步一步而不是一次性地进步。

2 个答案:

答案 0 :(得分:1)

如果当前功能在辅助线程中,那么您可以使用

CFRunLoopRun() - 停止线程然后按下按钮调用CFRunLoopStop()以恢复操作。

我希望您以不同的方式执行相同的实现: 将流量制动为2份

在第一部分---

if (a) {
    do something

    }
} else if (b) {
    do something else
}

在第二部分中 - 单击一次按钮即可执行此操作。

   if (aa) {
        do something
    } else if (ab) {
        do something

答案 1 :(得分:1)

这是一种不同的方法,使用视图的状态或控制变量来确定在按下按钮时它应该做什么。

// pseudo-code based on your example
-(IBAction) buttonPress1
if (a) {
    do something
    self.setState = stateA;
} else if (b) {
    do something else
    self.setState = stateB;
}

-(IBAction) buttonPress2
 if (self.state == stateA) {
    if (a) {
        do something
    } else if (b) {
        do something
    }
 }

希望有帮助,如果没有,请在评论中提出要求。


[编辑]

好的,在您解释了DA / ME / ME2参考后,我得到了您正在寻找的东西。

您可能想要做的是将Dialogue存储在plist中,以加载到NSDictionary中。 (阅读NSDictionary)

每个NSDictionary应如下所示:

key = value

@“prompt”= @“我的名字是鲍勃” @“MEAN”= NSDictionary对象,用于下一个convo选择的意思 @“NICE”= NSDictionary对象,用于下一个好的选择

const BOOL NICE = YES;
const BOOL MEAN = !NICE;

NSDictionary *convo = //loaded to initial starting point from your plist file

- (IBAction) playerChoseMean:(id)sender
{
    [self sayConvo:convo withChoice:MEAN];
}

- (IBAction) playerChoseNice:(id)sender
{
    [self sayConvo:convo withChoice:NICE];
}

- (void) sayConvo:(NSDictionary)convo withChoice:(BOOL)b
{
  NSLog(@"NPC says: %@", [convo valueForKey:@"prompt"]);
  if(b) {
     convo = (NSDictionary*)[convo valueForKey:@"NICE"];
  } else {
     convo = (NSDictionary*)[convo valueForKey:@"MEAN"];
  }

  if (convo == [NSNull null] || convo == nil) then continue;
  //else continue
}

这是一个示例NSDictionary图,可以帮助您入门。

NSDictionary *intro = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *nice = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *nicenice = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *nicegoodbye = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *mean = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *meangoodbye = [NSDictionary dictionaryWithCapacity:3];

[intro addValue:@"hi there!" forKey:prompt];
[intro addValue:nice forKey:@"NICE"];
[intro addValue:mean forKey:@"MEAN"];

[nice addValue:@"that was nice" forKey:prompt];
[nice addValue:nicenice forKey:@"NICE"];
[nice addValue:mean forKey:@"MEAN"];

[nicenice addValue:@"awww" forKey:prompt];
[nicenice addValue:nicegoodbye forKey:@"NICE"];
[nicenice addValue:mean forKey:@"MEAN"];

[nicegoodbye addValue:@"you were super nice, here's a bonus for being so nice" forKey:prompt];
[nicegoodbye addValue:[NSNull null] forKey:@"NICE"];
[nicegoodbye addValue:[NSNull null] forKey:@"MEAN"];

[mean addValue:@"that was mean" forKey:prompt];
[mean addValue:nice forKey:@"NICE"];
[mean addValue:meangoodbye forKey:@"MEAN"];

[meangoodbye addValue:@"you're a jerk!  goodbye!" forKey:prompt];
[meangoodbye addValue:[NSNull null] forKey:@"NICE"];
[meangoodbye addValue:[NSNull null] forKey:@"MEAN"];

GameDeve.SE

上查看这个优秀的例子