简体   繁体   English

如何从其他选项卡中在tabbar中推送其他视图?

[英]How to push other view in tabbar from other tab?

I have tabbar - two tabs with navigation controller in each. 我有tabbar - 两个选项卡,每个选项卡都有导航控制器。 On second card I change database, so i need to refresh data(I know viewWillAppear) on first card. 我在第二张卡上更改了数据库,所以我需要在第一张卡上刷新数据(我知道viewWillAppear)。 But there is navigation controller and i can be on next view of it. 但是有导航控制器,我可以在它的下一个视图。

(unfortunatelly, I have tabbar with navigation bar, connected static - by .XIB) (不幸的是,我有带导航栏的tabbar,连接静态 - 通过.XIB)

How I can return to first view in navigation controller on first tab (from second tab) ? 如何在第一个选项卡(从第二个选项卡)返回导航控制器中的第一个视图? Or how I can push other view on it, but don' t break navigation controller? 或者我如何推动其他观点,但不要破坏导航控制器?

In your viewWillAppear of your first tab you'll want to pop to the root viewcontroller: 在第一个选项卡的viewWillAppear中,您将要弹出到根视图控制器:

[self.navigationController popToRootViewControllerAnimated:YES];

You can't do this from your second tab. 您无法从第二个标签中执行此操作。 You could however, set a flag somewhere eg in nsuserdefaults or in your database and in your viewWillAppear (in your first tab) check this flag to see if you need to pop to the root viewcontroller or not. 但是,你可以在某个地方设置一个标志,例如在nsuserdefaults或你的数据库中,在你的viewWillAppear中(在你的第一个标签中)检查这个标志,看你是否需要弹出到根视图控制器。

You need to pop the navigation controller in your first tab bar to root, [firstTabBarViewController.navigationController popToRootViewControllerAnimated:NO]; 你需要将第一个标签栏中的导航控制器弹出到root,[firstTabBarViewController.navigationController popToRootViewControllerAnimated:NO];

Only problem with this is you need to have a reference to your first tabs view controllers. 唯一的问题是您需要引用第一个选项卡视图控制器。

Another way you could do it (And i have used this method before) is to use local notifications. 你可以做的另一种方式(我之前使用过这种方法)是使用本地通知。

In your first view controllers viewDidLoad method add the following line to register for a notification, you can name the notification anything you like maybe something like DatabaseChangedNotification. 在您的第一个视图控制器viewDidLoad方法中添加以下行来注册通知,您可以将通知命名为您喜欢的任何名称,例如DatabaseChangedNotification。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(databaeHasChanged:) name:@"DatabaseChangedNotification" object:nil]; 

Then add a new (void) method called databaseHasChanged, this method will be called each time the notification is raised. 然后添加一个名为databaseHasChanged的新(void)方法,每次引发通知时都会调用此方法。 Your databaseHasChanged method should look something like: 您的databaseHasChanged方法应该类似于:

-(void)databaseHasChanged
{
    [self.navigationController popToRootViewControllerAnimated:NO];
}

Then in your dealloc method make sure you unregister the notification using the following code: 然后在dealloc方法中确保使用以下代码取消注册通知:

   [[NSNotificationCenter defaultCenter] removeObserver:self];

The above code will setup your first view controller to listen and handle the DatabaseChangedNotification. 上面的代码将设置您的第一个视图控制器来监听和处理DatabaseChangedNotification。

Now all you need to do is add some code to your second view controller which changes the database. 现在您需要做的就是向第二个视图控制器添加一些代码来更改数据库。 After the database has changed just fire the DatabaseChangedNotification using the following code: 数据库更改后,只需使用以下代码触发DatabaseChangedNotification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"DatabaseChangedNotification"
                                                    object:nil];

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

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