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;
}
Related
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 the following requirements:
A Tabbar Navigation, with 2 "Tabs" that include a Login. So if a user tab (for example) an Button3 of the UITabBar ill get a fullscreen login view - But only on 2 specific tabs.
This is the workflow:
If there is an active session (i am using a login session management) - i will not present a login button - but if there is none, a login window should appear in fullscreen.
So when ill now show in any of the RootViewControllers in ViewDidLoad the loginViewController, the "content" of the rootViewControllers still get loaded.
So ill my opinion ill should handle after click on a button, WITHOUT loading the RootViewController for that tab.
Can anyone help me whats the best approach to solve such things?
You can use -(BOOL)tabBarController:shouldSelectViewController: method of UITabBarDelegate to handle tab selection and present login view instead of activating tab if needed.
You'll possible need to store a callback (i. e. block), in which you activate tab manually using tabBar.selectedIndex = index.
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
NSInteger index = [_tabBarController.viewControllers indexOfObject:viewController];
if (index==4)
{
if (_authorized)
{
return YES;
}
else
{
[self authorize];
__weak UITabBarController *tabBarController = _tabBarController;
_authorizationCallback = ^(){
//
tabBarController.selectedIndex = 4;
};
return NO;
}
}
return YES;
}
I have a UITabViewController. I'm trying to modally present a UIImagePickerController when a specific tab bar item is selected. It must appear over the current UIViewController. This is like what happens in the Instagram or Periscope app when you press on the camera icon. However I can't figure out how to display the UIImagePickerController without displaying the UIViewController that's connected to the Tab Bar Item. Is this possible?
Thanks,
Yusuf
You can use the shouldSelectViewController method of UITabBarControllerDelegate to accomplish something like this.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
// Check if it's the "image picker" tab
if (viewController == self.imagePickerTabView) {
// Present image picker
return NO;
}
// All other cases switch to the tab
return YES;
}
Alright so here is the deal. I have 5 tabs, 3 of them are just links. So when the user clicks on them, it actually brings them to a website using this:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{...}
So the tab bar item pretty much works as a button.
The links work just fine but the problem is that I don't want a view loaded on the click. So initially I added some [UIViewController alloc] for self.viewController (in the tab bar controller). Those view controllers are blank but they allow me to add the icon like so:
UIViewController *vc = [UIViewController alloc];
[vc1.tabBarItem setImage:[[UIImage imageNamed:#"tab-fb"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
vc1.title=#"Facebook";
All of this works fine, but when the user clicks on it, there is a blank black view. I understand this is normal, but how do I get rid of that? I want the user to be able to stay on the current view, but be led to the link.
I tried doing something like this:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
selectedIndex=[[tabBar items] indexOfObject:item];
if(selectedIndex == 1){
[self setSelectedIndex:0]; //RIGHT HERE
//...code to go to link...
}
}
but for some reason, the program does not respond to [self setSelectedIndex:0];
Any tips?
This does not work for me either in my tab bar.
However, this approach might work for you. In your app delegate didFinishLaunchingWithOptions:
MyTabBarController.delegate = self;
Then implement this delegate method in your app delegate:
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
In the method you can check for the VC like this:
if ([tabBarController.viewControllers indexOfObject:viewController] == indexOfTabToBlock) return false;
This effectively blocks the switch to that tab.
You would have to find some way to communicate this tap to the main view controller that you want to keep the user on so that it can process the link however you want. You could use a notification for example.
I'm working on an app and I would like to use the tab bar controller styling, but without the tabs. I would like it so that when I click on a tab button a function is run, but I don't want it to jump to a new tab. Is this possible and if so how do I do it?
Yes it is possible.
Implement the UITabBarControllerDelegate protocol's tabBarController:shouldSelectViewController: method execute your code and return NO.
Example
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
// Do your stuff here.
return NO;
}