简体   繁体   English

iOS动态更改rootViewController

[英]iOS Change rootViewController Dynamically

I would like to change rootViewController dynamically. 我想动态更改rootViewController。 It depends on json response. 这取决于json响应。 I have a BOOL value , it changes after getting json response. 我有一个BOOL值,它在得到json响应后发生变化。 The problem is that this value never change even when the condition is true. 问题在于,即使条件为真,该值也不会改变。

my code is below , I put it in application didFinishLaunchingWithOptions : 我的代码在下面,我将其放在应用程序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? 有任何想法吗?

Change your code as below. 如下更改您的代码。

 {   
    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];
}  

Is pretty simple, just change the window -rootViewController 很简单,只需更改窗口-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. 由于距我上次在objC中编写代码已经很长时间了,因此可能存在语法错误,但是我认为它可以给您一个思路。

// try like this //这样尝试

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
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM