开发者

iOS - Reload UIView when tab icon clicked?

开发者 https://www.devze.com 2023-01-31 17:46 出处:网络
I have an application that uses a 开发者_开发问答UITabBarController, and inside each tab I have Navigation Controllers.

I have an application that uses a 开发者_开发问答UITabBarController, and inside each tab I have Navigation Controllers.

I would like to make one of the views reload itself after an action happens in another view, so that when the user goes back to that view - it's updated.

Is it possible?


The simplest way to accomplish this is to put your loading code into viewWillAppear: instead of viewDidLoad. While viewDidLoad may only get called once, when your view is initialized, viewWillAppear: is called any time your view controller is about to be presented, whether it be in the tab bar, in a navigation controller, etc., which gives you a nice hook to implement loading and layout functionality.


Thanks. that worked perfectly. For anyone reading this here is my code. It works but be warned, I'm new to iOS and objective-C so caveat emptor.

- (void)viewWillAppear:(BOOL) animated
{
    NSURL *imageURL = [NSURL URLWithString:@"http://www.domain.com/image.jpg"];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
        dispatch_async(dispatch_get_main_queue(), ^{
            // Update the UI
            self.img.image = [UIImage imageWithData:imageData];
        });
    });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消