私は変数NSString sessionIdを宣言しました。アプリケーションのデリゲートファイルをグローバルに使用します。グローバル変数の値の問題iPhone
appdelegate.m@synthesize sessionId;
でappdelegate.h
@property (strong, nonatomic) NSMutableString *sessionId;
で
そして、このように私のビューコントローラのいずれかでそれを使用。
((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID.retain;
NSLog(@"session id = %@", ((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId);
は、私は私のビューコントローラのすべてでその変数セッションIDにアクセスすることが可能だが、問題は、値がそれで救われていないということです。 viewcontroller1.mのように、私はいくつかの値を割り当てましたが、別のviewcontroller2.mでsessionIdにアクセスしたときに、その値がコンソール上でnullとして表示されました。
この問題を解決するにはどうすればよいですか。
コードを追加:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
self.receivedData = nil;
NSError *error;
SBJSON *json = [[SBJSON new] autorelease];
NSArray *session = [json objectWithString:responseString error:&error];
SBJsonParser *parser =[[SBJsonParser alloc] init];
NSDictionary *jsonObject = [parser objectWithString:responseString];
sID = [jsonObject objectForKey:@"session"];
((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID;
NSLog(@"session id = %@", ((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId);
NSUserDefaults *sessID = [NSUserDefaults standardUserDefaults];
[sessID setObject:sID forKey:@"sessionID"];
[sessID synchronize];
NSLog(@"This session %@ saved", sID);
NSDictionary *accountsObj = [parser objectWithString:responseString];
NSString *accounts = [accountsObj objectForKey:@"account"];
NSArray *accountList = [accounts componentsSeparatedByString:@","];
NSLog(@"Accounts: %@", accountList);
if (session == nil)
NSLog(@"JSON parsing failed: %@", [error localizedDescription]);
else {
if (accountList.count > 1) {
ChooseAccountsController *accountsView = [[ChooseAccountsController alloc] initWithNibName:@"ChooseAccountsController" bundle:nil ];
self.chooseAcc = accountsView;
[self.view addSubview: accountsView.view];
} else if (accountList.count == 1) {
MainScreenController *mainScreen1 = [[MainScreenController alloc] initWithNibName:@"MainScreen" bundle:nil ];
self.mainScreen = mainScreen1;
[self.view addSubview: mainScreen1.view];
}
}
}
最初に変数を宣言した方法を教えてください。 –
私の質問 – AppDeveloper
を編集しました。あなたの変数の命名は恐ろしいです!あなたのユーザのデフォルトを名前sessIDに割り当てるのはなぜですか? – vikingosegundo