如何从其他选项卡推送选项卡栏中的其他视图?

发布于 2024-12-22 05:24:15 字数 217 浏览 2 评论 0原文

我有选项卡栏 - 两个选项卡,每个选项卡都带有导航控制器。在第二张卡上,我更改数据库,因此我需要刷新第一张卡上的数据(我知道 viewWillAppear)。但有导航控制器,我可以在下一个视图中看到它。

(不幸的是,我有带有导航栏的选项卡栏,通过 .XIB 静态连接)

如何返回第一个选项卡上导航控制器中的第一个视图(从第二个选项卡)?或者我如何在其上推送其他视图,但不破坏导航控制器?

I have tabbar - two tabs with navigation controller in each. On second card I change database, so i need to refresh data(I know viewWillAppear) on first card. 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)

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风苍溪 2024-12-29 05:24:15

您需要将第一个选项卡栏中的导航控制器弹出到根目录,[firstTabBarViewController.navigationController popToRootViewControllerAnimated:NO];

唯一的问题是您需要引用第一个选项卡视图控制器。

另一种方法(我以前使用过这种方法)是使用本地通知。

在您的第一个视图控制器 viewDidLoad 方法中添加以下行来注册通知,您可以将通知命名为任何您喜欢的名称,例如 DatabaseChangedNotification。

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

然后添加一个名为databaseHasChanged的新(void)方法,每次引发通知时都会调用该方法。您的databaseHasChanged方法应该类似于:

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

然后在您的dealloc方法中,确保使用以下代码取消注册通知:

   [[NSNotificationCenter defaultCenter] removeObserver:self];

上面的代码将设置您的第一个视图控制器来侦听和处理DatabaseChangedNotification。

现在您需要做的就是向第二个视图控制器添加一些代码来更改数据库。数据库更改后,只需使用以下代码触发 DatabaseChangedNotification:

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

You need to pop the navigation controller in your first tab bar to 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.

[[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. Your databaseHasChanged method should look something like:

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

Then in your dealloc method make sure you unregister the notification using the following code:

   [[NSNotificationCenter defaultCenter] removeObserver:self];

The above code will setup your first view controller to listen and handle the 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:

[[NSNotificationCenter defaultCenter] postNotificationName:@"DatabaseChangedNotification"
                                                    object:nil];
花开半夏魅人心 2024-12-29 05:24:15

在第一个选项卡的 viewWillAppear 中,您需要弹出到根视图控制器:

[self.navigationController popToRootViewControllerAnimated:YES];

您无法从第二个选项卡执行此操作。但是,您可以在某处设置一个标志,例如在 nsuserdefaults 或数据库中以及在 viewWillAppear(在第一个选项卡中)中检查此标志以查看是否需要弹出到根视图控制器。

In your viewWillAppear of your first tab you'll want to pop to the root viewcontroller:

[self.navigationController popToRootViewControllerAnimated:YES];

You can't do this from your second tab. You could however, set a flag somewhere e.g. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文