I have a question about UINavigationController.
I have create a project with storyboard using skd 7.
In my storyboard I have add a first NavigationViewController cnnected to TabBar Controller.
Now, into every ViewController of my TabBar I would to set the title...
In a first time I have try to set the title using:
[self setTitle:#"my title"];
But I can't to see the title..So, I have try with this (int a ViewDidLoad method).
self.navigationController.navigationBar.topItem.title = #"My title";
In this way I can see my correct title..but I can not figure out ...why?. What is the best way or the correct way to assign the title to my controller dynamically?
The correct answer is this self.navigationController.navigationBar.topItem.title = #"My title"; because you in order to change a title for a view that is pushed via UINavigationController you have to inform the navigation controller to do so.
Without the navigation controller, the other would be the correct answer. The issue here is that even without the navigation controller the first view in the nav controller should get UITabBarItem title.
Related
I'm new to xcode ios 7.
I've struggling a lot with navigation controls building app for iPhone ios7.
I don't want to use storyboard. I prefer to do it programmatically.
What i am trying to do.
I know how to create NavigationBar & Navigation Toolbar via AppDelegate.m
But if it's possible i don't want to do it, because let's assume i want in FirstViewController to show just simple button "Go to Second View Controller" (No Navigation Bar or Toolbar here).
And now in SecondViewController i want to create Navigation Toolbar (bottom) with 4 different tabs linking to ViewControllers!
But the trick is, i want to maintain different Navigation Bar (top) for every ViewController (as you can see in this screenshot).
Here is my Xcode Project File
And here is screenshot form Fancy app showing what i am trying to achieve.
Thanks a lot in advance!
Just to give you an idea, When you tap the button on your first view controller, you can create a UINavigationController and set your second view controller as its root view controller. This way, your first view controller remains no nav bar view and the second view controller holds a Navigation controller. Try something like below:
-(IBAction)goToSecondVC:(id)sender // An action for the button in your first VC
{
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil]; //Init with second view controller
UINavigationController *secondNavVC = [[UINavigationController alloc] initWithRootViewController:secondVC]; // create a navigation controller and set the root as your second view controller
[self presentViewController:secondNavVC animated:YES completion:nil]; // and then present it with anim or no anim..
}
I'm trying to select no tabs at all in my application. At first the first tab is selected, but I'd like to deselect it so no tabs at all would be selected.
Don't ask me why, it's just that way the client wants it! hehe
Thanks for your help!
PS: I already tried:
// rootController = UITabBarController
rootController.tabBar.selectedItem = 0;
rootController.tabBar.selectedItem = nil;
[rootController setSelectedIndex:[rootController.items objectAtIndex:0]];
[rootController setSelectedIndex:nil];
[rootController setSelectedIndex:0];
// That one works : (but I can't select 0 or -1 for instance)
[rootController setSelectedIndex:2];
Any ideas? Thanks again!
You can deselect all tab bar items if you are using UITabBar instance without UITabBarController one.
In such case below code works well.
[tabBar setSelectedItem:nil];
If UITabBar is a part of UITabBarController then application will crash with exception:
'Directly modifying a tab bar managed
by a tab bar controller is not
allowed.'
In other words if you would like to get this worked you need to manage tabbar's routines manually without controller.
I just came across this question and it's actually really simple:
tabBarController.selectedViewController = viewController;
This is somewhat similar to HG's answer, but setting the selected view controller to nil is unnecessary.
I finally managed to do this using the following code:
DefaultView *defaultView = [[DefaultView alloc]initWithNibName:#"DefaultView" bundle:[NSBundle mainBundle]];
[self.tabBarController setSelectedViewController:nil];
[self.tabBarController setSelectedViewController:defaultView];
Note that just doing [self.tabBarController setSelectedViewController:nil]; won't do anything. You HAVE TO specify a view controller. This view Controller will be displayed with no tabBar icon selected. Upon selecting the other TabBar options, the defaultView will disappear and the required view will be loaded.
Better to change selected image whenever you want & make a view hide or show according to your requirement. Here my piece of code that could help to understand:
-(void)viewWillAppear:(BOOL)animated{
if ([[NSUserDefaults standardUserDefaults]integerForKey:#"flagAsk"]) {
UITabBarItem *firstTab = [self.tabBarController.tabBar.items objectAtIndex:0];
firstTab.selectedImage = [[UIImage imageNamed:#"Ask2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
vieToHide.hidden=YES;
}
else{
UITabBarItem *firstTab = [self.tabBarController.tabBar.items objectAtIndex:0];
firstTab.selectedImage = [[UIImage imageNamed:#"Ask"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
vieToHide.hidden=NO;
}
}
From the documentation:
This view controller is the one whose custom view is currently displayed by the tab bar interface. The specified view controller must be in the viewControllers array. Assigning a new view controller to this property changes the currently displayed view and also selects an appropriate tab in the tab bar. Changing the view controller also updates the selectedIndex property accordingly. The default value of this property is nil.
So, I would assume you need to [rootController setSelectedViewController: nil];.
Update:
To clarify a bit,
[self.tabBarController setSelectedViewController:nil];
There is also documentation on preventing the selection of tabs that could be helpful.
Are there better methods?
use [self.tabBarController setSelectedViewController:nil],
Warning : "-[UITabBarController setSelectedViewController:] only a view controller in the tab bar controller's list of view controllers can be selected."
I think rootController.tabBar.selectedItem = 0;
it's wrong whatever you have tried.
Because when You are setting selectedItem=0, then sure it will take the first tabBarItem of tabBarController.
I have structure of project as shown below.
On the left side I have MainViewController. There I have two buttons as English and Arabic. What I want to do is when I click English, I want to go English tab bar controller (HomeViewController).
Hence what I wrote is
- (IBAction)langEnglish:(id)sender {
HomeViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:#"enghome"];
[self.navigationController pushViewController:secondView animated:YES];
}
This is working perfectly, but I don't see tab bar.
Tab bar is missing from this.
Any idea what is going wrong?
Basically what I have is view controller as main controller and upon clicking button on this controller, tab view controller should get open...
Go to your main storyboard and select your main view controller. On top select Editor->Embed in->navigation bar.
EDIT: If this does not work push to your tab bar and use this code:
self.tabBarController.selectedIndex = 1;
What I did is as below
- (IBAction)langEnglish:(id)sender {
EngTabViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:#"engtab"];
[self.navigationController pushViewController:secondView animated:YES];
}
EngTabViewController is the UITabBarController and I assigned id to tab bar controller... this works like charm...
Means instead of viewcontorller, I used tabbarcontroller...
You shouldn't push a UITabBarController on a UINavigationController. Set it as the window's root view controller instead and the tab bar should be displayed as expected.
I am a beginner in programming in iOS. I have a iOS application that has two classes(with xib), firstviewcontroller and secondviewcontroller. I want to add a tab bar to switch between these view controllers. if I for example add a tab bar to the first view, how to connect the views to the tab bar? Its just there, doing nothing..
Try Look into this url , this will help.
http://www.amateurinmotion.com/articles/2009/01/24/creating-uitabbarcontroller-based-app-using-interfacebuilder.html
This assumes that you want the tab bar as the main interface of your app.
In your app delegate, create a subclass of UITabBarController:
UITabBarController *myTbc = [UITabBarController alloc] init];
Create instances of your two view controllers and add them to an NSArray
NSArray *tabsArray = #[firstVC, secondVC];
Set that NSArray as the viewControllers property of the tab bar controller
[myTbc setViewControllers:tabsArray];
Set the tab bar controller as the app's root view:
self.window.rootViewController = theTbc;
How do you access an element in a tab bar controller and change its value? For instance i want to access a particular view controller in a tab bar controller and change a property in the view controller. Been stuck with this for a while. I have called tab bar init with nib name and initliased the tab bar. Now i need to know how to access one view in it. I tried
UITabBarController *newTabBar = [[UITabBarController alloc]initWithNibName:#"PlaceTabBarControllers" bundle:nil];
[newTabBar.selectedViewController setView:listViewController.view];
but this doesnt work.
UIViewController *viewController = [newTabBar.viewControllers objectAtIndex:0];