0

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

我该如何解决?有任何想法吗?

4

3 回答 3

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];
}  
于 2016-06-24T09:25:28.347 回答
1

很简单,只要换个窗口-rootViewController

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

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

于 2016-06-24T09:31:25.280 回答
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
  }
}
于 2016-06-24T09:25:50.457 回答