Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UI不立即显示微调器_Objective C_Web Services_Nsurlconnection_Spinner - Fatal编程技术网

Objective c UI不立即显示微调器

Objective c UI不立即显示微调器,objective-c,web-services,nsurlconnection,spinner,Objective C,Web Services,Nsurlconnection,Spinner,嘿,我正在创建一个iOS应用程序,要求用户使用用户名和密码登录,然后将此用户名和密码发送到web服务器进行身份验证。当它发送请求并等待响应时,我显示一个UIActivityIndicator 我遇到的问题是,有时UIActivityIndicator会在按下按钮时立即出现,而有时它只会在几秒钟后出现。我猜这是因为操作系统在更新UI之前正在执行另一个线程,但我想知道是否有人确切知道是什么导致了这个延迟,或者只是我的代码出了问题 我的代码如下: -(void)loginPressed:(id)sen

嘿,我正在创建一个iOS应用程序,要求用户使用用户名和密码登录,然后将此用户名和密码发送到web服务器进行身份验证。当它发送请求并等待响应时,我显示一个UIActivityIndicator

我遇到的问题是,有时UIActivityIndicator会在按下按钮时立即出现,而有时它只会在几秒钟后出现。我猜这是因为操作系统在更新UI之前正在执行另一个线程,但我想知道是否有人确切知道是什么导致了这个延迟,或者只是我的代码出了问题

我的代码如下:

-(void)loginPressed:(id)sender{

    if ([self validateForm]) {

        [self startLoadingAnimation:@"CONNECTING"];

        [Session theSession].authenicatedUsername = usernameTextField.text;
        [Session theSession].authenicatedPassword = passwordTextField.text;

        usernameTextField.text = @"";
        passwordTextField.text = @"";

        [self setUserDetailsBasedOnUserID:[Session theSession].authenicatedUsername password:[Session theSession].authenicatedPassword]; 

    }else {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"MISSING_LOGIN_DETAILS_TITLE", @"")
                                                        message:NSLocalizedString(@"MISSING_LOGIN_MESSAGE", @"")
                                                       delegate:self
                                              cancelButtonTitle:NSLocalizedString(@"OK_BUTTON", @"")
                                              otherButtonTitles:nil];

        [alert show];
        [alert release];
    }
}

谢谢

您应该异步进行网络连接,否则,您将阻塞主(UI)线程,同时可能需要很长时间等待服务器响应(您不知道用户的连接类型等)

如果在后台线程中或使用NSURLConnection的异步接口都无法执行此操作,则可以首先让runloop返回以更新UI(启动活动指示器),然后启动任务,如下所示:

- (void)loginPressed:(id)sender {
  [activityIndicator startAnimating];
  [self performSelector:@selector(doLogin) withObject:nil afterDelay:0.0];
}

- (void)doLogin {
  //perform your task here...
}

但正如我所说,这通常是不好的做法…

谢谢,天哪,这似乎正是我所需要的。遗憾的是,我不得不让用户等待,因为应用程序使用的所有数据都必须是最新的,而且我不能在手机上存储任何东西