UITabBarController delegate didselect Item in UIViewContoller - objective-c

I have added UITabBarController in appdelegate.m. Please check the below code
DiViewController *contact = [[DiViewController alloc] initWithNibName:#"DiViewController" bundle:nil];
UINavigationController *contactNav = [[UINavigationController alloc] initWithRootViewController:contact];
contactNav.navigationBar.barStyle = UIBarStyleBlack;
self.tabController = [[UITabBarController alloc] init];
CGRect tabframe = self.tabController.tabBar.frame;
tabframe.origin.y -= 10;
tabframe.size.height += 10;
self.tabController.tabBar.frame = tabframe;
self.tabController.delegate = self;
self.tabController.viewControllers = #[contactNav];
Then i have arun DiViewController.xib in simulator bottom tab bar opened. Then i have click tabbar item.didSelectViewController working in appdeletgate.m. But i want in DiViewController.m. Ho can i get didSelectViewController in DiViewController.m. i have used same below in DiViewController.m but not working.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
}

Related

TabBar with center button

Hi, I'd like to make a tabBar with center button. As we can see from the image, when I clicked the tabbar, it will show me a view to select buttons. When I clicked the "notes" button, the view will disappear and my app will jump to the correspond viewcontroller and the tabbar.selectIndex = 2. I already made a subclass of UITabBarController, and add a button to the view, the code is like this [self.view addSubview:middleButton]. Now the problem is, when I push viewcontroll, the tabbar disappear while the center button still on the page. And when I go back to the main viewcontroller, the tabbar will overlap the center button. I don't want to make a whole custom UITabBarViewController, please tell me how to hide the center button when push viewcontroll and how to show it on the tapbar when I go back to the main viewcontroller.
Hi, Hament, I have not use method "-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item". My code is as below:
#implementation MyTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupViews];
[self setupTabBarItems];
[self setupMiddleButton];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)setupViews
{
mask = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([MyMiddleButtonMask class]) owner:self options:nil].firstObject;
mask.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
mask.delegate = self;
}
-(void)setupTabBarItems
{
UIViewController *vc1 = [[UIViewController alloc] init];
vc1Nav = [[UINavigationController alloc] initWithRootViewController:vc1];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2Nav = [[UINavigationController alloc] initWithRootViewController:vc2];
UIViewController *vc3 = [[UIViewController alloc] init];
vc3Nav = [[UINavigationController alloc] initWithRootViewController:vc3];
UIViewController *vc6 = [[UIViewController alloc] init];
vc6Nav = [[UINavigationController alloc] initWithRootViewController:vc6];
UIViewController *vc7 = [[UIViewController alloc] init];
vc7Nav = [[UINavigationController alloc] initWithRootViewController:vc7];
self.viewControllers = #[vc1Nav, vc2Nav, vc3Nav, vc6Nav, vc7Nav];
}
-(void)setupMiddleButton
{
middleButton = [[UIButton alloc] initWithFrame:CGRectMake(0, -16, 64, 64)];
CGRect f = middleButton.frame;
f.origin.y = SCREEN_HEIGHT - f.size.height;
f.origin.x = (SCREEN_WIDTH - f.size.width)/2;
middleButton.frame = f;
[middleButton setImage:[UIImage imageNamed:#"plus"] forState:UIControlStateNormal];
middleButton.backgroundColor = [UIColor redColor];
middleButton.layer.cornerRadius = middleButton.frame.size.height/2;
[self.view addSubview:middleButton];
[middleButton addTarget:self action:#selector(didClickedMiddleButton:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)didClickedMiddleButton:(id)sender
{
[[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:mask];
}
#pragma mark - KGOMiddleButtonMaskDelegate, Button1,2,3 are on the mask page.
-(void)didClickedButton1:(KGOMiddleButtonMask *)maskView
{
self.viewControllers = #[vc1Nav, vc2Nav, vc3Nav, vc6Nav, vc7Nav];
self.selectedIndex = 2;
[maskView removeFromSuperview];
}
-(void)didClickedButton2:(KGOMiddleButtonMask *)maskView
{
self.viewControllers = #[vc1Nav, vc2Nav, vc4Nav, vc6Nav, vc7Nav];
self.selectedIndex = 2;
[maskView removeFromSuperview];
}
-(void)didClickedButton3:(KGOMiddleButtonMask *)maskView
{
self.viewControllers = #[vc1Nav, vc2Nav, vc5Nav, vc6Nav, vc7Nav];
self.selectedIndex = 2;
[maskView removeFromSuperview];
}
-(void)didClickedSelf:(KGOMiddleButtonMask *)maskView
{
[maskView removeFromSuperview];
}
-(void)didClickedCloseButton:(KGOMiddleButtonMask *)maskView
{
[maskView removeFromSuperview];
}

Trying to add BarButtonItem to programmatically created NavigationController

I have pushed a view controller embedded in a navigation controller after tapping a button in my app. I am trying to add a "SAVE" button in the right corner of the nav bar. However, with this code it is not showing up. My assumption is that it is not showing up because nc.navigationItem.rightBarButtonItem should be placed in viewWillAppear. Since nc is programmatically created and does not have a class file with viewWillAppear how can I do this?
Here is my code:
- (IBAction)editProfileButtonTapped:(UIButton *)sender {
FXFormViewController *vc = [[FXFormViewController alloc] init];
vc.formController.form = [[PersonalContactInfoForm alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
nc.navigationBar.translucent = NO;
nc.navigationBar.barTintColor = [UIColor colorWithRed:(105/255.0)
green:(210/255.0)
blue:(231/255.0)
alpha:1.0];
// WHY IS THIS BUTTON NOT BEING ADDED?
nc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:nil];
[self presentViewController:nc animated:YES completion:nil];
}
The UINavigationController uses the -navigationItem of the currently displayed view controller. Try this instead:
vc.navigationItem.rightBarButtonItem = ...
Here you can take advantage of a powerful feature of OOP called Inheritance.
Create a Class say MyFXFormViewController inherited from FXFormViewController.
MyFXFormViewController.h
#import "FXFormViewController.h"
#interface MyFXFormViewController : FXFormViewController
#end
MyFXFormViewController.m
#import "MyFXFormViewController.h"
#implementation MyFXFormViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.rightBarButtonItem = saveBarButton;
}
And your previous code should be like
- (IBAction)editProfileButtonTapped:(UIButton *)sender {
MyFXFormViewController *vc = [[MyFXFormViewController alloc] init];
vc.formController.form = [[PersonalContactInfoForm alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
nc.navigationBar.translucent = NO;
nc.navigationBar.barTintColor = [UIColor colorWithRed:(105/255.0)
green:(210/255.0)
blue:(231/255.0)
alpha:1.0];
[self presentViewController:nc animated:YES completion:nil];
}

UITabBarController Cropped

I have a button in a viewcontroller that loads a motherViewController. This seems to be working correctly but for some reason the newly introduced view is so tall that the content at the bottom of are cropped.
- (IBAction)LoginButton:(UIButton *)sender
{
FirstViewController *FirstView = [self.storyboard instantiateViewControllerWithIdentifier:#"First"];
SecondViewController *SecondView = [self.storyboard instantiateViewControllerWithIdentifier:#"Second"];
ThirdViewController *ThirdView = [self.storyboard instantiateViewControllerWithIdentifier:#"Third"];
FourthViewController *FourthView = [self.storyboard instantiateViewControllerWithIdentifier:#"Fourth"];
FifthViewController *FifthView = [self.storyboard instantiateViewControllerWithIdentifier:#"Fifth"];
FirstView.Username = self.UsernameBox.text;
UINavigationController *FirstNavController = [[UINavigationController alloc]init];
[FirstNavController pushViewController:FirstView animated:NO];
UINavigationController *SecondNavController = [[UINavigationController alloc]init];
[SecondNavController pushViewController:SecondView animated:NO];
UINavigationController *ThirdNavController = [[UINavigationController alloc]init];
[ThirdNavController pushViewController:ThirdView animated:NO];
UINavigationController *FourthNavController = [[UINavigationController alloc]init];
[FourthNavController pushViewController:FourthView animated:NO];
UINavigationController *FifthNavController = [[UINavigationController alloc]init];
[FifthNavController pushViewController:FifthView animated:NO];
tabBar = [[UITabBarController alloc]init];
tabBar.viewControllers = [NSArray arrayWithObjects:FirstView, SecondView, ThirdView, FourthView, FifthView, nil];
[self.view addSubview:tabBar.view];
}
I don't whether i'm right or not. But here's the answer, you are trying to add UIViewController (UITabbarController) to your ViewController. So you need to position it in your view, for example
tabbar.view.frame = CGRectMake(0, 0, 320, 460);
But only thing i don't know its how it's perfectly positioned, when we are adding to window. Try your code by setting frame for your tabbarcontroller.
try this -
tabBar.tabBar.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;

I can not set title to my programatically tab bar controller

I try to create tab bar controller as programatically. It is ok but I can not set title to tab bar items.. How can I do this?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
myTabBarController = [[UITabBarController alloc] init];
tab1 = [[ZiyaretFormTab1 alloc] initWithNibName:#"ZiyaretFormTab1" bundle:nil];
tab2 = [[ZiyaretFormTab2 alloc] initWithNibName:#"ZiyaretFormTab2" bundle:nil];
tab3 = [[ZiyaretFormTab3 alloc] initWithNibName:#"ZiyaretFormTab3" bundle:nil];
tab4 = [[ZiyaretFormTab4 alloc] initWithNibName:#"ZiyaretFormTab4" bundle:nil];
tab5 = [[ZiyaretFormTab5 alloc] initWithNibName:#"ZiyaretFormTab5" bundle:nil];
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:#"title" image:nil tag:0];
tab1.tabBarItem = item;
myTabBarController.viewControllers = [NSArray arrayWithObjects: tab1, tab2,tab3,tab4,tab5,nil];
[self.view addSubview:myTabBarController.view];
myTabBarController.selectedIndex=0;
}
UITabBarItem *tabItem = [[[myTabBarController tabBar] items] objectAtIndex:1];
1 in this line means, that you try to get second object from array.
Also UITabBarItem has nice method initWithTitle:image:tag:. And this is link to documenttion its very helpfull.
My solve:
1) Create some items with:
initWithTitle:image:tag:
2) Add them to your tabbar with tab bar method:
- (void)setItems:(NSArray *)items animated:(BOOL)animated

Bottom half of UITabbar is not responding to clicks

I have created a UITabbar with 3 items and placed it on a UIScrollView
When I click the buttons of the tabbar they do not respond in the bottom half.
The upper area is working fine.
When clicking in the area just above the tabbar the tabs are also switched.
What can be wrong?
How can i correct this misalignment of the clickable button area?
In viewDidLoad:
[super viewDidLoad];
scroll.frame = CGRectMake(0, 20, 320, 460);
scroll.pagingEnabled = YES;
scroll.contentSize = CGSizeMake(320 * 2, 460);
scroll.showsHorizontalScrollIndicator = NO;
scroll.showsVerticalScrollIndicator = NO;
scroll.scrollsToTop = NO;
scroll.delegate = self;
scroll.pagingEnabled = YES;
viewNavController1 = [[viewNavController1 alloc] init];
ctrl = [[UITabBarController alloc] init];
ViewController1 *viewC1= [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
UINavigationController *control = [[UINavigationController alloc] initWithRootViewController:viewC1];
viewC1.title = #"Title1";
[viewC1 release];
ViewController2 *viewC2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
UINavigationController *control2 = [[UINavigationController alloc] initWithRootViewController:viewC2];
viewC2.title = #"Title2";
[viewC2 release];
UINavigationController *control3 = [[UINavigationController alloc] init];
ViewController3 *viewC3 = [[ViewController3 alloc] initWithNibName:#"ViewController3" bundle:nil];
[control3 pushViewController:viewC3 animated:NO];
viewC3.title = #"Title3";
[viewC3 release];
[ctrl setViewControllers:[NSArray arrayWithObjects:control,control2,control3,nil]];
CGRect frame = scroll.frame;
frame.origin.x = frame.size.width * 0;
frame.origin.y = 0;
viewNavController1.view.frame = frame;
viewC4 = [[ViewController4 alloc] initWithNibName:#"ViewController4" bundle:nil];
[viewNavController1 pushViewController:viewC4 animated:NO];
[scroll addSubview:viewNavController1.view];
frame = scroll.frame;
frame.origin.x = frame.size.width * 1;
frame.origin.y = 0;
ctrl.view.frame = frame;
[scroll addSubview:ctrl.view];
[scroll scrollRectToVisible:CGRectMake(320, 0, 320, 460) animated:NO];
UITabBarItem *itm = [ctrl.tabBar.items objectAtIndex:0];
itm.image = [UIImage imageNamed:#"img1.png"];
itm = [ctrl.tabBar.items objectAtIndex:1];
itm.image = [UIImage imageNamed:#"img2.png"];
itm = [ctrl.tabBar.items objectAtIndex:2];
itm.image = [UIImage imageNamed:#"img3.png"];
[control release];
[control2 release];
[control3 release];
The problem was that the UITabbarController did not have its view added as the root view of the windows. Which it apparently assumes.
so I had to trick it using:
[vc setWantsFullScreenLayout:YES];
vc is the main ViewController holding the scrollview containing the UITabbarController.
See Offset on UIWindow addSubview for more explanations.