Swift viewWillAppear not being called first time usingTabBarController - swift

So i have a tab bar controller and when I select into the second tab it brings me to a table view controller. I have it setup so that when the viewWillAppear it animates the cells in. The problem I am having is that the first time I go into that view, everything is stationary, but if i go to another tab and come back, everything animates perfectly.
How can I get it to animate in the first time I go to the tab as well?
I have not included code because I do not think it will help answer the question.
Edit* I can go to the tab as many times as I want and it will animate each time, but will never animate on the first load of the app.

I had a very similar issue with viewDidAppear. My problem was that I called the viewDidAppear function in the TabBarController to do some stuff but forgot to call super.viewDidAppear(true) in this Method. Because of that the viewDidAppear of the child wasn't called. After I added it everything works like a charm.
So be sure to add super.viewDidAppear(true) or in your case super.viewWillAppear(true) in the TabBarController. Maybe it helps

I had the same issue. Tried the below in "ViewDidAppear" and it is working.
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})

Related

iOS: return from pushed ViewController via delegate methods?

I've been wrestling with this for almost 2 hours now and no luck.
I have a View Controller. Lets call it First. I press a button and it takes me to Second view controller using a modal style. In Second I add some data which I can save or discard via two navigation bar buttons: Save and Cancel. both do what they do and then they return me to the previous screen. I implemented a protocol and I use the delegate. So far everything works fine.
Today I decided to have a Third View Controller which can be accessed from the Second VC. The problem is I couldn't move to THIRD until I changed the transition from First to Second to PUSH (instead of modal). Now I can go from First to Second and from Second to Third. All good again.
THE PROBLEM : If I press Cancel or Save on the Second VC, it calls the methods, it uses the delegate to go to FIRST and execute some function but when it calls:
[self dismissViewControllerAnimated:YES completion:nil];
nothing happens and Im stuck on the Second view. Basically I was using MODAL with Delegate and protocol methods... I switched to PUSH, and I cant get rid of my Second view and return to First. It seems like the dismissViewController doesnt do anything.
Any help is greatly appreciated. I can fix all this by removing the cancel button and using the BACK which comes by default with PUSH but I just want to know what the problem is with it.. THANK YOU !!
On your 2nd viewContrller call the UINavigationController's method popViewControllerAnimated: or popToRootViewControllerAnimated:. This will pop the controller (the one calling, which is the 2nd) off the nav stack. You can get the navigation controller in a UIViewController via its property navigationController.

how to refresh UX data on a ViewController when its TabBarItem gets tapped?

In the view controller that has a UITabBarItem, i realized that viewDidLoad() method only gets called the first time when the tab bar item is clicked. So I dont know how to bring up the dynamic graphics when it's clicked the 2nd time. Can some guru help me on this? thanks in advance.
Should I conform to some kind of delegates or should i do it within didSelectViewController method on the root controller of all the tab bars? If i do the later one, it just seems to be weird since i think the controller that has the corresponding tab bar item should render itself instead of doing the rendering on root controller..
You want to put any code that should run every time the view controller appears in viewWillAppear: instead of viewDidLoad. viewDidLoad is designed for code that should run when the view backed by your UIViewController is created (and then possibly re-created after being thrown away during low-memory situations).
Actually i resolved this by using the parameter passed into the callback didSelectViewController(param).

I am not able to refresh the old view on button click

I have viewBased Application, i have 3 view controller in that.
When I want to go back to first view from second view, I want that view to be reloaded from start.
Code I use to dismiss the present view is:
//Back Button Code:
[self dismissModalViewControllerAnimated:YES];
//Code I use to go to new view is:
[self presentModalViewController:secondView animated:YES];
I am not getting what you trying to do...
put your logic inside viewWillAppear method... which gets called everytime view Loads..
When I want to go back to first view from second view, I want that view to be reloaded from start.
Wich one?
Well I think it should work since dismissing a modal dialog unloads it. What happens exactly that you don't want? You must have retained something you don't want to.
It's not clear if you want your first view to restart, or your second view to restart.
Answer for both cases:
If you want view 1 to restart, move the code you need re-executed into viewWillAppear()
If you want view 2 to restart, it's been released from memory so going back to it will call viewDidLoad() and restart it anyway.
Hope that helps....

Only showing View after second Push on iPhone

I'm having an annoying problem, that may have a simple answer!
I have a ViewController (which contains a TableView Controller and Header View) which I am pushing on to a Navigation Controller - When I push it on the first time after launching, I get a blank view. When I click the Back button to pop it, it appears fine from then on until I re-launch the app.
Does anyone know an "obvious" reason why this would happen?
BTW- following the code in Debug, it appears to be doing all the correct things.. loading the ViewController variables if nil, etc. before the first view.
Thanks!
did you reload the table after data are loaded ?
[tableView reloadData];

Why is self.navigationItem.hidesBackButton not working?

I have a UIViewController that is pushed onto a UINavigationController and is currently displayed. When I go to start some asynchronous task inside the view controller, I can set hidesBackButton on self.navigationItem to YES, and the back button is hidden correctly.
As soon as the task is finished, and I set hidesBackButton back to NO (on the UI thread, I might add, I've made sure of this), nothing happens. The back button remains hidden.
Has anyone seen this before? What drives me especially crazy is that in my application (the same application), in a different UINavigationController hierarchy, the exact same code works correctly!
Are you calling hidesBackButton = NO from a thread? All UI operations should be done on the main thread, otherwise they won't have any effect.
i have not been able to replicate your problem on my machine. however, i faced a similar issue with tableviews even when i was updating my ui on the main thread. but calling setNeedsDisplay fixed that issue.
Can you try this and see if this works:
[self.navigationController.navigationBar setNeedsDisplay];
I guess this should work, you need to do the same, BUT ON THE NAVIGATIONBAR instead. please let me know if this worked - as i cannot test my solution because i never get this problem :-)
Have you tried forcing the view to refresh by calling setNeedsDisplay?
Maybe the OS is not picking up the changes instantly and you need to force it.
Have you tried using the setHidesBackButton:animated: method instead? Perhaps that has a slightly different behavior.
In my case I simply had to give a title to the view, as in:
self.navigationItem.title = #"Menu";
Marinus
I have had a similar issue recently. I tried literally everything I found in SO and other forums- nothing worked.
In my case there was a modally shown UINavigationController with a simple root controller which would push one of two view controllers (A and B) on top of the controller stack when the button A or B was pressed, respectively. Controller B was the one which was not supposed to show the back button. But still, sometimes it did, sometimes it didn't.
After hours of debugging, I managed to track it down. Controller A was a UITableViewController. Each time I selected a cell in this controller, the delegate would pop Controller A off the stack. BUT. I made use of a UISearchDisplayController as well. Turned out that popping the view while the search controller was still active messed up something in the navigation controller that made it impossible to hide the back button in Controller B afterwards (well, it eventually stayed hidden between viewDidLoad and viewDidAppear: but then it always turned visible).
So the solution (rather workaround) was adding this line to where Controller A was dismissed:
controllerA.searchDisplayController.active = NO;
// ...
// [self.navigationController popViewControllerAnimated:YES];
Hope this spares someone a couple of hours.