UIAlertview in Tab bar icon - iphone

how to make tab bar icon clickable. Once I tap on it should get a pop-up or an alert (UIAlertView).
I know how to present UIAlertView, but i want to have alert on click the tab bar icon.

First you need to set your UITabBarController to its delegate and use the method didSelectItem
In .h add <UITabBarDelegate>
In viewDidLoad
yourTabBarController.delegate=self;
Then use the delegate method:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if(item == yourAlertTab)
{
//Code to show alert.
}
}

Related

how to program selecteditem in tab bar

i have this tab bar i want to configure every tabBarItem in this tab bar for exemple when i select the first item i want to go to this view (My Espace) for exemple the first item
i had made this code but it didn't work !
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
if(self.tabBar.selectedItem==[self.tabBar.items objectAtIndex:0])
{
CondidatsViewController *condidatsViewController=[[CondidatsViewController alloc]initWithNibName:#"condidatsViewController"bundle:nil];
[self.navigationController pushViewController:condidatsViewController animated:YES];
}
}

Pressing UITabBar icon and select.... Error Code

I am using UITabbar because I like the Icon design. But I don't use UIButtons. I have two icon buttons and one label.
I've delegate method this. My NSLog won't appear if click first icon and second icon. I have this code appear warnings Xcode. How do you fix this?
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
if ([viewController.tabBarItem.title isEqualToString:#"FIRST"])
{
label.hidden = YES;
NSLog(#"FIRST");
} else if ([viewController.tabBarItem.title isEqualToString:#"SECOND"])
{
label.hidden = NO;
NSLog(#"SECOND");
}
}
In the code you posted, you need to replace the viewController.tabBarItem with just item both of the times it appears. The tab bar item that you want to investigate is the item parameter passed to this method. The method is not passed a viewController parameter, so the viewController variable is undefined. That is why XCode underlines it in red.

How to display different view everytime when a tabbar item is clicked in UITabBarBased app?

I want screen 1 to be shown when tabbaritem 1 is clicked and if I change some settings, i go to different view, , when I click the tabbaritem 1 again I want to show screen 2.
I have a UITabbar based app and the MainWindow.xib has different tabs loaded before with views.
How do I change it programmatically?
Please help
just put the code for the views to be created in the method viewWillAppear instead of viewDidLoad. This is being called each time go go back to your tab 1
Implement below method in your App Delegate
- (void)tabBarController:(UITabBarController *)controller willBeginCustomizingViewControllers:(NSArray *)viewControllers {
UINavigationController *nc;
nc = viewController;
if(controller.selectedIndex == 3){
[[nc.viewControllers objectAtIndex:0] replaceSubView];
}
}
Here,
if(controller.selectedIndex == 3)
3 = your viewcontroller index in which you want to change subview.
And "replaceSubView"
is your method in view controller in which you want to change subview.
Let me know in case of any difficulty.
Cheers.
Turn MainWindow or AppDelegate into UITabBarDelegate, than use
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
to caught tab selections. When tab you need is selected, use image property of UITabBarItem to set image you like.

UITabBarDelegate in AppDelegate

I have a UITabController in my main window, and would like to add some logic when each tab is selected. I've added the delegate to the header file:
#interface MyAppAppDelegate : NSObject <UIApplicationDelegate, UITabBarDelegate> {
I have a method for the tab change event:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
//some code
}
But the code inside the didSelectItem method isn't called. I've connected the delegate for the UITabBarController to my AppDelegate in IB. Is there anything else I need to do?
If you assign the delegate via IB, you should connect the delegate for your TabBar (as opposed to your TabBarController) to your app delegate.
Indeed, you are not looking for the UITabBarControllerDelegate, but for the UITabBarDelegate.
If you do it programmatically, then, from your tab bar controller viewDidLoad execute:
self.tabBar.delegate = [UIApplication sharedApplication].delegate;
If you use UITabbarController you can use UITabBarControllerDelegate instead of UITabBarDelegate.
Then, you can set self.delegate = self. Then you use:
(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
instead of:
(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
Did you assign some class to be the tab bar's delegate? Something like
myTabBar.delegate = self;
i might be out on a ledge here but i think the signature of the method should be:
- (IBAction)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
and then you connect it to the tabbar in IB. after you change to IBAction it should appear in IB
I ended up putting it in the viewWillAppear method of the view in the specific tab I need. Seems to work fine.

How can i get Tabbar Item Button's Click event by clicking on TabbarItem button?

I have taken UITabbar on that i have used two buttons as TabItem.I want to perform two different Action by clicking on that two button so how can i get particular Action on clicking on particular Tabbar button.
You most likely want to take advantage of the UITabBarControllerDelegate, and then handle the didSelectViewController method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
See here for more details: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITabBarControllerDelegate_Protocol/Reference/Reference.html
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if(item.tag==1)
{
//your code
}
else
{
//your code
}
}
You can use UITabBarDelegate for keeping the track of which button is pressed by assigning the tag or title for title you can use item.title.