1

在我的应用程序中,数据来自网络服务,并且我已经实现了代码来显示活动指示器。我在 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];
}

当我设置断点进行复制时,指示器再次显示,但是当删除断点时,指示器不显示。

我已经在 .h 文件和项目中分别导入了用于加载视图和 tickview 的文件。

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

请帮我。

4

4 回答 4

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];

我希望这对你有帮助..

:)

于 2012-10-05T10:00:57.393 回答
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];
        }       
}
于 2012-10-05T09:37:45.300 回答
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];
}
于 2012-10-05T09:41:43.190 回答
0

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

于 2012-10-05T09:48:54.350 回答