活动指示器未显示

时间:2012-10-05 09:30:04

标签: ios objective-c ios4

在我的应用数据来自网络服务,我已经实现了显示活动指标的代码。 我在viewWillAppear()中从web服务获取数据。

问题是,当第一次调用viewWillAppear()时,指标只显示一次,之后只要调用了viewWillAppear()方法,指示符就不会显示。

我的代码如下: -

-(void)viewDidAppear:(BOOL)animated
{
    //Initializaing  views for Acticity Indicators
   loadingview = [[LoadingView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];    
    Tickview=    [[tickview alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];   
}

-(void)viewWillAppear:(BOOL)animated
{
        Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
        NetworkStatus internetStatus = [r currentReachabilityStatus];
        if ((internetStatus == ReachableViaWiFi) || (internetStatus == ReachableViaWWAN))
        {                 
           [NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];

        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Check Your Internet Connection. Internet   Connection is not active" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }       
}

- (void)startTheBackgroundJob
{
    [self performSelectorOnMainThread:@selector(loading) withObject:nil waitUntilDone:YES];

}

-(void)loading
{
     //
     //some database operations here..
     //


    //after that showing the activity indicator
    [NSThread detachNewThreadSelector:@selector(threadAnimates:) toTarget:self withObject:nil];



    //retriving data from web service..   
    [self retrieveData];


    //than removing loadingview
     [loadingview removeFromSuperview];
     [loadingview setHidden:YES];

    [NSThread detachNewThreadSelector:@selector(tickshow:) toTarget:self withObject:nil];
    [self performSelector:@selector(tickhide:) withObject:nil afterDelay:2]; 

}

 - (void) threadAnimates:(id)data
{
[self.view addSubview:self.loadingview];

[loadingview setHidden:NO];

}

-(void)tickshow:(id)sender
{
    [self.view addSubview:Tickview];
    [loadingview setHidden:NO];
}

-(void)tickhide:(id)sender
{
    [Tickview removeFromSuperview];
    [Tickview setHidden:YES];
}

当我为dubegging设置断点时,指示器再次显示,但是当移除断点时,指示器不显示。

我已经在.h文件和项目中为loadingview和tickview导入了单独的文件。

我已经尝试了很多,但无法得到解决方案。

请帮帮我。

4 个答案:

答案 0 :(得分:4)

试试这段代码

-(void)viewDidAppear:(BOOL)animated
{
    //Initializaing  views for Acticity Indicators
   loadingview = [[LoadingView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];    
    Tickview=    [[tickview alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];   
    [loadingview startAnimating];
   [self.view addSubview:loadingview];

   [self.view bringSubviewToFront: loadingview];
}

已编辑:

还可以尝试使用以下代码来添加视图中的活动指示器

        UIActivityIndicatorView *spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(10.0, 11.0, 25.0, 25.0)];
        [spinningWheel startAnimating];
        spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
        [self.view addSubview:spinningWheel];
        [spinningWheel release];

我希望这可以帮到你..

:)

答案 1 :(得分:0)

[self.ActiveIndicatorName startAnimating];方法中只写ViewWillAppear语句。

-(void)viewWillAppear:(BOOL)animated
{
      [self.ActiveIndicatorName startAnimating];

        Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
        NetworkStatus internetStatus = [r currentReachabilityStatus];
        if ((internetStatus == ReachableViaWiFi) || (internetStatus == ReachableViaWWAN))
        {                 
           [NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];

        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Check Your Internet Connection. Internet   Connection is not active" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }       
}

答案 2 :(得分:0)

像这样修改ViewWillAppear方法....

-(void)viewWillAppear:(BOOL)animated
{
//Initializaing  views for Acticity Indicators
loadingview = [[LoadingView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];    
Tickview=    [[tickview alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];  
[Tickview startAnimating];
}

答案 3 :(得分:0)

一旦加载请求,就应该开始加载指标[myActivityIndi​​cator startAnimating];并使用委托来阻止它。如果在nib文件中创建活动指示器,请不要忘记在停止时将其隐藏(停止时隐藏)。希望这会有所帮助。