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);
}
你能否證實'accountIndexLabel'不是''nil' – 2012-01-02 19:00:36
卷if'語句不是程序本身的問題,但它確實增加了編程錯誤的可能性因爲越來越難以理解正在發生的事情。 – 2012-01-02 19:02:35
accountIndexLabel默認爲0,但在nslog中出現「(null)」 – Netnyke 2012-01-02 19:03:31