2012-01-02 87 views
0

如果某些特定条件为真(因为部分代码正确,请不要关注它们),我需要设置标签的文本。这应该很容易,但令人惊讶的是它不起作用!该行为完全被忽略。我认为这个问题是由大量的“if语句”造成的。 这是我的代码:(被跳过是在第二片的代码的一部分)不执行到“if语句”的动作

-(void)setCustomUsername{ 

     stillChecking = YES; 

     ACAccountStore *account = [[ACAccountStore alloc] init]; 
     ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

     [account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) 
     { 
      // Did user allow us access? 
      if (granted == YES) 
      { 
       // Populate array with all available Twitter accounts 
       arrayOfAccounts = [account accountsWithAccountType:accountType]; 

       // Sanity check 
       if ([arrayOfAccounts count] > 0) 
       { 
        NSString *customUser = [self updateCustomUser]; 
        int numberOfAccounts = [arrayOfAccounts count]; 
        int accountsAdded = 0; 
        specAccountIndex = 0; 

        NSLog(@"index 0 = %@", [[arrayOfAccounts objectAtIndex:0] username]); 
        NSLog(@"index 1 = %@", [[arrayOfAccounts objectAtIndex:1] username]); 
        NSLog(@"spec_username = %@", customUser); 
        NSLog(@"numberOfAccounts = %i", numberOfAccounts); 

        // Check if a specified username exist. 
        if (isThereASpecifiedUsername) { 

         NSLog(@"3"); 
         while (numberOfAccounts > accountsAdded) { 
          NSLog(@"4"); 
          if ([customUser isEqualToString:[[arrayOfAccounts objectAtIndex:specAccountIndex] username]]) { 
           NSLog(@"NewTweet will use the account at index %i", specAccountIndex); 
           accountsAdded = numberOfAccounts; 
           stillChecking = NO; 
           //[accountIndexLabel setText:[NSString stringWithFormat:@"%i", selAccountIndex]]; 
          } 
          else{ 
           ++specAccountIndex; 
           ++accountsAdded; 
          } 
         } 
         NSLog(@"specAccountIndex: %i", specAccountIndex); 

        } 
        else { 

---------------------- -----这是重要的部分(下面)----------------------------

//we set the value of a simple integer to 0 
         specAccountIndex = 0; 

    //now we set the string "finalChoice" equal to specAccountIndex 
         NSString *finalChoice = [NSString stringWithFormat:@"%i", specAccountIndex]; 

    //now just a check (and yes, it works) 
         NSLog(@"The app will use the account at index %@", finalChoice); 

    //than we set the text of a label equal to finalChoice (This part does *NOT* work) 
         [accountIndexLabel setText:finalChoice]; 

    //than we check if the text has been set (This part does *NOT* work) 
         NSLog(@"accountIndexLabel check = %@", accountIndexLabel.text); 

         stillChecking = NO; 
        } 

        }}}]; 


     while (stillChecking) {} 

     NSLog(@"accountIndexLabel check at the end of the process = %@", accountIndexLabel.text); 
    } 
+0

你能否证实'accountIndexLabel'不是''nil' – 2012-01-02 19:00:36

+6

卷if'语句不是程序本身的问题,但它确实增加了编程错误的可能性因为越来越难以理解正在发生的事情。 – 2012-01-02 19:02:35

+0

accountIndexLabel默认为0,但在nslog中出现“(null)” – Netnyke 2012-01-02 19:03:31

回答

0

作为在Paul的评论中发现,你的accountIndexLabelnil。我想这是因为你忘了将一行从UILabel拖到所有者代码中的出口,或者忘记在你的UILabel字段之前添加outlet,或者如果你在代码中初始化你的UILabel,你忘记了分配init。

设置一个的NSString到零的对象不会提高在Objective-C,因为它会在其他语言中做一个例外,事实上它调用一个nil对象被调用的方法的nil对象上一个setter刚刚在Objective-C中什么都不做。

投票了保罗的评论:-)