iOS动态更改rootViewController

时间:2016-06-24 09:20:50

标签: ios objective-c rootviewcontroller

我想动态更改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
}

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

3 个答案:

答案 0 :(得分:2)

更改您的代码如下。

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

答案 1 :(得分:1)

非常简单,只需更改窗口-rootViewController

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

从我上次写在objC中已经很长时间了,可能会出现语法错误,但我认为它可以给你一个想法。

答案 2 :(得分:0)

//试试这个

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