I have a UITabBarController with five tab bar items, one of them is share button action (to share the itunes store link - tell a friend). My issue is when I click on this tab, the UIActivityViewController has displayed with blank controller, I need to show the share window (UIActivityViewController) to appear above the last tab bar view selected to avoid the blank view of this tab bar item.
You can implement the UITabBarControllerDelegate protocol and use the
- tabBarController:shouldSelectViewController: method to intercept the tap on the share tab like this:
- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if (viewController == [tabBarController.viewControllers objectAtIndex:/*share button index*/]){
//show UIActivityViewController
return NO;
}
return YES;
}
I have a tab bar navigation view and I would like to use one of the tab bar items to go to a twitter link either in the twitter app or safari. When you go back to the app, I would like it to be in the same view as before the tab bar item was touched.
I'm not sure if this is possible as I am new to iOS.
You may consider just directly opening the twitter link when the user selects the tab, but not actually changing tabs. Just override the tabBarController:shouldSelectViewController: delegate method and do something like this:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
if(index == yourTwitterTabIndex){
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:#"whatever the Twitter app url is"]];
return NO;
}
return YES;
}
I have a NavigationController then a TabBarController which has Four Tabs.
I wanted to display Different titles on TopBar when a Different Tab is selected.
One way was to Embed each TabBarItem View into Navigation Controller but for some reason this doesn't seems the correct way, i wanted to apply this via code.
I managed accomplish this by using this code: (Products_ViewController.m custom class)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UINavigationController *navCon = (UINavigationController*) [self.navigationController.viewControllers objectAtIndex:0];
navCon.navigationItem.title = #"Products";
}
But the problem is now when a tab is clicked First time, it changes the title but then it doesn't. I then applied the same code on -(void)viewDidAppear{} but still the same result.
How can i manage to display navigation top bar title (or run the above code) whenever the tab bar item is clicked or the view is shown?
Thanks!
You could implement the UITabBarControllerDelegate in the Products_ViewController.m class and execute your code in the tabBarController:didSelectViewController: method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
UINavigationController *navCon = (UINavigationController*) [self.navigationController.viewControllers objectAtIndex:0];
navCon.navigationItem.title = #"Products";
}
In the viewDidLoad method you have to set the delegate to self.
I have 3 tabs in my UITabbarController, that I created in my Appdelegate.
When I open the app, I have made the selected tabbarItem the third tabbarItem.
The user can only select the UITabBarItem at Index 0, when he is logged in.
I tried every thing to restrict the user from going to TabBarItem_0 when he is at TabBarItem_2.
But nothing worked. I used
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
}
But it's not working as I desired. I checked the stackoverflow and found almost the same question, where I found this delegate. But this is not working for me as desired. I googled, but couldn't find any solution other than stackoverflows links, which didn't help this time.
On the click of that disabled TabBar Item, I have to show a pop up. How can I implement that, too?
Try something like this,
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
{
if (tabBarController.selectedIndex == 0) {
if(isUserLoggedIn)
return YES;
else
return NO;
}
return YES;
}
If this does not work then,
Add this after you create the bar bar in app delegate,
[[[[self.tabBarController tabBar]items]objectAtIndex:0]setEnabled:FALSE];
once you log in enable it again
[[[[self.tabBarController tabBar]items]objectAtIndex:0]setEnabled:TRUE];
Quick hack that i used to present a popup on top of the current view when tapped on a tab bar item.
In your TabBarViewController class, implement the UITabBarControllerDelegate and remember to set self.delegate = self.
After that
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
if viewController.title == "Unique_title" //set an unique title for the view controller in storyboard or the view controller class.
{
performSegueWithIdentifier("YourModalViewIdentifier", sender: nil)
return false
} else {
return true
}
}
That should help you display a modal view when tap is received on the uitabbaritem. I know using title as an unique identifier is bad practice, but just a quick solution to achieve what you want.
Hope that helps.
You can do this in your code
- (void)viewDidLoad {
...
[self checkLogin];
...
}
- (void)checkLogin {
if (!loggedIn) {
[[[[self.tabBarController tabBar]items]objectAtIndex:0]setEnabled:NO];
} else {
[[[[self.tabBarController tabBar]items]objectAtIndex:0]setEnabled:YES];
}
}
- (void)tapLogin {
// Do the login action
}
- (void)processLoginResult {
// Process the result of the login action
// If the result is success, set 'loggedIn = YES'
// Otherwise, set 'loggedIn = NO'
...
[self checkLogin];
...
}
If you want to do it with the Storyboard, simply selected the TabBarItem in the destination view controller scene and uncheck the Enabled box.
This is what I did in Swift 2.1:
self.tabBarController!.tabBar.items![0].enabled = false
Here is how you disable a tabbar item in Swift 3 and 4
tabBarController.tabBar.items![0].isEnabled = false
I have a UITabBarController in the root of my storyboard with three tabs. One of the tab is used to display the user information. I have set this tab to transition to the register/login page if the user has not registered. But the ViewController gets popped(and corrupts the Navigation Bar) when I double tap the tab icon. I cannot use the modal transition as it hides the TabBarController. What is the best solution to solve this issue?
You can use 'shouldSelectViewController' delegate of TabViewConroller to check if the selected viewController is same as the clicked one and skip it accordingly.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if(viewController == tabBarController.selectedViewController)
{
return NO;
}
}