Rockers23 Rockers23 - 5 months ago 25
iOS Question

iOS Change rootViewController Dynamically

I would like to change rootViewController dynamically. It depends on json response. I have a BOOL value , it changes after getting json response. The problem is that this value never change even when the condition is true.

my code is below , I put it in application didFinishLaunchingWithOptions :

isTime = NO;
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:
[url
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ]];


__block NSDictionary * json;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError){
json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&connectionError];
NSLog(@"JSON >>>> %@",[json objectForKey:@"status"]);
if ([[json objectForKey:@"status"] isEqualToString:@"ok"]) {
isTime = YES;
}
}];
if(isTime){
//rootView1
}else{
//rootView2
}


How can I fix that? Any ideas?

Answer

Is pretty simple, just change the window -rootViewController

-(void) changeRootViewController:(UIViewController*) vc {
    [[[[UIApplication sharedApplication] delegate] window] setRootViewController: vc];
}

Since its been a long time from the last time I wrote in objC there could be syntax errors, but I think it can give you an idea.

Comments