I need to call a delegate method on my main view controller ('showDetails:') from a popover view's pushed view (embedded in navigation controller). This is all from a storyboard setup.
The hierarchy is: Main view -> Popover (menu tableview embedded in navigation controller)->Popover secondary View (pushed onto popover navigation controller)
I know how to setup a delegate on the popover using prepareForSegue, but not on an inner view.
How can I call a delegate method on the main view from an inner (pushed) view of a popover?
Here is how I setup the delegate on a popover main view:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segueSearchResults"]) {
//Dismiss User Popover
[self dismissUserPopover];
SearchResultsViewController *vc = segue.destinationViewController;
vc.searchDelegate = self;
self.searchPopover = [(UIStoryboardPopoverSegue *)segue popoverController];
self.searchPopover.delegate = self;
}
}
Instead Delegate i prefer "NSNotificationCenter" in your case
Add an observer to your ViewController for some action in uiview
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveActionNotification:)
name:#"someActionNotification"
object:nil];
Post Notification from your pushed View in PopOverController
Post Notification and method in your Viewcontroller will be called
[[NSNotificationCenter defaultCenter] postNotificationName:#"someActionNotification" object:self];
At the end Dont forget to remove Observer.
[[NSNotificationCenter defaultCenter] removeObserver:#"someActionNotification"];
When you need to communicate between two view controllers which are far apart in the VC hierarchy, trying to reference one from the other so you can directly call methods on it doesn't work so well -- there's several levels of indirection in between, and it's very fragile if you change your VC hierarchy later.
Look into notifications (NSNotificationCenter) instead; you can have one VC "broadcast" info for another to respond to, regardless of where they are in your app.
Related
I have an app that have a login view and then after the login is successes I want to have a view with a side menu.
I am using SWRevealViewController to make the slide menu.
But the problem as I said is the login view will be the first view not the SWRevealViewController.
I tried to do the following inside prepareForSegue method.
SWRevealViewController *revealViewController;
[revealViewController initWithRearViewController: [self.storyboard instantiateViewControllerWithIdentifier:#"MenuTableViewCell"]frontViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"]];
this is not working. That's the only code I have inside prepareForSegue method, I deleted the if statement because I only have one segue inside the loginview so I figured I don't need the IF.
Should I delete the SWRevealViewController from the storyboard? or make the segue from the login view to SWRevealViewController
I don't know what to do.
There's no explanation for situation for using SWRevealViewController inside the views not the first view.
I am building this app for iOS for objective c for iPhone.
Please help.
Thanks.
You should do your storyboard hierarchy like this.
Drag a ViewController and change a class to SWRevealViewController.
Drag a ViewController Embedded in NavigationController. Change class name to LoginViewController.
Change your NavigationController's storyboard ID to "LoginNavigationController".
Assign new custom segue named sw_front from SWRevealViewController to NavigationController and change class of segue to SWRevealViewControllerSegueSetController.
Drag a controller having TableView as a subView which is your MenuViewController for Sidebar.
Assign new custom segue named sw_rear from SWRevealViewController to MenuViewController and change class of segue to SWRevealViewControllerSegueSetController.
Now drag a ViewController named HomeViewController embedded in NavigationController and assign a custom segue with identifier "Home" and class with SWRevealViewControllerSeguePushController.
Change storyboard ID of NavigationController to "HomeNavigationController".
This should be the setup in your storyboard.
Now here is the coding part:
In SWRevealViewController.m
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(userSignedInSuccessfully) name:kUserSignedInNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(userSignOutSuccessfully) name:kUserSignedOutNotification object:nil];
// this flag should be maintained in user defaults
if(iSUserSignedIn){
//Show Home page if user is already signed in
[self showHomeScreen];
}
}
#pragma mark - Show Home screen
-(void)showHomeScreen
{
UINavigationController *navigation = [[UIStoryboard storyboardWithName:kStoryboardName bundle:nil] instantiateViewControllerWithIdentifier:#"HomeNavigationController"];
[self setFrontViewController:navigation];
[self setFrontViewPosition:FrontViewPositionLeft];
}
-(void)showLoginScreen{
UINavigationController *navigation = (UINavigationController *)[[UIStoryboard storyboardWithName:kStoryboardName bundle:nil] instantiateViewControllerWithIdentifier:#"LoginNavigationController"];
[self setFrontViewController:navigation];
[self setFrontViewPosition:FrontViewPositionLeft];
}
Now when user signed first save flag iSUserSignedIn in user defaults and post this notification.
//Post notification for successful sign in
[[NSNotificationCenter defaultCenter] postNotificationName:kUserSignedInNotification object:nil];
When user signed out set flag iSUserSignedIn to nil and post this notification.
//Post notification for successful sign out
[[NSNotificationCenter defaultCenter] postNotificationName:kUserSignedOutNotification object:nil];
There are a lot of questions and answers on Stack Overflow about this but they are just valid for iOS 8 and before.
iOS 9 deprecated a lot of things and the answers on SO did not work anymore.
Said that, I am presenting a popover by performing a segue like this
[self performSegueWithIdentifier:#"myPopover" sender:self];
This segue is created between the current viewController and the viewController used by the popover. There is no button involved. The popover is anchored to a view.
The problem is that on prepareForSegue:identifier
[segue destinationViewController]
is a UIViewController and
[[segue destinationViewController] popoverPresentationController]
is the new UIPopoverPresentationController and this object does not offer a dismiss api anymore.
Instead, we are supposed to use
[self dismissViewControllerAnimated:YES completion:nil];
to dismiss the popover but this has no effect for me.
My situation is this: I have a popover with a text field. I want to dismiss the popover if the user hides the keyboard.
So I did this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
and then
- (void)keyboardWillHide:(NSNotification *)notification {
[self dismissViewControllerAnimated:YES completion:nil];
}
but this has no effect at all.
I have also tried to create an unwind segue inside the popover viewController and call that from the presenting view controller but that makes the app crash.
Just tried it and it all seems to work perfectly.
What is your view controller hierarchy (navigation controllers, etc.)?
Are you properly calling dismissViewControllerAnimated:completion: on the same view controller that presented the popover?
Make sure there isn't a mixup between child view controller and navigation controller.
You can also log the popover view controller's presentingViewController to check.
I need to go back all the way to the view controller that presented the first navigation controller. However I haven't dismissed multiple controllers before at once, and when I've tried doing so, it doesn't work. It just goes to the first navigation controller instead of all the way to the one before it.
Here is my current code:
[(UINavigationController *)self.presentingViewController popViewControllerAnimated:NO];
[self dismissViewControllerAnimated:YES completion:nil];
I have a view controller which modally presents the first navigation controller. The first navigation controller screen is called Main View Controller. It then pushes to Login View Controller. Login View Controller does presentViewController to MenuViewController (UIViewController).
I need to get from MenuViewController all the way back to the view that presented the first navigation controller. Thanks.
Try this
UIViewController *vc = self;
while (vc.presentingViewController != nil) {
vc = vc.presentingViewController;
}
[vc dismissViewControllerAnimated:YES completion:nil];
One option would be to use NSNotifications.
You can add an observer in your first/root/initial UINavigationController subclass e.g.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(logout:)
name:#"LogoutNotification"
object:nil];
Then in your "logout:" method you have direct control over the initial UINavigationController rather than those further up the hierarchy.
You can then send an NSNotification from anywhere in the app in order to trigger the method.
e.g.
[[NSNotificationCenter defaultCenter] postNotificationName:#"LogoutNotification" object:self];
I have a UITabBarController with four tabs. In each of the view controllers presented when a tab is selected I have a reset button. Tapping the button will change the appearance of all the view controllers. In particular, it will change the text of some labels in the different view controllers.
Is there some recommended way to update all the view controllers of a UITabBarController at the same time i.e. to make them reload their views?
My current approach is to make those view controllers conform to a protocol
#protocol XYReloadableViewController
- (void)reloadContents;
#end
and then send the message -reloadContents to all the view controllers when the button is tapped:
- (IBAction)touchUpInsideResetButton {
// ...
NSArray *viewControllers = self.tabBarController.viewControllers;
for (UIViewController<XYReloadableViewController> *viewController in viewControllers) {
[viewController reloadContents];
}
}
Then in each of the view controllers I would have to implement that method:
- (void)reloadContents {
[self.tableView reloadData];
// other updates to UI ...
}
But that seems a little too complicated. So is there an easier way to tell the view controllers to reload their views?
Edit: And what happens if I present a UINavigationController in some of the tabs or a container view controller? I would need to pass the message along the chain of all its child view controllers...
You can create ReloadViewController and all you contrlollers inheritance
from him.
ReloadViewController have property UIButton and methods:
-(void)reloadContents;
-(IBAction)touchUpInsideResetButton:(id)sender;
in .m file:
-(void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadContents)
name:#"MyNotification"
object:nil];
}
- (IBAction)touchUpInsideResetButton:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"MyNotification"
object:nil];
}
in your viewControllers need only override method reloadContents
Notifications sound like a better fit for this. When view controllers need to be reset, broadcast an NSNotification and have any view controllers that might need to reset themselves listen for that notification, and trigger what they need to do. That way it doesn't matter how far down a navigation stack they are.
You might want to defer updates until the view actually appears. You could set a BOOL needsUpdate when the VCs receive the notification, but only do the actual update in viewWillAppear:, to save resources and prevent a large number of updates from going off at once (and perhaps blocking the main thread).
If this behaviour is common to all your view controllers, make a UIViewController subclass to prevent repeating code and have them all inherit from that. Alternatively, (if you're using Apple VC subclasses) make a category on UIViewController to add the notification methods.
I have a viewController I've built in storyboard. I also have a NSObject Subclass which acts as my model, which sends and listens for API requests and responses. When a method fires in my model, I want to present a modal View of my viewController from whatever view happens to be visible at the time.
An example would be if my API hears "show this view" I want to show viewController regardless of what view is being shown.
Conceptually, how does one do this?
EDIT: I don't know which view controller will be showing when I want to present my modal viewController. Also, I need to pass params from my model to the modalVC when it's presented.
I would send a notification from the model telling "someone" that some view needs be displayed.
NSDictionary *userInfo = #{ #"TheViewKey": viewToDisplay];
[[NSNoticationCenter defaultCenter] postNotificationName:#"NotificationThatThisViewNeedsToBeDisplayed" object:self userInfo:userInfo];
And then on the delegate (or the active view controller) would register to this notification and handle the display.
// self is the delegate and/or the view controller that will receive the notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleViewToDisplay:) name:#"NotificationThatThisViewNeedsToBeDisplayed" object:nil];
If you put in the view controller remember to remove self from the observers when the view is not visible:
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"NotificationThatThisViewNeedsToBeDisplayed"];
This way your model is decoupled from the presentation.
You have the current viewController (any viewController subclass) present the new view using:
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
EDIT: To find the top view controller, you ask the UITabBarController for the selectedViewController (if you use a tabBarController) to get the 'seed', or start with the window.rootViewController.
Once you are past any tabBarControllers, then you should only have UIViewController subclasses and UINavigationControllers. You can use a loop like this:
- (UIViewController *)frontmostController:(UIViewController *)seed
{
UIViewController *ret;
if([seed isKindOfClass:[UINavigationController class]]) {
ret = [(UINavigationController *)seed topViewController];
} else
if([seed isKindOfClass:[UIViewController class]]) {
ret = seed.presentedViewController;
}
return ret ? [self frontmostController:ret] : seed;
}