使用ASIHTTPRequest隐藏modalviewcontroller的问题

时间:2011-08-02 21:23:01

标签: iphone objective-c ipad asihttprequest

当我使用ASIHttpRequest连接到服务器时,我遇到了隐藏modalviewcontroller的问题。 我连接后台线程并在主线程中显示modalview。 这是我的代码:

    [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 个答案:

答案 0 :(得分:2)

您正在混合使用异步和同步方法。

您将请求设置为异步请求,但随后调用[request startSynchronous];

因此,不会调用委托方法,也不会解除你的模态。

修复方法是使用[request startAsynchronous];

触发请求异步

这也意味着您无需调用performSelectorInBackground(或使用loginServerRequest方法设置自动释放池)。

答案 1 :(得分:0)

在异步版本中,在[self hideServerConnectView];

之后移动[self loginServerRequest];

或者使用- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait来调用- (void)hideServerConnectView,因为UI更新必须在主线程上进行。