I have got a problem. I need to update data of master view data on segue from detail view. I've tried to implement prepareForSegue method, but it's not called on tapping back button.
What's the way i can do that?
Prepare for segue doesn't get called when popping a view controller from the navigation stack. I'd suggest calling [tableView reloadData] either in the detail view's viewWillDisappear method, or in the master view's viewWillAppear method. Alternatively, if you want to reload your table view after making a change, you can try using notifications. Something like the following in your master view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(refresh:) name:#"RefreshMasterNotification" object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)refresh:(NSNotification *)notification
{
[tableView reloadData];
}
And something like the following whenever you make a change in your detail view controller:
[[NSNotificationCenter defaultCenter] postNotificationName:#"RefreshMasterNotification"];
Related
I have a tab based application and I want to update the information that is displayed in all UIViewControllers reloading them when I click in a button.
I have tried several ways to refesh the view controllers like [view setNeedsDisplay] and [view setNeedsLayout] but without success. I had also iterate all navigation controllers of tab bar and it's view controllers and applied this function calls without no success too.
Can anyone help me with this?
You basically add this to all controllers you want to refresh :
- (instancetype)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(refreshAllViews:) name:#"RefreshAllViews" object:nil];
}
return self;
}
- (void)refreshAllViews:(NSNotification *)notification
{
// Reload all the data and views you want here
// eg. [self.tableView reloadData];
}
- (void)reloadAllViewsAction:(id)sender
{
// Call this from the button to refresh all views
[[NSNotificationCenter defaultCenter] postNotificationName:#"RefreshAllViews" object:nil userInfo:nil];
}
Two options:
(1) Use notification, when user clicks the button, send a notification(Maybe with some informations), so that all the view controllers which are interested in that notification could update itself accordingly;
(2) Store the information in a shared model (Singleton) so it can be accessed by all the view controllers, and for each view controller, update UI in viewWillAppear according to that shared model
I have a table view inside a view controller. When i want to add something to my tableview i press the add-button and i go to a detail view controller where i have a save button. Now I want to go back to my tableviewcontroller and refresh it after pressing my save button.
I'm sending my data to an external database and not in the core data.
Since you are using modalViewController, after you tap Save button, you may dismiss it and to refresh the data, you will need to Use NSNotification in this case.
Try the following code:-
Inside the view controller that contains the tableview,
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(tableReload) name:#"tableReload" object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"tableReload" object:nil];
}
-(void)tableReload{
[self.tableView reloadData];
}
In the second view controller, IBAction for the save button
[[NSNotificationCenter defaultCenter]postNotificationName:#"tableReload" object:nil];
[self dismissViewControllerAnimated:YES completion:nil];
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];
});
I'm working on my embedded table views. They should scroll up when the keyboard hides some textfields. But I have several view controllers in my navigation view controller with this behaviour. So far my code registration and unregistration code is:
- (void)viewDidLoad
{
[super viewDidLoad];
[self registerForKeyboardNotifications];
}
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
But if I'm showing the keyboard in let's say view controller number 3 in my navigation controller, the keyboardWasShown method is called three times. Doesn't this matter or do I need to unregister everytime the viewWillDisappear?
You should move [self registerForKeyboardNotifications]; to viewDidAppear and unregister in viewDidDisappear.
By registering in viewDidLoad and unregistering in dealloc, especially within a navigation controller, the notification will fire once for every view controller on the navigation stack. You only need to call it for the currently visible view.
Alternatively, you could subclass the navigation controller and have it call a method on its currently visible controller. Then you don't have to do all this registering and unregistering. Just register once in a nav controller subclass and have that class pass the message along to the proper view controller.
I've searched this site and the web and just looking for a simple example on how to reload the root view controller's table view from the detail view. I've tried notifications, setting a tableview in the detailview controller equal to the tableview of the rootview controller...nothing works.
Has anyone experienced this or have any sample code?
Use notificationcenter to pass a notification from the detail to the rootviewcontroller telling it to reload the data.
ex:
IN ROOT VIEW CONTROLLER
(where i've created a method called reloadRootTable, that calls [self.tableView reloadData];)
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reloadRootTable) name:#"reloadRootTable" object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"reloadRootTable" object:nil];
[super viewWillDisappear:animated];
}
IN DETAILVIEWCONTROLLER:
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadRootTable" object:nil];