简体   繁体   中英

working on parent UI from containerview (iOS - objective-c)

I have a method in my main viewcontroller:

- (void)playMusic:(NSString*)songTitle :(NSString*)songArtist :(NSString*)songDuration :(NSString*)songUrl{

    [songPlayer pause];

    songPlayer = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:songUrl]];

    [songPlayer play];

    self.songSlider.maximumValue = 100;

    self.songSlider.value = 0;

}

I call it from a view inside a containerview like this (from a button click)

 [mainController playMusic:songTitle:songArtist :songDuration :songUrl];

songPlayer (it s an AVPlayer) is doing his job well, the song start without problem. But the songSlider (it s an UISlider) is not updated.

If I call the exam same instructions:

self.songSlider.maximumValue = 100;

self.songSlider.value = 0;

inside viewDidLoad method of my main cotroller, songSlider is updated without problem.

That makes me think the problem is that I call the function from the containerview? in this case how can I fix this? Thank you

I answered a similar question about an hour ago.

I suspect the problem to be that your button click action is not being called on the main thread. I didn't find any resources to indicate if UIControl always fires events on the main thread and thus there is a possibility that your code is running on a background thread. This is important because you cannot make UI modifications on any other thread than the main thread.

Try the following in your button callback

dispatch_sync(dispatch_get_main_queue(), ^{
     [mainController playMusic:songTitle:songArtist :songDuration :songUrl];
});

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