2012-06-27 131 views
0

在我的IF语句中,它有点奇怪,导致每次我的代码循环时,只有SLOT1被调用并显示在我的调试器中,为什么?我的发言有问题吗?谢谢。IF语句比较

choiceSlot1 = TRUE; 
    choiceSlot2 = TRUE; 
    choiceSlot3 = TRUE; 
    choiceSlot4 = TRUE; 

    if (slot1 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot1 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot1) { 

     NSLog(@"Slot1"); 
     choiceSlot1 = FALSE; 

    } 
    else if (slot2 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot2 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot2) { 

     NSLog(@"Slot2"); 
     choiceSlot2 = FALSE; 


    } 
    else if (slot3 != [carousel1 indexOfItemViewOrSubview:carousel1.superview] || twoSlot3 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot3) { 

     NSLog(@"Slot3"); 
     choiceSlot3 = FALSE; 


    } 
    else if (slot4 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot4 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot4) { 

     NSLog(@"Slot4"); 
     choiceSlot4 = FALSE; 

    } 
+2

那么,你将choiceSlot1设置为true,所以它总是进入第一个if。 – borrrden

+0

'choiceSlot1'始终为TRUE .. – Kjuly

+0

我应该如何处理if语句的其余部分? – Bazinga

回答

1

因为else if在情况下,只有验证的首if语句是false

例子:

if (true) 
{ 
    // Will be executed 
} 
else if (true) 
{ 
    // Will not be executed 
} 

if (false) 
{ 
    // Will not be executed 
} 
else if (true) 
{ 
    // Will be executed 
} 
+0

我应该如何处理if语句的其余部分? – Bazinga

+1

在if之前删除'else'。换句话说,做四个单独的'if'语句。 – Anne

+0

是的,然后在所有它被记录。 – Bazinga

1

这很容易,

if (slot1 != [(UIImageView*)[self.carousel1 currentItemView] tag] || twoSlot1 != [(UIImageView*)[self.carousel2 currentItemView] tag] || choiceSlot1) 

如果choiceSlot1为TRUE,则第一个语句总是要真(用OR,其真正如果要素之一是真的)。

当第一条语句为true时,其余的else/if不会被调用!