I am using following code to show DashBoardContainerVC in app delegate:
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
UINavigationController *svc = [storyboard instantiateViewControllerWithIdentifier:#"DashBoardNavigationController"];
self.window.rootViewController = svc;
Code for navigation bar button is:
self.navigationItem.leftBarButtonItem = [self leftMenuBarButtonItem];
- (UIBarButtonItem *)leftMenuBarButtonItem {
return [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"menu-icon"] style:UIBarButtonItemStylePlain
target:self
action:#selector(menuButton:)];
}
menuButton action fails to call in this case. But when i am using the code like the one below, it works fine:
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
DashBoardContainerVC *svc = [storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([DashBoardContainerVC class])];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:svc];
self.window.rootViewController = nav;
So what's issue in my first code. Any suggestions.?
I had same issue with storyboard, Your code has no issue, There is some bug with storyboard, Just delete your view/scene form storyboard and add them again.
Related
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];
SecondViewController *testAppViewController2Obj =[[SecondViewController alloc]initWithNibName:#"TestAppViewController2" bundle:nil];
[self.navigationController pushViewController:testAppViewController2Obj animated:YES];
ContainerViewController *container = [[ContainerViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:container];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 65)];
container.navigationItem.title = #"Frames";
self.window.rootViewController = nav;
i hope it works
In order to push a new view controller you should first create a view controller, VC1, link it with a navigation controller and then link the navigation controller to the rootViewController in the AppDelegate
#AppDelegate
FirstViewController *VC1 = [FirstViewController new];
UINavigationController *nVC = [UINavigationController alloc] initWithRootViewController:VC1];
self.window.rootViewController = nVC;
Then when VC1 gets pushed, you can create another view controller and push it like this:
#Now in VC1
SecondViewController *VC2 = [SecondViewController new];
[self.navigationController pushViewController:VC2 animated:YES];
Currently I develop with a StoryBoard for the iPad. Additionally I add a ViewController.xib file with a view. Now I want to back button from xib to storyboard. My code is below but this is not working.
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.navigationController pushViewController:initViewController animated:YES];
You can present the View Controller from a storyboard scene, using
- presentViewController:animated:completion: (e.g. for a modally scenario)
ViewController *vc= [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:navController animated:YES completion:NULL];
then, you can add a custom back button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Back", #"Back from ViewController.XIB") style:UIBarButtonItemStylePlain target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = backButton;
and dismiss it:
- (void)backAction:(id)sender{
[self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
}
Forget about the navigationController.
In your active view controller init the new view controller like this.
UIViewController *initViewController = [[UIViewController alloc]
initWithNibName:#"YourNibFile" bundle:[NSBundle mainBundle]];
[self presentViewController:initViewController animated:YES completion:nil];
And to dismiss.
[self dismissViewControllerAnimated:self completion:nil];
I'm working on an app and I face a problem with UINavigationcontroller.
First in the app delegate I check if the user is logged in, if so I take him to Main screen, if notI take him to the login screen.
This is my code:
UINavigationController *diabetesNavigationController = [UINavigationController alloc];
LoginViewController *loginPage = [[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:nil];
MainViewController *mainPage = [[MainViewController alloc]initWithNibName:#"MainViewController" bundle:nil];
if ([DataStore instance].userIsLoggedIn)
diabetesNavigationController = [diabetesNavigationController initWithRootViewController:mainPage];
else
diabetesNavigationController = [diabetesNavigationController initWithRootViewController:loginPage];
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
[[UINavigationBar appearance]setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance]setTitleTextAttributes:navbarTitleTextAttributes];
[self.window setRootViewController:diabetesNavigationController];
The problem when the user logs out, he goes back to loginscreen but without the navigationcontroller.
I made something in MainViewController, which is this:
-(void)viewDidAppear:(BOOL)animated
{
self.navigationItem.title = #"Diabetes";
UIBarButtonItem *settingButton=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"burger.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(onBurger:)];
self.navigationItem.rightBarButtonItem = settingButton;
self.navigationItem.leftBarButtonItem.enabled = YES;
}
So when I logout the user, with this code:
LoginViewController *loginPage = [[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:nil];
[self.navigationController setViewControllers:[NSArray arrayWithObjects:loginPage, nil] animated:YES];
I get to the login screen but without Navigationcontroller. I tried to set Hidden property to NO in all view delegates, but it still has this issue.
You can try this:
UIWindow *window = [UIApplication sharedApplication].keyWindow;
LoginViewController * loginPage = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: loginPage];
[window.rootViewController presentViewController:nav animated:YES completion:NULL];
Let me know if it works.. :)
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;
.
.
.