2012-01-02 7 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'が '' if'文のnil' –

+6

ボリュームは、プログラム自体の問題ではありませんされていないが、それは、プログラマのエラーの可能性を増大し、 '確認することができます。何が起こっているのかを理解することがますます困難になってきているからです。 –

+0

accountIndexLabelはデフォルトでは0ですが、nslogでは "(null)"と表示されます – Netnyke

答えて

0

の通りですポールのコメントで見つかったaccountIndexLabelnilです。これは、あなたがUILabelからUILabelのアウトレットをオーナーのコードでドラッグするのを忘れたか、UILabelフィールドの前にoutletを追加するのを忘れたか、UILabelを初期化してinitを割り当てるのを忘れたコードでUILabelを初期化した場合です。

NSStringをnilオブジェクトに設定してもObjective-Cでは他の言語と同じように例外が発生しません。実際にはnilオブジェクトのsetterを呼び出しています。nilオブジェクトのメソッドを呼び出していますObjective-Cでは何もしません。