Dismiss a UIPopoverController from another view - objective-c

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...");
}

Related

Collapsing A Collection View Cell

I have a scroll view, inside a collection view cell. The scroll view has a class of its own (ClassA), as does the collection view (ClassB). When you tap the index row, it expands. Sweet, works just fine. Only problem is, you have to tap on the index row to collapse the cell. Since there is a UIScrollView hanging out in the cell, tapping on it won't collapse the cell. So, what I did was create tap detection in the scroll view. The tap detections selector then handles collapsing the cell via notifications:
Class A:
- (void)singleTap:(UITapGestureRecognizer *)gesture {
// Post a notification to collapse
[[NSNotificationCenter defaultCenter] postNotificationName:#"collapseCell" object:nil];
}
Class B:
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(deselectItemAtIndexPath:animated:)
name:#"collapseCell" object:nil];
}
Error: 'NSInvalidArgumentException', reason: '-[CollectionViewController deselectItemAtIndexPath:animated:]: unrecognized selector sent to instance 0x7caa
That NSNotificationis not doing what you think it's doing. Its not calling or trying to call the UICollectionView's delegate method, deselectItemAtIndexPath:animated:. Its actually looking for a different method, but with the same signature as the UICollectionViews delegate method.
Make a test method, call it something simple and see if that works to confirm what I think is happening.

return to the main menu from any UIViewcontroller

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

reloadData in MasterView from DetailView

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

Reloading NSTableView on re-focus

In my application, I have a NSWindow that has a NSTableView object and a few buttons. When the user presses the "new" button, an "ItemAdd" NSWindowController is activated where the user types in attributes for the item to be added to the NSTableView.
My question is this: since NSTableView requires reloadDatato update its view, how do I call reloadData after the ItemAdd window closes and focus shifts back to the NSWindow with the NSTableView.
Thanks for the help!
You could put reload data in a notification handler:
Put this in an initialization method of an object that you want the notification to get called on:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didBecomeMainWindow) name:#"NSWindowDidBecomeKeyNotification" object:nil];
Then make a method something like this:
- (void) didBecomeMainWindow
{
[tableView reloadData];
}
You can subclass the NSWindow and override the following method:
- (void)becomeKeyWindow

UITableView Trouble

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