简体   繁体   English

如何从主屏幕重新打开应用程序时刷新标签栏内容? (iOS / Xcode)

[英]How to refresh tab bar content upon reopening the app from home screen? (iOS / Xcode)

for all of my view controller.m I have the majority of my code in: 对于我的所有视图controller.m我的大部分代码都在:

- (void)viewDidAppear:(BOOL)animated

So, each time I switch between tab bars all of the info in each view refreshes. 因此,每次我在标签栏之间切换时,每个视图中的所有信息都会刷新。 (which is good!) Although, when I open the app from the home screen the tab won't update...I have to switch to another tab and back again to get it to load. (这很好!)虽然,当我从主屏幕打开应用程序时,选项卡不会更新...我必须切换到另一个选项卡,然后再返回以加载它。

Any solutions? 有解决方案吗

You need to sign up for a notifications to handle it. 您需要注册通知才能处理它。 Register each tab for the notification and a method to handle it. 注册通知的每个选项卡以及处理它的方法。 Then just perform your viewDidAppear. 然后只需执行viewDidAppear。 It works like a charm. 它就像一个魅力。

-(void)viewDidLoad {

   [super viewDidLoad];

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

-(void)becomeActive:(NSNotification *)notification {
    // only respond if the selected tab is our current tab
    if (self.tabBarController.selectedIndex == 0) { // Tab 1 is 0 index, Tab 2 is 1, etc 
        [self viewDidAppear:YES];
    }

}

Maybe you can invoke all your refreshing code in 也许你可以调用所有刷新的代码

(void)applicationDidBecomeActive:(UIApplication *)application
{
}

this method is in your AppDelegate. 这个方法在你的AppDelegate中。

Try putting this in the view controller's m file: 尝试将其放在视图控制器的m文件中:

- (IBAction)done:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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