2012-01-02 67 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中什麼都不做。

投票了保羅的評論:-)