2016-06-24 26 views
0

我想動態更改rootViewController。這取決於json響應。我有一個BOOL值,它獲得JSON響應後發生變化。問題是即使條件爲真,這個值也不會改變。iOS更改rootViewController動態

我的代碼如下,我把它放在應用程序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 
} 

我該如何解決呢?有任何想法嗎?

+0

在'-application:didFinishLaunchingWithOptions:'你不能做這樣的事情,__你的整個概念是完全錯誤的。您需要儘快完成午餐,並開始自定義作業網絡操作_after_。 – holex

回答

1

是非常簡單的,只是改變了窗口-rootViewController

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

自已經從我最後一次在objC寫有很長一段時間可能是語法錯誤,但我認爲它可以給你一個想法。

+0

好的我會試試:) – Rockers23

+0

它工作thnx :) – Rockers23

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]; 
} 
+0

我做到了,應用程序崩潰原因:應用程序窗口預計在應用程序啓動結束時有一個根視圖控制器。 – Rockers23

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