Se l'istruzione viene eseguita prima che siano impostate le sue variabili condizionali
-
30-10-2019 - |
Domanda
ho un if
Dichiarazione che è la condizione passa a questo file di implementazione tramite NSUserDefaults
come visto di seguito.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *code = [defaults objectForKey:@"codeKey"];
selectedCodeLocal = code;
Dopo questo codice per recuperare la variabile String, ho un'istruzione IF:
if (selectedCodeLocal == @"1")
textView.text = "@blah blah blah";
else
textview.text = "@abcdefghijklmnop";
Quando costruisco e corro, sembra che la variabile venga passata, ma non viene passata fino a dopo il if
L'istruzione esegue.
Ho posti NSLog
è attorno a questo codice che restituisce il mio selectedCodeLocal
Variabile stringa e il valore della variabile è sempre un passo indietro. (Ad esempio, se lo passo per la prima volta come 4, quindi passiamo come 1, verrà restituito prima nel registro come 1, quindi come 4, quindi come 1) scusa se ti ho confuso con quello.
AGGIORNARE:
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults synchronize];
selectedCodeLocal = [defaults objectForKey:@"codeKey"];
NSLog(@"set: %@",selectedCodeLocal);
self.navigationItem.title = selectedCodeLocal;
[textView setClipsToBounds:NO];
[textView setEditable:NO];
[textView setFrame:CGRectMake(20, 100, 50, 50)];
if ([selectedCodeLocal isEqualToString:@"100"])
textView.text = @"abc";
else
textView.text = @"xyz";
}
L'NSLOG visualizza ancora il vecchio valore di selectedCodeLocal
.
Aggiornamento: ecco dove è impostata quella chiave. (nella vista precedente)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the selected code
NSString *selectedCode = nil;
if(searching)
selectedCode = [copyListOfItems objectAtIndex:indexPath.row];
else {
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"codesKey"];
selectedCode = [array objectAtIndex:indexPath.row];
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:selectedCode forKey:@"codeKey"];
[defaults synchronize];
}
@Firoze lafeer: risponde alla tua domanda?
Nessuna soluzione corretta