Reload tab with updated values - ios

I have a Tab Bar Controller. There are two tab. First one is for showing some data on Table View Controller on the basis of some calculation. And the second tab is for changing values of variables which are used for the calculation of first tab's data.
Now if I change value in second tab and back to first tab it does not update the calculated value in first tab. But it does when I run the app again.
I have gone through many solutions but not worked for me. I used
[self.tableView reloadData];
But not worked for me.
Please Help.

Use ViewWillAppear to refresh the viewController every time you click on the tab.
As in tabViewController ViewController is loaded only once.So ViewDidLoad Method is called only first time (As you say when your app restarts).
While viewWillAppear will be called every time you switch between the tabs.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//refresh the view controller
}

Please pass data between two ViewController and Put your code inside
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
It will work.

You are most likely calling your [self.tableView reloadData];
in viewDidLoad only. This is not called when you switch tabs since the views are already loaded. You can reload your tableView on viewWillAppear: like proposed by #Ved
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];
}
otherwise, you can insert it in your setter. So every time your calculated value changes, your tableView will be reloaded.

Related

Is it necessary to call super in viewWillAppear?

I am trying to understand the scenario of the method calls to view did/will appear and disappear.
What I did is selecting the table cell (higlights in grey) , go to detail view and go back and deselect the selected row (remove the selected cell grey color).
Here are my methods:
-(void)viewDidAppear:(BOOL)animated {
DLog(#"%# did appear", self);
[super viewDidAppear:animated];
if (_isPushed) {
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
_isPushed=NO;
}
}
-(void)viewWillAppear:(BOOL)animated {
DLog(#"%# will appear", self);
[super viewWillAppear:animated]; //If I remove this super call , then it works fine and there is no delay in deselecting the table cell
}
-(void)viewWillDisappear:(BOOL)animated {
DLog(#"%# will disappear", self);
[super viewWillDisappear:animated];
}
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
_isPushed=YES;
}
So , when I put breakpoint the flow goes like this:
without super call:
while pushing to new VC:
current viewWillDisappear //makes sense
new viewWillAppear //makes sense
current viewDidAppear // doesnt make sense , y this should get called as the view is already appeared?
current viewWillDisappear // make sense
current viewDidDisappear //make sense
new viewDidAppear //make sense
while coming back from pushed VC:
current viewWillDisappear
new viewDidDisappear
current viewDidDisappear
new viewDidAppear
with super call:
while pushing to new VC:
current viewWillDisappear
new viewWillAppear
current viewDidAppear
current viewWillDisappear
current viewDidDisappear
new viewDidAppear
while going back from pushed VC:
current viewWillDisappear
new viewDidDisappear
current viewDidDisappear
new viewDidAppear
The flow is pretty much the same either I use super call or not.
But the problem I am facing is, when I use super call in viewWillAppear, there is a delay(around >1second) in deselcting the cell.
If I dont use the super call in viewWillAppear , there is no delay and the cell is deselcting (around <0.5 seconds)
I am not sure to use super call or not.
Can anyone please tell me why there is a delay in deselecting the cell?
Yes, the documentation states you must:
Discussion
This method is called before the receiver's view is about
to be added to a view hierarchy and before any animations are
configured for showing the view. You can override this method to
perform custom tasks associated with displaying the view. For example,
you might use this method to change the orientation or style of the
status bar to coordinate with the orientation or style of the view
being presented. If you override this method, you must call super at
some point in your implementation.
Generally yes, call super. I've seen weird things happen in nav controllers when I forget.
In this case, if you have a UITableViewController, try using its clearsSelectionOnViewWillAppear flag to clear the selection for you.
Yes It's necessary to write super.

Pushing self.view to navigation controller by allocing, but when coming back same data is shown

I am pushing self view to self.navigationcontroller by allocating. I have a tableView on that view so I am changing the content of tableview. But when I am pressing back button (that is automatically created), I am not able to show previous content. Its showing updated content.
Please suggest.
You set code in viewWillAppear method
- (void)viewWillAppear:(BOOL)animated
{
//code set here
}
If you fill the tableView's data in viewWillAppear: or viewDidAppear:, it will reload even if you only press the back button of your top viewController. If you do not want to have your content changed, you are supposed to use initWithNibName: or viewDidLoad: methoads. They are called only at creation time of the view.
Based on the comments on #Kirti's post, You can check if your viewcontroller is being popped by following method, and take some necessary actions for you controller holding table.
-(void) viewWillDisappear:(BOOL)animated
{
if(![self.navigationController.viewControllers containsObject:self])
{
YourControllerWithTable *instance = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 1];
instance.loadOldContent = YES;
}
}
In viewWillAppear: of YourControllerWithTable, you can check:
-(void) viewWillAppear:(BOOL)animated
{
if(loadOldContent)
{
//Do your work here
}
}
You don't push UIView instances onto a UINavigationController instance, only instances of UIViewController.

How to run viewDidAppear

I need to load some data into a view every time it is shown. The data changes, each time time view is shown, so I figure I can load the data in the method viewDidAppear. Unfortunately, I've found that viewDidAppear is not called each time the view is displayed.
The code that displays the view from any other view is....
[self clearView];
[self.view insertSubview:fifthViewController.view atIndex:4];
So I figured I could change it to the following to run viewDidAppear...
[[self.view insertSubview:fifthViewController.view atIndex: 4 viewDidAppear:YES];
Unfortunaely, this causes an error "bad receiver type 'void'
What do I need to do to insert the subview and also call viewDidAppear?
If you show the view by modifying ViewController.view visibility directly, you won't get viewDidAppear message by that. You need to use ViewController method to display the view, e.g. push controller into UINavigationController or using presentModalViewController method. You can use the hack like calling viewWillAppear: and viewDidAppear: manually, but I don't like the idea.
Thank you for the assistance with this question.
I have settled on the addition of a viewDidAppear in the method that inserts the subview.
Below is the code that is working for me at this time.
In the .m file of the highest level view controller, the following code sets up the viewDidAppear call and then inserts the fifth subview.
-(IBAction) loadFifthView:(id)sender
{
[fifthViewController viewDidAppear:YES]; // sets up viewDidAppear
[self clearView];
[self.view insertSubview:fifthViewController.view atIndex:4];
}
With the above code snippet in place, the following code snippet, located in the .m file of the fifth view controller reports that it is working.
- (void)viewDidAppear:(BOOL)animated
{
NSLog(#" xxxxxxxxxxxxxxxx inside viewDidAppear ");
}

Force tableView reloadData from Modal Dismissal?

When my application launches, a table view is present and uses the information in two arrays (countdowns and countdown_info) to populate the cell title and description in the table view respectively.
A modal can be opened and used to create a new countdown. So far, it saves the data in a local file, closes, and then returns to the front controller, but the table is not updated with the new data.
How can I get the new information added to the table? The UITableView is named as *countdown_table
Thanks!
EDIT: The return code looks like this:
...Above Saves the File...
// The following generates a warning. ClockworkViewController is the main view controller.
//[[ClockworkViewController countdown_table] reloadData];
[[self parentViewController] dismissModalViewControllerAnimated:YES];
Load the newly added value to the array
Call [tableview reloadData] in your table view controller's viewWillAppear method.
Are you calling the [yourTableView reloadData] function? You should call this either when the modal view is saved, or when the table view becomes active again. This will then check all of the tableView's methods for number of rows, number of sections, etc, and reload the entire table.
You may explicitly call [table reloadData] when you dismiss the modal view
If you're using Interface Builder to perform your presentations/dismissals, you can very easily reload the ParentVC's tableView from the ModalVC's Exit Segue (transitioning back to the ParentVC, of course).
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"YourDismmssSegueID"]) {
ParentVC *vc = segue.destinationViewController;
// perform any needed data manipulation here
[vc.tableView reloadData];
}
}

Make UITableView selection before view appears

I'm building a detail view for a UITableView. The detail view contains a UITableView which has two rows, and I want the first one to be selected when the detail view is opened. I have tried to use the method [selectRowAtIndexPath:animated:scrollPosition] in [viewWillAppear]:
- (void) viewWillAppear:(BOOL)animated {
[[self tableView] selectRowAtIndexPath:[NSIndexPath indexPathForRow:kStartDateRow inSection:kSection]
animated:NO
scrollPosition:UITableViewScrollPositionNone];
}
However, the selection is not affected by this, and using it in [viewDidAppear] means the selection changes once the view has moved into place. How can I make the selection before the detail view appears on screen?
You can see an example of the behaviour I want in the Settings app. Go to General > Date & Time > Set Date & Time.
You don't mention any error but try doing [[self tableView] reloadData] before the selectRowAtIndexPath line.

Resources