iphone - 在用户单击后立即显示事件指示器代替登录按钮

标签 iphone ios objective-c xcode

我想在我的应用程序检查用户身份验证时显示一个事件指示器来代替我的右 UIBarButtonItem。

当前发生的情况是,用户单击登录按钮,然后 UI 暂停并等待网络调用返回。建议我使用调度队列在不同的线程上运行它,但我不确定如何组合这两段代码:

调度队列:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^ {
//show your indicator button
dispatch_async(dispatch_get_main_queue(), ^ {
    //stop indicator when user has logged in
});
});

登录方式:

- (void) loginAction{

UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] 
initWithFrame:CGRectMake(0, 0, 20, 20)];
[activityIndicator startAnimating];
UIBarButtonItem *activityItem = [[UIBarButtonItem alloc] 
initWithCustomView:activityIndicator];
self.navigationItem.rightBarButtonItem = activityItem;

if ([userNameTextField.text isEqualToString:@""] || [passwordTextField.text 
isEqualToString:@""]) {

UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Login" 
style:UIBarButtonItemStyleBordered target:self action:@selector(loginAction)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor colorWithRed:44.0/255.0 
green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Login" message:@"" 
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
    return;
}

if (loginSuccess== true) {

    //continue
}

}else
{
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Login" 
style:UIBarButtonItemStyleBordered target:self action:@selector(loginAction)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor colorWithRed:44.0/255.0 
green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Login" message:@"" 
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;

}
}

最佳答案

我遇到过同样的问题。我已经这样修好了。在 viewdidload 中,我将事件指示器添加为 subview ,并在单击登录按钮时开始为指示器设置动画。然后使用 performSelector 我调用了一个包含登录服务调用的方法,最后我只是停止了指示器。它对我来说很好用。使用此代码..

.h UIActivityIndi​​catorView *activityIndi​​cator;

以.m为单位

- (void)viewDidLoad
{

    [super viewDidLoad];

    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [activityIndicator setFrame:CGRectMake(0, 0, 37, 37)];
    [activityIndicator setCenter:self.view.center];
    [activityIndicator setHidesWhenStopped:YES];
    [self.view addSubview:activityIndicator];
    [self setDisplayStatusOfControls];

}


  -(IBAction)loginButton
  {

     [activityIndicator startAnimating];
     [self performSelector:@selector(Loginauthentication) withObject:nil afterDelay:0.1f];

  }

- (void) loginAction
{
//Log in authentication..



[activityIndicator stopAnimating];
}

关于iphone - 在用户单击后立即显示事件指示器代替登录按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15266645/

相关文章:

ios - 如何使用代码中的标识符触发一系列(自动)segues

ios - 如何从 webView 解析数据以在代码中使用

ios - 蓝牙扫描在后台不工作

ios - 来自 iPhone 设置和 GMT 偏移的日期时间格式

ios - 随机选择、不重复的Sql语句

iphone - iOS - 在特定时间覆盖特定录音

iphone - 在 iPhone 模拟器中运行应用程序时,如何查看调用的每个方法?

iphone - 移动Safari : Disable scrolling pages "out of screen"

ios - NSURLIsExcludedFromBackupKey - 应用程序必须遵循 iOS 数据存储指南,否则将被拒绝

iphone - 如何在 AVAudioPlayer 发布时调试此死锁?