Navigation Controller Toolbar Buttons issue - Xcode - ios

I'm trying to add some button to the toolbar of my navigationController: i see the toolbar, but without the buttons. This is the portion of my code where I set the toolbar...
(this is my AppDelegate)
// Create a table view controller
RootViewController *rootViewController = [[RootViewController alloc]
initWithStyle:UITableViewStyleGrouped];
rootViewController.managedObjectContext = context;
rootViewController.entityName = #"County";
//Navigation Controller
UINavigationController *aNavigationController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
//Barbuttons
UIBarButtonItem *homeButton;
homeButton = [[[UIBarButtonItem alloc] initWithTitle:#" Inizio " style:UIBarButtonItemStyleBordered target:self action:#selector(home)] autorelease];
UIBarButtonItem *barButton;
barButton = [[[UIBarButtonItem alloc] initWithTitle:#" Funzioni online " style:UIBarButtonItemStyleBordered target:self action:#selector(caricamappa)] autorelease];
UIBarButtonItem *creditsButton;
creditsButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"credits2.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(credits)] autorelease];
NSArray *baritems = [NSArray arrayWithObjects: homeButton, barButton, creditsButton, nil];
[window addSubview:[navigationController view]];
[self.navigationController.toolbar setItems:baritems];
[self.navigationController setToolbarHidden:NO];
[window makeKeyAndVisible];
[rootViewController release];
[aNavigationController release];
Any idea about my mistake?

You should add buttons to navigationItem property of your rootViewController, not to the toolbar of navigation controller.
Something like:
rootViewController.navigationItem.rightBarButtonItems = barItems;

Check out the documentation, especially this part:
Management of this toolbar’s contents is done through the custom view
controllers associated with this navigation controller. For each view
controller on the navigation stack, you can assign a custom set of
toolbar items using the setToolbarItems:animated: method of
UIViewController.

Related

iOS - [ObjC] NavigationBarButtonItem is not showing when the navigation bar's root view is presented modally

In my app I have two tabs that handle different set of functions.
One tab is the user tab, when the user switch to this tab, the tab controller checks whether the user has logged in. If not, it shows a button(LoginBtn) which triggers a log in view controller to show when tapped.
I intend to present the log in controller modally with a navigation bar.
However, the navigation bar is not showing the right button item although I've initiated it.
Here's the code
- (void)clickLoginBtn{
LogginController* _cLogginController = [[LogginController alloc] init];
UINavigationController *_cNavController = [[UINavigationController alloc] initWithRootViewController:_cLogginController];
_cNavController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"取消" style:UIBarButtonItemStylePlain target:self action:#selector(dismissLoginView)];
[_cNavController.navigationItem.rightBarButtonItem setTintColor:kColorWhite];
_cNavController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:_cNavController animated:YES completion:nil];
}
What could be the problem? Is it possible that is because I present to controller modally?
You have put your buttons to the login controller:
_cLogginController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"取消" style:UIBarButtonItemStylePlain target:self action:#selector(dismissLoginView)];
You shouldn't add navigation items to the navigation controller.
try this code.I think this issue is solved by this code
- (void)clickLoginBtn{
LogginController* _cLogginController = [[LogginController alloc] init];
_cLogginController .hidesBottomBarWhenPushed=No;//You need to add this line
UINavigationController *_cNavController = [[UINavigationController alloc] initWithRootViewController:_cLogginController];
_cNavController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"取消" style:UIBarButtonItemStylePlain target:self action:#selector(dismissLoginView)];
[_cNavController.navigationItem.rightBarButtonItem setTintColor:kColorWhite];
_cNavController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:_cNavController animated:YES completion:nil];
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
DemoViewController * _cLogginController = [[DemoViewController alloc]init];
_cLogginController = [storyboard instantiateViewControllerWithIdentifier:#"DemoViewController"];
_cLogginController .hidesBottomBarWhenPushed = NO;
UINavigationController *_cNavController = [[UINavigationController alloc]initWithRootViewController:_cLogginController];
_cNavController.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];
_cNavController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
_cLogginController.navigationItem.title = #"取消";
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView:)];
flipButton.tintColor = [UIColor grayColor];
_cLogginController.navigationItem.rightBarButtonItem = flipButton;
[self presentViewController:_cNavController animated:YES completion:nil];

iOS 7: different navigation items for tab bar controller

I am relative new with the iOS app development. Currently I am developing a small app with a tab bar. The problem I am facing is that I would like to have different navigation items foreach tab. I tried a lot of things, but things aren't working. I am programming in the native iOS language.
In my app I've got a AppDelegate. In my AppDelegate there is a little piece of code for setting up my mainViewController:
- (void)setupOverlordViewController
{
MainViewController *rootVC = [[MainViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = navVC;
}
I am setting up my tabs in my MainViewController:
- (void)viewDidLoad
{
UIViewController *tabView1 = [[Tab1ViewController alloc] init];
UIViewController *tabView2 = [[Tab2ViewController alloc] init];
UIViewController *tabView3 = [[Tab3ViewController alloc] init];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:tabView1];
[tabViewControllers addObject:tabView2];
[tabViewControllers addObject:tabView3];
[self setViewControllers:tabViewControllers];
tabView1.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"TabView1", nil)
image:[UIImage imageNamed:#"tabView1.png"]
tag:1];
tabView2.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"TabView2", nil)
image:[UIImage imageNamed:#"tabView2.png"]
tag:2];
tabView3.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"TabView3", nil)
image:[UIImage imageNamed:#"tabView3.png"]
tag:3];
}
Each View (tabView1, tabView2, tabView3) has there own layout, which is set in the ViewDidLoad method of the View. When I would like to add navigation buttons in the navigation bar by adding them in the ViewDidLoad method, but it seems impossible to add the buttons. The only way to add them is directly in my MainViewController, but then I can't set the navigation bar buttons different foreach tab.
The code for adding the buttons to my navigation bar is as follows:
UIBarButtonItem *btnNewRecord = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(btnNewRecord)];
NSArray *rightItems = [NSArray arrayWithObjects:btnNewRecord, nil];
[self.navigationItem setRightBarButtonItems:rightItems];
Could somebody explain me what I am doing wrong?
I have created a example for you by using xib files. I have created three View Controllers and added them to navigation controllers. Following the appdelegate code :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:#"FirstView" bundle:nil];
UINavigationController *firstNavVC = [[UINavigationController alloc] initWithRootViewController: firstVC];
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:nil];
UINavigationController *secondNavVC = [[UINavigationController alloc] initWithRootViewController: secondVC];
ThirdViewController *thirdVC = [[ThirdViewController alloc] initWithNibName:#"ThirdView" bundle:nil];
UINavigationController *thirdNavVC = [[UINavigationController alloc] initWithRootViewController: thirdVC];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:firstNavVC];
[tabViewControllers addObject:secondNavVC];
[tabViewControllers addObject:thirdNavVC];
firstNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"First", nil)
image:nil
tag:1];
secondNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"Second", nil)
image:nil
tag:2];
thirdNavVC.tabBarItem =
[[UITabBarItem alloc] initWithTitle:NSLocalizedString(#"Third", nil)
image:nil
tag:3];
UITabBarController *tabbarController = [[UITabBarController alloc] init];
tabbarController.viewControllers = tabViewControllers;
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = tabbarController;
[self.window makeKeyAndVisible];
return YES;
}
Following is the output :
You can download code example here
you need separate navigation controller for each tab bar view controller & then you can add UIBarButtonItem on each navigation controller.

UInavigatonbar items

I add a uinavigationcontroller as a subview on a uiviewcontroller (rootviewcontroller of aap) and this view controller is a subview as a rootviewcontroller on self.window.
in Appdelegate I use:
self.window.rootViewController = self.rootVC;
and in viewdidload of my RootViewController I wrote:
[self.navVC.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
[self.navVC.navigationController.navigationBar setTranslucent:YES];
self.navVC.navigationController.navigationBarHidden = NO;
[app.rootVC.view addSubview:self.navVC.view];
self.navVC.delegate = self;
Here, navigationbar is displaying but I am unable to display uibarbutton items on navigation bar. Example code:
leftBtn = [[UIBarButtonItem alloc] initWithTitle:#"Left" style:UIBar
flxBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBa
rightBtn1 = [[UIBarButtonItem alloc] initWithTitle:#"Right" style:UIBar
rightBtn = [[UIBarButtonItem alloc] initWithTitle:#"Right" style:UIBar
[self.navVC.navigationItem setLeftBarButtonItem:leftBtn];
[self.navVC.navigationItem setRightBarButtonItems:#[rightbtn1,rightbtn,flxBtn]];
Can somebody help me out to resolve my problem. How can I change appearance of navigationbar and how the uibarbutton items can be displayed ?
You will simply need to change your
[self.navVC.navigationItem setRightBarButtonItems:#[rightbtn1,rightbtn,flxBtn]];
to
[self.navigationItem setRightBarButtonItems:#[rightbtn1,rightbtn,flxBtn]];
and
[self.navVC.navigationItem setLeftBarButtonItem:leftBtn];
to
[self.navigationItem setLeftBarButtonItem:leftBtn];`

Add a navigationbar with a back button to the app [duplicate]

This question already has answers here:
how to create back button in navigation bar
(3 answers)
Closed 9 years ago.
I need a step by step tutorial to add a navigation bar including a back button to my project.
My rootViewController defined in the AppDelegate is the LoginViewController. After successfull login it goes to the MainView and then to the SingleView
How would I add a navigation bar and a back button? This is the last thing I need for my app. I already tried many things
for example:
everything in the viewDidLoad method
First Try
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem = anotherButton;
Second Try
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem = anotherButton;
What exactly do I write into the RootViewController and what do I write into the other UIViewController to get a butotn?
Edit 2 after Popeye's suggestion
//Appdelegate.m
LoginViewController *viewController = [[LoginViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:viewController];
[navCon setNavigationBarHidden:NO];
self.window.rootViewController = viewController;
//LoginViewController.m
[self.navigationController setNavigationBarHidden:NO];
ToDoListViewController *viewController = [[ToDoListViewController alloc] init];
viewController.stringUserId = //userid//;
[self presentViewController:viewController animated:NO completion:nil];
//ToDoListViewController.m
[self.navigationController setNavigationBarHidden:NO];
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = #"Back";
UINavigationItem *right = [[UINavigationItem alloc] initWithTitle:#"Hello!"];
right.leftBarButtonItem = myBarButtonItem;
[self.navigationController.navigationBar pushNavigationItem:right animated:YES];
still, no buttons!
Best way is add NavigationController to your rootViewController and you also able to hide and show NavigationBar by using following code
yourNavigationController.navigationBarHidden:YES/NO;
you can add rootViewController with navigationController by
LoginViewController *loginVC = [[LoginViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:loginVC];
navCon.navigationBarHidden = YES/NO;
self.window.rootViewController = navCon;
.
.
.

UINavigation bar back button not responding

I have a cameraview from which I'm pushing another view of with TableViewController. I can push the second view from my main view by using navbar controller. But the back button from the second view which is table view does not respond. I can't figure out why.
Thanks in advance.
#WrightCS Thank you for your immediate response.
In my app delegate.
ViewController *vc= [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController :navController];
[self.window makeKeyAndVisible];
return YES;
Right now I'm using a temporary button to push the view.
-(IBAction)testButtonPressed {
TableView *tableVC = [[TableView alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:tableVC animated:YES];
}
There are a few reasons your back button may not work:
You are using a custom UIButton and the frame/bounds are not correct you can use [yourButtonName sizeToFit]
to size it properly.
You are adding a button programmatically and not adding a click event:
[yourButtonName addTarget:self action:#selector(yourButtonNameTapped:) forControlEvents: UIControlEventTouchUpInside];
Don't forget your event.
...
-(void)yourButtonNamedTapped:(id)sender
{
...
}
docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html
If you are using a UINavigationController, back will automatically show up when you use [self.navigationController pushViewController:viewControllerName];
-(IBAction)testButtonPressed {
TableView *tableVC = [[TableView alloc] initWithStyle:UITableViewStylePlain];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
[self.navigationController pushViewController:tableVC animated:YES];
}
I think it will be helpful to you.
- (void)viewDidLoad
{
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonDidClicked:)];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
}
-(void)backButtonDidClicked :(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
It should work for you!!

Resources