iOS 动态更改 rootViewController

iOS Change rootViewController Dynamically

我想动态更改 rootViewController。这取决于 json 响应。我有一个 BOOL 值,它在收到 json 响应后发生变化。问题是即使条件为真,这个值也永远不会改变。

我的代码在下面,我把它放在应用程序 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
}

我该如何解决?有什么想法吗?

如下更改您的代码。

 {   
    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"]) {
        //rootView1
        //Change RootVC to RootView1
    }
    else {
        //rootView2
         //Change RootVC to RootView2
    }

}];

//Add LoadingVC (Intermediate) RootVC

}

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

// 像这样尝试

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"]) {
    [self changeRootController:YES];
} else {
   [self changeRootController:NO];
 }
}];


-(void)changeRootController:(Bool)change {
   if(change){
       //rootView1
   } else{
     //rootView2
  }
}

很简单,改成window -rootViewController

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

距离我上次用 objC 编写已经有很长时间了,可能会有语法错误,但我认为它可以给你一个想法。