I have the following in Interface Builder:
The top left is my main view controller where I have 2 buttons that have segue to two UIViewControllers. These two UIViewControllers are linked with the Tab Bar Controller. However, how could I make those 2 buttons to link to specifically to one/other views? Right now it's connected specifically, but it (or something else) causes the bar tab not show up.
Is it the problem that I don't have the Tab Bar Controller connected to the main view?
Yes, you're right that the problem occurs because the tab bar controller needs to be the destination of the segues. Fix it like this:
In IB, erase the segues from the two buttons and create two new ones, one from each button to the tab bar controller. Give each one an identifier, like buttonA from one button and buttonB from the other.
In the view controller, implement prepareForSegue for each segue understanding that the destination is a tab bar controller and that each segue requires a different tab selection...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"buttonA"]) {
UITabBarController *tabBarController = (UITabBarController *)segue.destinationViewController;
tabBarController.selectedIndex = 0;
}
if ([segue.identifier isEqualToString:#"buttonB"]) {
UITabBarController *tabBarController = (UITabBarController *)segue.destinationViewController;
tabBarController.selectedIndex = 1;
}
}
That's not quite how the tabBarController works.
I can see your initial view controller is the one on the top left, and it pushes either of the other two on the right on to the navigation stack if you push a button. But in your current setup, at no point does the tab controller itself get pushed on to the stack.
Instead, you would want to have your initial view controller push the tab bar controller on to the stack, through a button or otherwise, and the tab bar controller will display your other two view controllers as its setup to do.
Related
I have a view controller that has a navigation bar. It is the root view controller of the navigation controller. I have another view controller that can be segued from a button on the first controller. Up to this point, everything works fine; there are navigation bars on both view controllers.
However, from the second view controller, I want to be able to segue back to the first controller. When doing this, it removes the navigation bar from both of the view controllers.
How can I get the navigation bar on both view controllers with buttons as transitions? Thanks!
Implement below method in your both viewcontroller,
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
You may hiding navigationbar somewhere.
Try this in storyboard:
Select the navigation controller -> Attributes Inspector -> Under Navigation controller and Bar Visibility leave the "Show navigation bar" blank
I have several ViewController chained together with segue in a simple hierarchy.
In each of them :
[self performSegueWithIdentifier:#"myController" sender:myVar];
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"myController"]){
MyController *controller = (MyController *)segue.destinationViewController;
controller.myVar = sender;
}
}
I'm looking for the simplest way to integrate a top navigation bar with a back button. I know I can use something like this in viewWillAppear :
UINavigationBar *myNav = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, x, y)];
[self.view addSubview:myNav];
But in this case the navigation bar hides a part of the view (like a "position:absolute" would in css).
I can add a Navigation Controller which solves the problem above but from what I understand this will make my segue useless, which I don't want.
This is exactly what UINavigationController is for. Your first view controller (called the root) is embedded in a navigation controller. Additional view controllers are then pushed onto the navigation controller.
UINavigationController automatically manages the navigation bar on the top and creates the back button for you.
Once you have embedded your view controller in a navigation controller, you can use push segues.
I am developing one i pad application using story board.In my storyboard i have tab bar controller and another 4 view controllers are connected to tab bar controller are tab items.I have one button in the first view controller.If i click that button i need to display 4th view controller.If i give a direct segue from first view controller to 4th view controller using
[self performSegueWithIdentifier:#"SegueIdentifier" sender:nil];
the 4th view controller is appeared but the tab bar display at the bottom is disappear.
You need to do something like this when the button is pressed...
[self.tabBarController setSelectedIndex:3];
This will tell the tab bar controller to change the selected tab.
Try use it->
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"SegueIdentifier"]) {
[self.tabBarController setSelectedIndex:3];
}
}
I have a base nav controller which has a tab bar controller nested inside. inside that tab bar controller is another nav controller nested in it. If I'm in the child nav controller I can pop to root view in one of the views but it only takes me to the root view of the CHILD nav controller. Is there a way to pop back to the first nav controller?
If you are using storyboard then you can use Unwind segue approach which always works for me
Just Control + drag from "Pop to Main Screen" UIButton to Exit (green button at bottom of scene) select unwind method declared in first screen with title "Main" e.g.
-(IBAction)customUnwind:(UIStoryboardSegue *)sender
{
NSLog(#"Unwind successful");
}
Try this,
You can pop to root view from the child nav controller by
UITabBarController *tc = (UITabBarController *)self.navigationController.parentViewController;
[tc.navigationController popToRootViewControllerAnimated:YES];
I'm working on an iPhone app whose main navigation happens through a tab bar. However, it opens with a welcome screen that does not show the tab bar, but rather has two buttons to go to the two most common tabs. One goes to the first tab, so in my storyboard I added a segue to the main tab bar controller. This works great.
But the second button goes to the third tab, so I added a segue directly to that screen in my storyboard. This results in the tab bar not being shown. All my research into this has been obfuscated by the dozens of people who want to retain the tab bar, not create it, so I'm a bit lost on what to do. Thanks!
For the second button, you should also segue to your tabBarViewController and call
[self setSelectedIndex:2]; // If you want to show the third tab
in viewWillApear method in your tabBarViewController
If you are using storyboard segue,
add this method in your first View Controller
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"yourSegueName"])
{
MyTabBarVC *destinationVC = segue.destinationViewController;
destinationVC.selectedIndex = myIndex;
// myIndex's value depends on which button you pressed
}
}