What is the right way to go back a performed segue? - ios

Basically I have a view controller that pushes to the next view controller with [self performSegueWithIdentifier: sender:]; and in the prepareForSegue I wanted to change the VC's title to "Back" so that in the next VC the back bar button item will be "Back" instead of the initial VC's title.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
self.title = #"Back";
}
Now the problem is that when I return back to the first VC by tapping on the Back button, I have to change the title back to the initial title or else the title will stay as "Back".
If only there was a method like prepareForSegue so that I can get the destination VC and change it's title...
Another example is that I have a storyboard of 3 view controllers like so:
I've hidden the navigation controller to use my own button to go back the segue. While this way works, but it seems rather odd that I have to add 2 more segues just to return back to the first VC...

In your storyboard, drag a Navigation Item onto your first view controller. Set the Title to your view controller's title, and Back Button To "Back"
Alternatively, to do this programatically, create a UIBarButtonItem with nil action and target, and assign to navigationItem.backBarButtonItem (make sure you do this in the view controller you are coming from)

in the methodprepareForSegue:, you can get the destinationViewController by segue.destinationViewController. i think this is what you want.

Related

Change Back Link of Navigation Controller

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?

How to go from one view controller to another view controller through tab bar controller when Click a button in the first view controller

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];
}
}

Programmatically dismiss popover from view embedded in navigation controller

I have a view controller. When I press a button in it, a popover controller with a uitableview shows up. I select a row, which shows another view with some controls in it. When I press a button that says "Save Item", I want it to dismiss the popover. How do I do this?
Here's what I've tried:
Using the delegate and protocol pattern. This hasn't worked since in order to push another view inside my tableview, the whole thing must be embedded in a navigation view controller, so when I segue, it segues to a nav controller, not the tableview which I could set the popover delegate for.
Adding my main view as a member of the view I want to dismiss from. I don't know why this doesn't work.
The Hard Clean Way
There are four view controllers in the story, plus a popover controller. I will call the three view controllers "main view controller", "nav", "vcA", and "vcB". As I understand it, "nav" is the initial content view controller of the popover and has "vcA" as its root view controller.
main view controller -> popover controller -> nav -> vcA -> (later) vcB
When you present the popover from your main view controller, you keep a reference to the popover controller. This is what makes dismissing possible, as you know.
When you create the Save button, you make its target the main view controller and its action a method in the main view controller. You will have to set this up in code; it cannot be configured from a storyboard because you cannot form an action from one scene to another. (You are able to do this because you started out with a reference to nav and vcA when you configured the popover controller initially. Thus you can hand vcA a reference to self, the main view controller. If necessary, you can then pass this reference down the chain from vcA to vcB as vcB is summoned and pushed onto the navigation stack.)
Now the user taps Save, your main view controller's method runs, and it uses its reference to the popover controller to tell it to dismiss.
The Easy Dirty Way
The heck with all that. The main view controller registers for an NSNotification. The Save button posts that NSNotification. Done. :)
The Middle Way
You could set the whole chain up in your storyboard using a popover segue, and do the dismissal through an Unwind segue matched by an unwind method back in the main view controller. I never think of this initially, because I don't like popover segues very much. But it does work.
This is how I solved my problem (sorry for the bad english):
First, Create a property of UIStoryboardPopoverSegue in the VcA and set it from the main view controller.
Nav -> VcA_ViewController
#property (strong, nonatomic) UIStoryboardPopoverSegue *popupSegue;
Then, in the Main View Controller prepareForSegue set the property:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"your segue from the mainview to the navigation"]) {
UINavigationController *navigationController = (UINavigationController *)c;
VcA_ViewController *vcA = (VRPointOfInterestsFiltersViewController *) navigationController.topViewController;
vcA.popupSegue = (UIStoryboardPopoverSegue*)segue;
} }
Now, from the VcA controller you can have the dismiss button
- (IBAction)dismissPopoup:(id)sender {
[self.popupSegue.popoverController dismissPopoverAnimated:YES]; }
Don't forget to link the popOverSegue from the MainViewController to the NavController.
Hope it helps!

Load specific view in Navigation View Controller

I have a tab bar controller with 3 view controllers and one navigation view controller in it.
That navigation view controller has a table view controller and detail view.
In my first view controller (first tab) there are three buttons. I want the first button to load the detail view in the navigation controller. (as seen on image below)
Storyboard
How can i achieve this ?
I have tried calling the method that performs the segue in LocationsTableViewController, but that gives me the error "Receiver () has no segue with identifier 'addLocation'". Although a segue "addLocation" certainly exists.
Method connected to button in StartViewController:
- (IBAction)addLocationView:(id)sender {
LocationsTableViewController *LTVC = [[LocationsTableViewController alloc] init];
[LTVC addLocation];
}
LocationsTableViewController:
-(void)addLocation
{
[self performSegueWithIdentifier: #"addLocation" sender: self];
}
Your "addLocation" segue is from the Locations Table View Controller to the Add Location View Controller, so the segue exists on the LocationsTableViewController. That is correct. However, if your current view controller isn't LocationsTableViewController, it doesn't make sense to call that segue.
You have a few options:
Storyboard Option #1: Make a new segue from StartViewController to AddLocationViewController, and perform that segue in addLocationView:.
Storyboard Option #2: Make a new segue from the button to the AddLocationViewController, and remove your IBAction. The storyboard will automatically call that segue when the button is clicked.
Programatic Option: Instantiate a new AddLocationViewController and push it onto the navigation stack: [self.navigationController pushViewController:[[AddLocationViewController alloc] init] animated:YES].

Modal segue to show tab bar?

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
}
}

Resources