I have a tab bar which displays different views when clicked. When you click a tab for the first time, it calls the viewDidLoad method. But, it only calls that the first time.
Is there a method that is called when a user clicks back to that tab, since the viewDidLoad won't be called that second time?
(I need to do this to update a UITableView when the user clicks back to a tab)
Of course!
- (void)tabBarController:(UITabBarController *)aTabBarController didSelectViewController:(UIViewController *)viewController
Your best option when looking for these sorts of things is to look in the documentation, specifically at the delegate for the object you're interested in.
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITabBarControllerDelegate_Protocol/Reference/Reference.html
I would use - (void)viewWillAppear:(BOOL)animated in UIViewController (docs)
Related
I am trying to implement a way for a user to logout of my app through a custom tabBarController. I have 5 tabBarItems, and I want the first one to be a logout tabBarItem - so when a user taps on it, an alertview pops up asking "are you sure you want to logout?". Is this possible - and if so what would be the best way about implementing this feature?
thanks!
-Matt
That seems like an unintuitive UI. I would consider redesigning so that there is a logout button somewhere within a view controller, toolbar or navigation bar.
With that said, if you really, really insist on making a "logout" tab bar item, you can create a UIViewController that presents a UIAlertView in viewDidAppear: and implements the UIAlertViewDelegate protocol. Then in alertView:clickedButtonAtIndex: you can update your global state to handle a logout (i.e. broadcast a notification through NSNotificationCenter, make a custom AppDelegate call, etc.).
As stated above I'm having problems regarding UITabBarController or specifically the tab bar not responding after manually/programmatically setting the selectedViewController or selectedIndex. This also happens when I pop the view controller of the previously selected tab before moving to another tab screen. Yes I believe I have checked the multiple times the delegate for UITabBarController and yes I have confirmed that the
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
is not being fired. Is there anyone who has experienced this?
Make sure set the delegate right, if not, the delegate method won't be fired.
Seems like loading behavior of assets might have changed in iOS7
Make sure that the view is completely loaded.
Prior to iOS7, you would create the tabbed view, push it, then select the tab.
in ios7, the view is not loaded synchronously. So while you did create it and display it, the select index gets called before it is actually shown and therefore has no effect.
At least this is what bit me as we have started to transition to iOS7
(Apologies for not being able to embed my images yet).
Using iOS storyboards, I have a UITabBarController with a UINavigation Controller/UITableView(1) embedded in it. This UITableView(1) then calls another UITableView(2):
What I'm trying to do is to make UITableView(2) appear when the Tab Bar is changed to that tab, and then have the UINavigationBar left arrow button exist to get back to UITableView(1).
The existing functionality I can think of which does this is the iPhone Mail app, where when you launch it you see your Inbox, and you can hit the left-arrow Mailboxes button to get back to your mail box list.
I've tried attaching the tab bar directly to UITableView(2) but it doesn't work as expected, there's no left arrow back button to get back to the previous view when the app is run.
I've also tried adding a navigation controller to that UITableView(2) and the Navigation controller correctly appears, but still without any back button:
Any suggestions as to what I'm doing wrong would be greatly appreciated, I'm fairly new with storyboards and it's difficult to find what to search to get this working.
If it's not possible with just storyboards themselves, is there some kind of automatic (non-visible) push to the 2nd UITableView I could do?
Thanks!
Nick.
This tutorial will definitely help you : http://maybelost.com/2011/10/tutorial-storyboard-in-xcode-4-2-with-navigation-controller-and-tabbar-controller-part1/
I ended up implementing it the following way, as I wanted to perform the majority of the work within storyboards.
I set up the storyboard with the tab bar embedding the UINavigationController, which contained UITableView(1) which then contained a custom segue to UITableView(2):
Then within the class associated with UITableView(1) I added this single line:
- (void)viewDidLoad {
[self performSegueWithIdentifier:#"campaigns" sender:self];
...
}
On load of the tab, the viewDidLoad of UITableView(1) instantly calls UITableView(2) without any kind of animation, showing the back button to UITableView(1), which is exactly what I wanted.
Thanks to those who responded!
You can implement the delegate method as below.
(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
In this method you can check for the tabBarController.selectedIndex or viewController object. This ensures the selection of correct tab , then push the controller having table 1, then push the controller having table 2.
So this is pretty advanced and I am not sure if it is feasible in the iOS SDK but here goes (please read carefully, I don't want skimmers preemptively answering this question before they know the details):
I have a tab bar view controller with two tabs. One tab (view controller 1) holds a data-presenting view controller, and the other (view controller 2) is a settings view. In the settings view I allow the user to specify whether they want to view a simplified or advanced version of the interface of view controller 1.
What I need to do, is based on these settings, present the chosen view for view controller 1, and I want to be able to do it on the fly (the user doesn't have to close and then reopen the app). I attempted to recreate the view controller array of the tab bar within
- (BOOL)tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController
but it just caused my app to crash (SIGABRT crash), go figure...
I was thinking of making a "hollow" view controller that would be able to point to the appropriate class type but the challange I have there is making it completely transparent to any view I should choose to place in it (UITableViewController, UIViewController, etc.).
Any ideas? Thanks in advance!
Have you tried looking at the docs at all UITabBarController?
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
I am developing an application in which I am using a login form at first screen. If the user is new he is takne to the signup form and after that he is also taken to the home page as a normal user.
Now my question is "If the new user presses back btn, how can I direct him back to the login page and not to the sign-up forms and all in between?"
Should I change the navigationcontroller's viewControllers array?
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated on UINavigationController will pop the user back to the last view controller in the stack.
If you want to get the navigation stack to a new known state you can use - (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated the last item in the array will then be the active one.
UIViewController has some methods for this. You can use
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
to pop up to the top view controller, or
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
to keep popping until you get to a specific controller you know is on the stack somewhere...
Perhaps you should think about your problem from another angle.
The signup form isn't really part of the flow of things--it's a temporary, one-time, mandatory interjection.
If you made the signup form a modal view, then you could just dismiss the modal view when signup is complete (or cancelled) and move normally to the next view controller.
That way, when you back up, the login view is the immediately previous one.