导航另一页时需要添加活动指示器

时间:2016-04-30 07:01:17

标签: ios objective-c uinavigationcontroller uiactivityindicatorview viewdidload

#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

在这里,我首先显示指标并开始和停止微调器,但它不是为了我... 任何人帮我解决了导航另一个视图时如何添加活动指标的问题

先谢谢

4 个答案:

答案 0 :(得分:1)

[self.view addSubview:spinner];

在此行,您可以将活动指示符添加到当前ViewController视图。

然而,一旦您导航到其他视图

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

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

要解决此问题,您应该再次在第二个ViewController的viewDidLoad方法中添加活动指示符 在Dashboard 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];
}
  

更好的解决方案是使用第三方进度点击   加载指示器部分非常容易,如MBProgressHUD

答案 1 :(得分:0)

当你停止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];

答案 2 :(得分:0)

您应该在navigation controller上添加activityindicator,因此,当您导航时,它会保留在前面。

你应该使用MBProgressHud伟大的第三方图书馆。

只需在您的项目中放入类,并在您想要显示活动指示符然后添加HUD(活动指示符)时在您的班级中导入.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];
});
 });

希望这会有所帮助:)

答案 3 :(得分:0)

而不是使用指标,请使用svprogresshud。检查此链接https://github.com/SVProgressHUD/SVProgressHUD。 如果您需要动画进度条,请检查此链接https://github.com/cemolcay/GiFHUD