简体   繁体   中英

UIProgressView is not updating in iOS

In my project am downloading data from server, downloading code is in App Delegate.m file and once am passing downloading status(in bytes) to NSNotification object. In my ViewController am trying to update UIProgressView using the above downloading status. When i log Downlaoding status (bytes downloaded) am getting correct value. But when i tried to show that in UIProgressView and its not showing anything.

Code

App delegate.m

 ViewController *viewC=[[ViewController alloc]init];
[viewC postNotificationWithValue:bytesDownloaded:totalSize];

In ViewController.m

- (void)viewDidLoad
{
    _progreeView.progress = 0.0; //UIProgressView

    NSString *notificationName = @"MTPostNotificationTut";

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(useNotificationWithString:)
     name:notificationName
     object:nil];
}





- (void)postNotificationWithString:(NSUInteger )current:(NSUInteger )total {


   //   NSLog(@"%f",(double)current/(double)total);
    float status=(double)current/(double)total;
    _downloadStatus.text=[NSString stringWithFormat:@"%f",status];

[_progreeView  setProgress:status animated:YES];
}

and my UIProgressbar is not updating at all. Pls help me

Have u check if the _progreeView isn't nil ?! Why not load the ViewController from nib :

NSArray *viewArray = [[NSBundle mainBundle] loadNibNamed:@"ViewController"
                                                   owner:self 
                                                  options:nil];

ViewController *vc = (ViewController*)[viewArray objectAtIndex:0];

Anyway, Without go too deep with your code, I think that the problem is because that u don't update the UI from the main thread. Make sure that u updating:

[_progreeView  setProgress:status animated:YES];

From the MAIN thread.

Any UI changes must be on the main thread - Remember that!

So u can wrap that like:

dispatch_async(dispatch_get_main_queue(), ^{
  [_progreeView  setProgress:status animated:YES];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM