2011-08-02 45 views
0

當我使用ASIHttpRequest連接到服務器時,隱藏了modalviewcontroller的問題。 我在後臺線程連接並在主線程中顯示modalview。 這是我的代碼:使用ASIHTTPRequest隱藏modalviewcontroller的問題

[self performSelectorInBackground:@selector(loginServerRequest) withObject:nil]; 


    - (void)loginServerRequest { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    NSURL *url = [NSURL URLWithString:@"https://11.111.111.11/api/login"]; 
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
    [request setPostValue:[AccountSettings getCompany] forKey:@"companyName"]; 
    [request setPostValue:[AccountSettings getEmail] forKey:@"email"]; 
    [request setPostValue:[AccountSettings getPassword] forKey:@"password"]; 
    [request setRequestMethod:@"POST"]; 
    [request setTimeOutSeconds:10]; 
    [request setValidatesSecureCertificate:NO]; 
    [request setDelegate:self]; 
    [request startSynchronous]; 
    [pool drain]; 
} 
- (void)requestFinished:(ASIHTTPRequest *)request 
{ 
    [self performSelector:@selector(hideServerConnectView) withObject:nil afterDelay:0.0]; 
    int status = [request responseStatusCode]; 
    NSLog(@"%i", status); 
    if ([self.nibName isEqualToString:@"RootViewController"]) { 
     if (status == 200) { 
      //some code 
     } 
    } 
} 
- (void)hideServerConnectView { 
    [self.parentViewController dismissModalViewControllerAnimated:NO]; 
} 

如果服務器響應立即modalviewcontroller絲毫不掩飾! 如果通過幾秒鐘,那麼一切都很好。 有什麼問題?


我改變了這樣的代碼:

[self loginServerRequest]; 
    ServerConnectView *viewC = [[ServerConnectView alloc] init]; 
    [self.view addSubview:viewC.view]; 
    [self presentModalViewController:viewC animated:YES]; 
    [viewC release]; 

- (void)loginServerRequest { 
NSURL *url = [NSURL URLWithString:@"https://11.111.111.11/api/login"]; 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
[request setPostValue:[AccountSettings getCompany] forKey:@"companyName"]; 
[request setPostValue:[AccountSettings getEmail] forKey:@"email"]; 
[request setPostValue:[AccountSettings getPassword] forKey:@"password"]; 
[request setRequestMethod:@"POST"]; 
[request setTimeOutSeconds:10]; 
[request setValidatesSecureCertificate:NO]; 
[request setDelegate:self]; 
[request startAsynchronous]; 
} 

- (void)requestFinished:(ASIHTTPRequest *)request{ 
[self hideServerConnectView]; 
int status = [request responseStatusCode]; 
NSLog(@"%i", status); 
if ([self.nibName isEqualToString:@"RootViewController"]) { 
    if (status == 200) { 
     //some code 
    } 
    } 
} 
- (void)hideServerConnectView { 
    [self.parentViewController dismissModalViewControllerAnimated:NO]; 
} 

,並沒有解決我的問題。 任何想法?或者有什麼不對?

回答

2

你在混合異步和同步方法。

您將請求設置爲異步請求,但後來調用[request startSynchronous];

因此,委託方法將不會被調用,您的模態不會被解僱。

的解決方法是斷火請求異步,使用[request startAsynchronous];

這也意味着,你不需要調用performSelectorInBackground(或設置在loginServerRequest方法自動釋放池)。

0

在你的異步版本,移動[self hideServerConnectView];剛過[self loginServerRequest];

或使用- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait調用- (void)hideServerConnectView因爲UI更新必須在主線程發生。