I ran into an issue similar to this earlier on the iPhone, but the same solution doesn't seem to apply to the iPad environment. My goal is to call my method forceReload, defined in my RootViewController, that will reload the countdown_table's data in the RootView from the DetailView once a modal is dismissed. forceReload works fine when called directly from the RootViewController, but I can't seem to point to point to the RootViewController from the DetailView.
I've tried using
RootViewController *root_view = [self.splitViewController.viewControllers objectAtIndex:0];
[root_view forceReload];
[root_view.countdown_table reloadData];
But that only points to the Navigation Controller, not the View Controller inside of it. Even when the modal is dismissed, RootViewController's viewWillAppear does not fire.
How can I point to this file?
Thanks!
Try using notifications, i.e., register RootViewController as an observer for notifications that your DetailView or any view may send.
in RootViewController's viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(forceReload)
name:#"reloadRequest"
object:nil];
viewDidUnload or in dealloc:
[[NSNotificationCenter defaultCenter] removeObserver:self];
And in your DetailViewController or your modal view (you didn't say they're the same), put this right before you dismiss the view or exactly when you need RootViewController to call forceReload:
NSNotification *notif = [NSNotification notificationWithName:#"reloadRequest" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notif];
Related
I have a view controller that is modally presented, and I add it as an observer for a notification. After I dismiss the view controller, it keeps responding to the notification. Is this normal? If so, what should be done?
You should unregister the view controller from notification centre.
The good way to do that is do the register to notification in viewDidAppear method and unregister in viewDidDisappear.
Seems like you forget to remove observer after dismissing and your view controller is retained somewhere:
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
In my applicationWillEnterForeground, I check and send a notification if data refresh is necessary:
[[NSNotificationCenter defaultCenter] postNotificationName:#"refreshModelNotification" object:nil];
The only observer for that particular notification is a particular view controller:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(refreshData:) name:#"refreshModelNotification" object:nil];
That view controller is one of several inside a UITabBarController.
My question is: what happens if that view controller isn't the active tab when the notification is sent?
Thanks in advance.
If the observer is still set for the view controller the view controller will still receive the notification and behave normally except any visual changes to the view controller's view will not be seen
I'm wondering if there is any way to return from UIViewController like 3-4 step backward, I've a main screen which will navigate to other UIViewController via presentModalViewController, on the next view, it will have a UINavigationBar which will navigate to a 4-5 level deeps. i wanna to put a button that let the user go back to the home directly without returning for all the view he enter.
thx in advance.
Have your root level view controller register as an observer of a notification, such as "POP_TO_ROOT". When it receives this notification, call a method to dismiss your modal view controller (or whatever is first on the stack).
In your viewcontroller stack, any of the views 4 or 5 levels in can just post a notification "POP_TO_ROOT".
EDIT: add code
In your main "screen" before you call presentModalViewController, do this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handlePopToRoot)
name:#"POP_TO_ROOT"
object:nil];
and add this method:
- (void) handlePopToRoot {
[[NotificationCenter defaultCenter] removeObserver:self
name:#"POP_TO_ROOT"
object:nil];
[self.navigationController dismissModalViewControllerAnimated: YES];
}
Then, down deep in your viewcontroller hierarchy, when you want to pop all the way out,
you just need to post a notification:
[[NSNotificationCenter defaultCenter] postNotification:#"POP_TO_ROOT" object:nil];
If I understand your question correctly, you are presenting a navigation controller (with a root view controller attached to it) from your "main view controller" modally, and you want to be able to get back to your "main view controller".
Because you will always have pointer to your navigation controller, you should be able to call
dismissModalViewControllerAnimated: from any of your view controllers and it will take you right back to the main view controller.
[[self.navigationController parentViewController] dismissModalViewControllerAnimated:YES]
Save your root View Controller in some property and call:
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
I have a UIPopoverController named popover in another view. What I would like to know is, how can I dismiss the popover if a UIButton was pressed in the current popover view? Thanks in advance.
I always found it odd that a UIViewController knows how big it should be in a popover through it's "contentSizeForViewInPopover" property, but doesn't keep a pointer to the UIPopoverController itself. I always end up adding:
#property (nonatomic,assign) UIPopoverController* popover;
to my UIViewController classes, and set that when creating the popover. Then from anything in that UIViewController, I can do this to dismiss the popover:
[popover dismissPopoverAnimated:YES];
You could use an NSNotification to tell the other view to dismiss it's popover view.
Example usage:
// Add an observer that will respond to our notification.
[[NSNotificationCenter defaultCenter] addObserver:self // <- This is the object that will has the selector that we want to run (the same one we use in the next line).
selector:#selector(doSomething:) // <- This is the selector we want to run.
name:#"doSomethingNow" // <- This is notification name we will send to activate our observer's selector.
object:nil]; // Don't worry about this for now.
// Post the notification. This has the same name as our observer above, so our 'doSomething' selector should be run.
[[NSNotificationCenter defaultCenter] postNotificationName:#"doSomethingNow" object:nil];
// the function specified in the same class where we defined the addObserver
- (void)doSomething:(NSNotification *)pNotification {
NSLog(#"Received Notification...");
}
My navigation Controller 's Root viewController is UIViewController(rootViewController).
It contain's a UITableView, as follows:
alt text http://www.freeimagehosting.net/uploads/c9b0985b63.png
When i press the arrow button(call the function in rootViewController), the tableview will move to left until only the arrow button left on the left edge.
But now i also want to press the cell to make the same function. But the problem is the method didSelectRowAtIndexPath: cant call that function in rootViewController. So how should make this ?
I guess use addObserver method, but know nothing about this , anyone can explain or give me some idea , thanks a lot!
Two ways:
1.Make the "Map" view controller is the delegate of navigation view controller. Then in
didSelectRowAtIndexPath:
call the [delegate buttonPressed];
2. When the button was pressed first time, add a observer:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(buttonPressed:) name:#"flip_the_navigation_controller" object:nil];
Then when the cell was pressed, post a notificatoin:
[[NSNotificationCenter defaultCenter] postNotificationName:#"flip_the_navigation_controller" object:nil];
And remove the observer in dealloc method:
[[NSNotificationCenter defaultCenter] removeObserver:self];