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
}
}
Related
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.
I have 2 view controllers in my app that user can navigate from one to another.
In my first view controller I dragged from an icon in the toolbar to the second view controller to setup a segues and selected “show” from the popup.
So far no issue, I can click on the icon in the toolbar and will take me to the second view controller without any problem.
However I have also created an action from that icon using drag and drop so now I have something like this
#IBAction func setting(sender: UIBarButtonItem) {
println("Test")
}
The problem I have is the setting action is not getting fired when I click on the toolbar icon, however it will navigate to the second view controller without a problem.
Reason I want to call the setting function is to perform something prior to moving the second view controller.
Do you see any problem with the way I have implemented this?
As you are using segue from your storyboard, then that segue is triggered before the button action.If you want to perform some action on your button click, then you have to manually call the segue like this
[self performSegueWithIdentifier:#"YourSegueName" sender:sender];
then instantiate your segue like this
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"YourSegueName"])
{
}
}
I wanted to know if there's a way to change the Back link in my app.
My app is built like this : The first view (Oil) is a tableView inside a Navigation Controller.
There's 3 button at the bottom, Oil, Property, Application. So if I tap on the first one (Oil) it does'nt change anything as we are already on this view. If I tap on the second button which is Property the view goes on an other Navigation Controller and an other tableView is displayed.
But Now if I tap on a cell in my tableview Property it brings me back to the Oil view. I perform a Segue, and with this segue I set my Navigation Bar Title as the Property name.
PropertyViewController.m :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"PropDetail"]) {
OilViewController *test = segue.destinationViewController;
test.propName = propertyName;
}
}
OilViewController.m :
if (_propName) {
self.title = _propName;
/* Imaginary Code
back.link = PropertyView;
*/
}
else {
self.title = #"
}
This works If I go to Property View and then tap on a property I "go back" to the Oil View and the title is for example Dream. But the problem is that when I click on the Back buttonit brings me back to the Real Oil View. Instead of this I want the back button to go back to the PropertyView.
Can I change that in the code and how ? Thanks. I know I could just duplicate my code, or built my app in an other way, but I'm almost done and I don't want to start from scratch again.
I hope I was clear, Thanks !
You can use unwind segue to back to any view controller you want. Check out this question
What are Unwind segues for and how do you use them?
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 an application with several view controllers controlled from a tab bar controller. From one of these view controllers I want to (on clicking a button) segue to another view controller and retain the tab bar at the bottom of the segued to view.
I've used
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"newView"]){
UIViewController *controller =segue.destinationViewController;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
}
}
This works fine except the tab bar is missing from the segued to view (a placeholder shows for it in the storyboard, but it doesn't show up when the app is run)
I've also tried replacing
[self presentModalViewController:controller animated:YES];
with
[self presentViewController:controller animated:YES completion:nil];
but that doesn't work either.
A bit of debugging shows that for the segued-to view controller, the tabBarController property is set to nil.
Is there anyway to retain the tab bar in the segued-to view controller?
From your explanation, I don't think you want a modal controller. Modal is used to overlay, rendering your tab bar useless. From your storyboard, select your segue and select push, not modal.
Push vs Modal (Note the tab bar):