UIactivityindicator xcode ios

时间:2013-01-23 10:05:00

标签: ios xcode processing

我正在尝试UIActivityIndicator使用此link(http://www.cocoacontrols.com/platforms/ios/controls/mbprogresshud),但这里存在问题。我必须显示一个指示器在几秒钟内保持相同的ViewController然后它必须导航但我的代码表现就像我以前点击按钮它只是导航和显示指示器。我们可以停止导航几秒钟直到指示器过程完成吗?

- (void)myProgressTask {
    // This just increases the progress indicator in a loop
    float progress = 0.0f;
    while (progress < 1.0f) {
        progress += 0.01f;
        HUD.progress = progress;
        usleep(50000);
    }
}
-(IBAction)loginActionforIphone:(id)sender
{

    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:HUD];

    // Set the hud to display with a color
    HUD.color = [UIColor colorWithRed:0.23 green:0.50 blue:0.82 alpha:0.90];

    HUD.delegate = self;
    [HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];


    if([loginText_ipad.text length]<=0||[passwordText_ipad.text length]<=0)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Please enter all the fields" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release]; 
    }
    else
    {
        if(([loginText_ipad.text isEqualToString:@"21770765"]&&[passwordText_ipad.text isEqualToString:@"21770765"])||([loginText_ipad.text isEqualToString:@"21770766"]&&[passwordText_ipad.text isEqualToString:@"21770766"]))
        {


            HPEBDashboardViewController_iphone *dashboardView = [[HPEBDashboardViewController_iphone alloc] initWithNibName:@"HPEBDashboardViewController_iphone" bundle:nil];
            if([loginText_ipad.text isEqualToString:@"21770765"])
            {
                dashboardView.buttonvalue=1;
            }
            else
            {
                dashboardView.buttonvalue=2;
            }
            [self.navigationController pushViewController:dashboardView animated:YES];
        }
        else
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Invalid credentials" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }


    }

   }

2 个答案:

答案 0 :(得分:1)

- (void)myProgressTask {
    // This just increases the progress indicator in a loop
    float progress = 0.0f;
    while (progress < 1.0f) {
        progress += 0.01f;
        HUD.progress = progress;
        usleep(50000);
    }
}
-(IBAction)loginActionforIphone:(id)sender
{

    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:HUD];

    // Set the hud to display with a color
    HUD.color = [UIColor colorWithRed:0.23 green:0.50 blue:0.82 alpha:0.90];

    HUD.delegate = self;
    [HUD showAnimated:YES whileExecutingBlock:^{
    [self myProgressTask];
} completionBlock:^{
    if([loginText_ipad.text length]<=0||[passwordText_ipad.text length]<=0)
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Please enter all the fields" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    [alert release]; 
}
else
{
    if(([loginText_ipad.text isEqualToString:@"21770765"]&&[passwordText_ipad.text isEqualToString:@"21770765"])||([loginText_ipad.text isEqualToString:@"21770766"]&&[passwordText_ipad.text isEqualToString:@"21770766"]))
    {


        HPEBDashboardViewController_iphone *dashboardView = [[HPEBDashboardViewController_iphone alloc] initWithNibName:@"HPEBDashboardViewController_iphone" bundle:nil];
        if([loginText_ipad.text isEqualToString:@"21770765"])
        {
            dashboardView.buttonvalue=1;
        }
        else
        {
            dashboardView.buttonvalue=2;
        }
        [self.navigationController pushViewController:dashboardView animated:YES];
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Invalid credentials" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }

   }

答案 1 :(得分:0)

像这样改变你的方法:

-(IBAction)loginActionforIphone:(id)sender
{

    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:HUD];

    // Set the hud to display with a color
    HUD.color = [UIColor colorWithRed:0.23 green:0.50 blue:0.82 alpha:0.90];

    HUD.delegate = self;
    [HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];
}


- (void)myProgressTask {
    // This just increases the progress indicator in a loop
    float progress = 0.0f;
    while (progress < 1.0f) {
        progress += 0.01f;
        HUD.progress = progress;
        usleep(50000);
    }
   [self navigate];
}

添加一个新方法,如:

-(void)navigate
{
   if([loginText_ipad.text length]<=0||[passwordText_ipad.text length]<=0)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Please enter all the fields" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release]; 
    }
    else
    {
        if(([loginText_ipad.text isEqualToString:@"21770765"]&&[passwordText_ipad.text isEqualToString:@"21770765"])||([loginText_ipad.text isEqualToString:@"21770766"]&&[passwordText_ipad.text isEqualToString:@"21770766"]))
        {


            HPEBDashboardViewController_iphone *dashboardView = [[HPEBDashboardViewController_iphone alloc] initWithNibName:@"HPEBDashboardViewController_iphone" bundle:nil];
            if([loginText_ipad.text isEqualToString:@"21770765"])
            {
                dashboardView.buttonvalue=1;
            }
            else
            {
                dashboardView.buttonvalue=2;
            }
            [self.navigationController pushViewController:dashboardView animated:YES];
        }
        else
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Invalid credentials" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }


    }
}