As shown in the picture above, I have two view controllers, one of which is within a navigation controller. When a button is pressed in controller A, controller B is presented through the navigation controller and is displayed modally. Is it possible for me to pass data from controller B back to controller A when the dismiss function is called on B?
You can use delegation pattern or callbacks to do this
If you are using storyboards and navigating using segues, then an unwind segue will do this for you. Here is a simple tutorial that should help:
https://www.andrewcbancroft.com/2015/12/18/working-with-unwind-segues-programmatically-in-swift/
This stack overflow answer has much more detailed and valuable information on this topic
Passing Data between View Controllers
You can pass through NSNotificationCentre .
First you need to add Notification Observer and its selector in ViewControllerA as below :
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveTestNotification:) name:#"notificationName"
object:nil];
-(void) receiveTestNotification:(NSNotification*)notification
{
NSDictionary* userInfo = notification.userInfo;
NSLog (#"%#",userInfo);
}
Now in ViewController B you need to post notification as below :
NSDictionary* userInfo = your data ;
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:#"notificationName" object:self userInfo:userInfo];
Related
Hello I am using Page menu in application and i want to push from didSelectRowAtIndexPath in next view in one view which is involed in page menu
i know all way which we use for push and model.if i am using model with custom navigation bar then page menu hide on dismiss model.
Assumed hierarchy:
UINavigation Controller -- UIViewController -- UIPageController
UIPageController (with 2 UIViewController) -- UITableViewController-1 and UITableViewController-2
On didSelectRowAtIndexPath method
UIPageController -- UITableViewController-1 //Selected any one row
Then just push the next view controller onto the current UITableViewController.
Note - Both controller in UIPageController maintain separate life cycle that is independent from each other.
One can not push the viewcontroller in pageView , PageviewController don't have navigation controller .
Solution :
There are two ways to make this possible suggested by Nitin Gohel
set notification for event and Retrieve the notification on main Controller
You can send the information in Notification required in another viewController.
NSDictionary* userInfo = #{#"abc": #"aaa"};
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:#"One" object:self userInfo:userInfo];
In MainViewController just set the onserver for that notification in viewDidLoad method
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(OneNoti:) name:#"One" object:nil];
}
And perform push from MainViewController:
-(void) OneNoti:(NSNotification*)notification
{
NSDictionary* userInfo = notification.userInfo;
NSNumber* total = (NSNumber*)userInfo[#"total"];
NSLog (#"Successfully received test notification! %i", total.intValue);
NSLog(#"%#",notification.userInfo);
[self performSegueWithIdentifier:#"1_" sender:nil];
}
Set the delegate methods in MainView Controller and call the methods while performing action from page view and push from that Action .
Same like above way .
I have a UITabBarController, which has 4 tabs. Each one of those tabs is a separate UIViewController. I have objects on each one of those 4 VC's that use NSNotification's to perform actions upon the press of a certain object. The 4 VC's all respond to the notification in the same way because it is a similar object on each page. When this object is pressed it presents a view onto the current view controller. The problem is that if I move to any of the other 3 tabs now that view is also on their VC. That is because the notification is being responded to on all 4 tabs when it is pressed on any of the VC's. I am needing it to only respond to the VC that the user is currently on and not any of the others that are in the tab bar.
Is there a way to get this to work properly? Maybe a threshold where you can set how many times the notification can perform its selector after being called? That way I could set it to 1 and at any given time if that notification is called the selector can only be called 1 time.
The type of object implementation that I'm using requires me to use NSNotification's so there is no way to change how I interact.
edit:
This viewDidLoad method is on the top level VC for the 4 VC's in my tab bar. All 4 of them either use this directly or inherit from it.
- (void) viewDidLoad
{
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didSelectItemFromCollectionView:) name:#"didSelectItemFromCollectionView" object:nil];
}
Action Handler:
- (void) didSelectItemFromCollectionView:(NSNotification *)notification
{
NSDictionary *cellData = [notification object];
if (cellData)
{
NewVC *pushToVC = [self.storyboard instantiateViewControllerWithIdentifier:#"PushToVC"];
[self.navigationController pushViewController:pushToVC animated:YES];
}
}
Each of the 4 VC's is a UITableViewController and have cells with an object that can be pressed. This NSNotificationCenter action is what allows the operation to work.
You must have implemented the NSNotificationCenter's -addObserver:selector:name:object: method in the -viewDidLoad of every viewController
Example:
- (void)viewDidLoad
{
//...
[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doSomething:)
name:#"TestNotification"
object:nil];
}
Instead of having this in -viewDidLoad, move it within -viewWillAppear and implement removeObserver:name:object: in -viewWillDisappear.
This way, only the viewController that is currently on will respond to the notification.
Example:
- (void)viewWillAppear:(BOOL)animated
{
//...
[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doSomething:)
name:#"TestNotification"
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
//...
[NSNotificationCenter defaultCenter] removeObserver:self
name:#"TestNotification"
object:nil];
}
- (void)doSomething:(NSNotification *)userInfo
{
//...
//if you push a viewController then the following is all you need
[self.navigationController pushViewController:vcSomething
animated:YES];
//however.... if you're instead presenting a viewController modally then
//you should implement "-removeObserver:name:object: in this method as well
//[NSNotificationCenter defaultCenter] removeObserver:self
// name:#"TestNotification"
// object:nil];
//[self presentViewController:vcSomething
// animated:YES
// completion:nil];
//OR... in the completion parameter as:
//[self presentViewController:vcSomething
// animated:YES
// completion:^{
// [NSNotificationCenter defaultCenter] removeObserver:self
// name:#"TestNotification"
// object:nil];
// }];
}
EDIT:
You (#Jonathan) commented:
I really appreciate your answer and it has helped me out a lot! I
actually ran into one more scenario where this issue occur's and I'm
not sure how to figure it out. Right now I have a VC that presents
another VC modally. Each one has observers for the same
NSNotification. Everything performs perfectly well when I'm in the
modally presented VC, but once I dismiss that VC and return to the
underlying one I have the same issue where the notification is being
called multiple times. Do you have an idea for a solution in this
case?
Now... regarding this...
FIRSTLY... NOTE:
Multiple -addObserver:selector:name:object: will register the specified notification multiple times (means... same notification being registered for N times will call invoke the target selector N times)
Presenting a ViewController (call it Child) from, say, Parent viewController will NOT invoke the -viewWillDisappear: of the Parent
where as...
Dismissing the Child viewController will still invoke -viewWillAppear: of the Parent
This creates an imbalance in the logic and if not handled (as per the commented lines in the code example of the doSomething method above), it results in the Parent registering for the notification multiple times (as it's -viewWillAppear: method is called more often than not -viewWillDisappear:)
also see: similar question
it should be called only once so that it never gets called again
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(methodName:) name:#"name" object:nil];
});
My question is that I have one main UIViewController that allows three other UIViewControllers to be presented through it, but I am wondering if there is a way that once I dismiss one of those other three controllers, can the main UIViewController be notified or tell that it is now appearing due to the dismissal of said controller?
Thank you in advanced!
If your main view controller implements:
(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
and the presented view controllers send it that message, you will know since at that time it can query to see what the "presentedViewController" was.
When you dismiss one of the three ViewControllers, you could signal to the main ViewController that they have been dismissed via a NSNotification:
NSDictionary *viewControllerInfo = #{#"ViewControllerClass" : NSStringFromClass([self class])}
[[NSNotificationCenter defaultCenter] postNotificationName:#"ViewControllerDismissed" object:nil userInfo:viewControllerInfo];
And in your main viewController:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(viewControllerDismissed:) name:#"ViewControllerDismissed" object:nil];
And respond with this method:
- (void)viewControllerDismissed:(NSNotification *)notification {
NSDictionary *viewControllerInfo = [notification userInfo];
// Dictionary should be same as the one passed through the noticiation.
}
Additional note: If you are using a UIStoryboard, then you can use an unwind segue.
EDIT: Updated Dictionary to use NSStringFromClass()
I have 2 viewcontrollers with segue "page curl"
viewcontrollerA => pagecurl => viewcontrollerB
and Now I want to update viewcontrollerA since user make some change at viewcontrollerB.
I tryed:
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"mystoryboard"
bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:#"ExampleViewController"];
[vc ViewDidLoad]; // or ViewWillAppear or ViewDidApear
it works only for the NSLog I put in those functions.
but none of them works with the function which check out Coredata and update the interface.
please help
try this code:
you add parent class
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(actionremovecalender:)
name:#"subMitReport"
object:nil];
-(void)actionremovecalender:(NSNotification *)notification
{
[self ViewDidLoad]
}
call child class
[[NSNotificationCenter defaultCenter]postNotificationName:#"subMitReport" object:nil]
You can send a NSNotification that the parent will receive, or you can set a delegate with a method implemented by the parent view.
In both cases, just reload the view.
In a one-to-one relation, you should prefer the delegation pattern. Just add a weak reference of viewcontrollerA to your viewcontrollerB. You can just call a method (in this case viewDidLoad method) of viewcontrollerA using the reference so you can refresh the views. But I'd prefer declaring a protocol for delegation to prevent tight coupling of two view controllers.
ViewWillApear or ViewDidApear will be called since there is any object changes in the viewcontroller, but if you want to change your viewcontrollerA from another viewcontrollerB, that require NSNotificationCenter to call the function from viewcontrollerA
you can always use NSNotificationCenter to update your parent viewcontroller
At your parent viewcontroller put this:
//since child view controller calls turnItOff, notification center calls function "turnButtonCountinuesOff"
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(turnButtonCountinuesOff) name:#"turnItOff" object:nil];
turnButtonCountinuesOff is your function at parent viewcontroller
At your child viewcontroller put this:
//connect to parent UI view controller calls notification turnItOff.
[[NSNotificationCenter defaultCenter] postNotificationName:#"turnItOff" object:nil];
hope it helps.
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;
}