Main screen in my app has 2 buttons. When click button 1, show ViewController1. When click button 2, show ViewController2. ViewController1 has 2 bar items. ViewController of each bar item has Back and Done button. Back is back to main menu, Done is used to hide keyboard. I want to control 2 these buttons.
I have 2 directions:
Add Navigation Controller at main screen. It has Back button. Button Done is implemented in ViewController of each bar item. In this case, button Done works not good when change tab bar. I loged and see that, first click in tab bar, it works correct, but click Item1->Item2->Item1, button Done in Item1 this time not correct, because it is still button Done in Item2 Controller.
How to fix in this case?
I hide Navigation Controller in main screen, implement Navigation Controller in Controller of each Tab Bar. In this case, button Done works good, but button Back can't move to main screen when click on it.
How to move to main screen in this case?
Code in AppDelegate.m:
UIViewController *cont = [[VCMainMenu alloc]initWithNibName:#"VCMainMenu" bundle:nil];
self.navController = [[UINavigationController alloc]initWithRootViewController:cont];
[self.window setRootViewController:navController];
Code in MainMenu.m, ViewDidLoad:
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
Code in MainMenu.m, buttonClick:
self.navigationController.navigationBarHidden = YES;
[self.tab setSelectedIndex:0];
[self.navigationController pushViewController:self.tab animated:YES];
Button Done in each class:
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(btnDonePressed:)];
self.navigationController.topViewController.navigationItem.rightBarButtonItem = btnDone;
btnDone.enabled = TRUE;
btnDone.style = UIBarButtonSystemItemDone;
Thanks.
To hide the Keyboard you need to know which textView is firstResponder. You have to implement a dismisskeyboard method for each of your scenes.
Also, why are you coding all the navigation stuff? It would be much easier implementing them via storyboards and segues.
I hide Navigation Controller in main screen, implement Navigation
Controller in Controller of each Tab Bar. In this case, button Done
works good, but button Back can't move to main screen when click on
it. How to move to main screen in this case?
To do that, use
[self.navigationController popViewControllerAnimated:YES];
And as a sidenote #Marcal has a point, I think it might be better if you use storyboards and segues
Related
In iOS 7 Apple introduced new transition when you push view controller on top of another view controller. The transition comes with nice animation and back gesture. The back button displays the title from previous view controller which is good for accessibility:
You know where you are by looking on a title. You know that title is not intractable because it has different to tint color, usually, black.
You know where you come from with the back button label.
Unfortunately, our design require to remove navigation bar label because sometimes it is too long and it move navigation bar title to the right a little.
Here is how our design should look and work during the transition:
We removed the title from the first view controller in viewDidLoad of the first view controller (the one which is behind):
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
Now our transition has status bar background color problem:
Status bar change background color to grey during transition. Both view controllers have white status bar background.
Pushing second view controller:
SecondVC *svc = [sb instantiateInitialViewController];
[self.navigationController svc animated:YES];`
The solution is to remove this line from our code:
[[UINavigationBar appearance] setBackgroundColor:Colour_White];
In first ViewController -
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
UIBarButtonItem *btn=[[UIBarButtonItem alloc]initWithTitle:#"" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.backBarButtonItem=btn;
}
Note: Problem solved.
Here comes the story. I am using RevealViewController in my project. I am also hiding the navigationBars:
[self.navigationController setNavigationBarHidden];
My project can be seen in the picture below and the "menuButton" is implemented in the tabBarViewController.
Since the navigationBar is hidden due to my interface looks, all tabViews (HomeViewController) will not show the menuButton and the navigationBar as supposed to. I am not using panGestureRecognizer to trigger the menu aswell.
This means I have a problem to trigger the menu via a normal button in HomeViewController. The menuButton-event is placed in tabBarViewController.m:
_menuButton.target = self.revealViewController;
_menuButton.action = #selector(revealToggle:);
So I tried to call a method from HomeViewController to fire the button in tabBarViewController like this:
HomeViewController.m
- (IBAction) onMenuClicked: (id)sender{
tabBar = [[tabBarViewController alloc] init];
[tabBar setupMenu]:
}
tabBarViewController.m
-(void) setupMenu{
[_realMenuButton sendActionForControlEvents:UIControlEventTouchUpInside];
[_realMenuButton addTarget:self.revealViewController action:#selector(revealToggle:) UIControlEventTouchUpInside];
}
In this example I tried to make the realMenuButton and normal UIButton. Ive also tried as a UIBarButtonItem just to trigger the #selector(revealToggle:) But nothing happens in the app when I try to trigger the button from HomeViewController.
Not sure how I shall make this work. Any other Ideas or tricks? Please be specific if so! Regards
Yes, it will still work.
SWRevealViewController is just a subclass of a UIViewController, so you can use it at any point in the app:
By calling presentViewController:animated at some point.
By using it in a navigation stack etc.
Note that you can add gestures from SWRevealViewController to its content view controllers, which will alter the behaviour of used in a navigation view controller, but that's to be expected, and you still have full control over its behaviour.
Edit
The UI structure of your app is still not clear to me - it looks like you're trying to call revealToggle on an instance of SWRevealViewController when the VC in view is infact HomeViewController? How would this work, when SWVC is not even in view?
My best guess is that your UI structure should be as follows:
TabBarController --->(root)UINavigationController --->(root)SWRevealViewController.
Then, on your SWRevealViewController, set HomeViewController as the front view controller, and the TableViewController as the right or left view controller.
Do you mean like this?
it is possible. you can set the menu button in your tabBarController.m, like this :
UIBarButtonItem *menu = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"menu.png"] style:UIBarButtonItemStylePlain target:revealController action:#selector(revealToggle:)];
self.navigationItem.leftBarButtonItem = menu;
self.delegate = self;
For me, my initial view controller is the login screen (obviously I don't need reveal any VC here...). then when user tap the login button,
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:yourRootVC];
LeftMenuViewController *leftMenuVC = [[LeftMenuViewController alloc]init];
SWRevealViewController *revealController = [[SWRevealViewController alloc]initWithRearViewController:leftMenuVC frontViewController:nav];
revealController.delegate = self;
[self presentViewController:revealController animated:YES completion:nil];
I've tried it and it should work as normal. Even it isn't initial view controller
I am trying to add a bar button to my iOS app and can't get it to show up. I can see the bar in the Navigation Item's view hierarchy by setting a breakpoint. If it helps, I chose 'Embed Navigation Controller'. Any idea what's going on?
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(choosePreferredTerm:)];
[self.navItem setRightBarButtonItem:item animated:YES];
Here is the connection in IB
This is what the embedded Navigation Controller looks like:
This is what it looks like on the sim:
Try this rather than using custom outlet "navItem" also insure your application has a navigation controller in place
self.navigationItem.rightBarButtonItem = barButton;
I had two Table View Controllers and they are connected each other by "segues" in Storyboard. Then I've cut the "segues" and insert another controller. And connection now is not through "segues". To connect the views I'm using the code:
UIStoryboard *storyboard = self.storyboard;
OptionsViewController *options = [storyboard instantiateViewControllerWithIdentifier:#"OptionsViewController"]
[self.navigationController pushViewController:options animated:YES];
And now when I'm clicking a "Back" button in navigation bar my program crashes. How to fix it?
Console shows (After view loaded, before I've pressed the "Back" button.): "nested push animation can result in corrupted navigation bar"
"Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted."
After I've pressed the "Back" button: "terminating with uncaught exception of type NSException"
Navigation Controller is the initial controller in my storyboard, but the Views are not connected each other.
FIXED: I've just set the "animated:NO" at the end of my code.
Check that to calling pushViewController before viewDidAppear is not safe. So you should create your code according to that.
Try modifying line to
[self.navigationController pushViewController:options animated:YES];
Update
If you don't care about normal back button, try below code.
- (void) viewDidLoad
{
// ADD BELOW CODE IN viewDidLoad ALONG WITH REST OF YOUR CODE
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#”back”
style:UIBarButtonItemStyleBordered target:self action:#selector(backBarButton:)];
self.navigationItem.leftBarButtonItem = backButton;
}
- (void) backBarButton:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
You can play around with style to set your desired button style.
I am navigating to Tab bar controller with 6 tabs from a RootViewController... I have created back button method in left navbar item which pops back all tab views to RootViewController... Problem arises when i try to pop the 5th or 5th tabview.. the back button doesnt work at all in MoreViewController, 5th tab and 6th tab...
Here's what works:
1)Pops back to RootView from 1 to 4 tabs
2)Pops back to RootView from More View (only first time it works)
3)Pops back to MoreView from 5th/6th View (only first time it works)
Here's what doesnt work:
1)Doesnt pop back to RootView from MoreView
What am i doing wrong?
- (void)viewDidLoad
{
...
self.tabBarController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonSystemItemDone target:self action:#selector(goBack)];
...
}
Code for goBack method written in each tab
-(void)goBack{
[self.navigationController popToRootViewControllerAnimated:NO];
}
Try this.. May be it will help you
NSArray *viewContrlls=[[self navigationController] viewControllers];
for( int i=0;i<[ viewContrlls count];i++)
{
id obj=[viewContrlls objectAtIndex:i];
if([obj isKindOfClass:[rootController class]])
{
[[self navigationController] popToViewController:obj animated:YES];
return;
}
}
I figured out what was the problem and so i am going to answer my own question... I was writing popToRootViewController in each tab.. and so when i enter 5th tab and press back, it would come back to more view tab and forgets about the root view.. So i deleted popToRootViewController from each tab and wrote the code for popping the views at the time of Tab bar declaration in the Root View page... eg. Tab bar declaration, then setting tab bar nav button for going back which calls some method... and in that method i write popToRootViewController...