当导航到另一个页面时需要添加 activity 指示器

when Navigate the another page Need to add activity indicator

#import "LoginVC.h"
#import "HOMEVC.h"
#import "DashboardVC.h"
@interface LoginVC ()

@end

@implementation LoginVC
UIActivityIndicatorView *spinner ;
-(IBAction)login:(id)sender{

    NSString *userUpdate =[NSString stringWithFormat:@"%@",[Usernamefileld text]];
    NSString *userUpdate1 =[NSString stringWithFormat:@"%@",[PasswordField text]];
    NSString *baseURL = [NSString stringWithFormat:@"http://192.168.1.200:8094/YazakiService.svc/LOGIN/%@/%@",userUpdate,userUpdate1];
    NSURL *url = [NSURL URLWithString:[baseURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLResponse *response;
    NSError *error;
    NSData *responseData =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSMutableArray *serviceResponse=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"got response==%@", serviceResponse);
    NSDictionary *template=[serviceResponse objectAtIndex:0];

    NSString *test=[template objectForKey:@"ValidState"];
    // NSString *test1=[template objectForKey:@"Userid"];
    NSString *helloString = @"1";
    //   //
    //   NSString *helloString1 =@"LFY430";
    if ([test isEqual:helloString]) {
        [NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];
        [self moveToView];

        //        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Login Successfully" message:@"Correct Uername/Password" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    }
    else{

        UIAlertView *alert2=[[UIAlertView alloc]initWithTitle:@"Login Failed" message:@"Incorrect Uername/Password" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];

        [alert2 show];
        [self alertView1:alert2 didDismissWithButtonIndex:alert2];

    }

}

- (void)viewDidLoad {

    [super viewDidLoad];
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(150, 225, 20, 30)];
    [spinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    spinner.color = [UIColor blackColor];
    [self.view addSubview:spinner];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)moveToView{

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];


    DashboardVC *initView =  (DashboardVC*)[storyboard instantiateViewControllerWithIdentifier:@"Dashboardvc"];

    [initView setModalPresentationStyle:UIModalPresentationFullScreen];

    [spinner stopAnimating];
    [self presentViewController:initView animated:NO completion:nil];
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self moveToView];
}

- (void) alertView1:(UIAlertView *)alertView1 didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LoginVC *initView =  (LoginVC*)[storyboard instantiateViewControllerWithIdentifier:@"loginvc"];
    [initView setModalPresentationStyle:UIModalPresentationFullScreen];
    [self presentViewController:initView animated:NO completion:nil];
}
-(void)threadStartAnimating:(id)data
{
    [spinner startAnimating];
}


@end

在这里我初始化了指示器并启动和停止微调器但它不适合我...... 任何人都可以帮助我解决如何在导航另一个视图

时添加 activity 指示符的问题

提前致谢

当你停止时UIActivityIndicatorView然后也隐藏它。

[spinner setHidesWhenStopped:YES];

编辑: 如果你不确定 NSThread 那么就这样做吧。

if ([test isEqual:helloString]) {
    //[NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];
    [spinner startAnimating];
    [self moveToView];

}

当你想停止它时,十

[spinner stopAnimating];
[spinner setHidesWhenStopped:YES];
[self.view addSubview:spinner];

在这一行,您将 activity 指标添加到当前 ViewController 视图。

但是一旦您导航到其他视图

DashboardVC *initView =  (DashboardVC*)[storyboard instantiateViewControllerWithIdentifier:@"Dashboardvc"];
[self presentViewController:initView animated:NO completion:nil];

当前视图更改为新的 ViewController 视图。 activity 指标不存在于该视图中,因此您不会在那里看到 activity 指标。

要解决,您应该在第二个ViewController的viewDidLoad方法中再次添加activity指标 在仪表板中 VC

- (void)viewDidLoad {

    [super viewDidLoad];
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(150, 225, 20, 30)];
    [spinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    spinner.color = [UIColor blackColor];
    [self.view addSubview:spinner];
}

A better solution would be to use third party progress huds which make the loading indicator part absolutely easy like MBProgressHUD

您应该在 navigation controller 上添加 activity 指示器,这样,当您导航时它会保持在前面。

你应该使用 MBProgressHud 很棒的第三方库。

当你想显示activity指示器然后添加HUD(activity指示器)时,只需将class放入你的项目并在你的class中导入.h文件喜欢,

 [MBProgressHUD showHUDAddedTo:self.view animated:YES];
 // in your case show hud on `self.navigationController.view`
 // use main thread to show if required

然后隐藏起来,

 dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do something...
dispatch_async(dispatch_get_main_queue(), ^{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
});
 });

希望这会有所帮助:)

不使用指标,而是使用 svprogresshud.Check 这个 link https://github.com/SVProgressHUD/SVProgressHUD。 如果你想要动画进度条检查这个 link https://github.com/cemolcay/GiFHUD