简体   繁体   中英

How to push other view in tabbar from other tab?

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?

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 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.

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];

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