不执行对“if语句”的操作

时间:2012-01-02 18:57:33

标签: iphone objective-c ios if-statement

如果某些特定条件为真,我需要设置标签文本(不要注意它们,因为这部分代码是正确的)。它应该很容易,但令人惊讶的是它不起作用!该动作完全被跳过。我认为问题是由大量的“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);
    }

1 个答案:

答案 0 :(得分:0)

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

将NSString设置为nil对象不会像在其他语言中那样在Objective-C中引发异常,实际上它正在调用nil对象上的一个setter,该对象正在{ {1}}在Objective-C中什么都不做的对象。

投票Pauls评论: - )