Change Back Link of Navigation Controller - ios

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?

Related

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

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.

IBAction not getting fired

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"])
{
}
}

How can i hold the value of a string when the viewController goes to another tableViewController

Well, that question sure sounds weird but i couldn't find a better way to put it.
I m pretty sure its a basic mistake but i m stuck.
I got a main home view controller, there are 2 buttons which leads to 2 different tableViewController.
i will use both of the selections.
But when i get the selected index from one table view and go the the next one, the first one's value become null.
if (tempFromLocationString!=NULL) {
//tempFromLocationString=#"asd";
fromLocationLabel.text=tempFromLocationString;
}
if (tempToLocationString!=NULL) {
toLocationLabel.text=tempToLocationString;
}
this is how i segue from tableView to View controller
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"fromLocationSegue"])
{
NSLog(#"%#",selectionString);
ViewController *vc = [segue destinationViewController];
vc.tempFromLocationString=selectionString;
}
}
and this is how i get the selected cell's value.
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectionString=[fromLocationArray objectAtIndex:indexPath.row];
NSLog(#"%#",selectionString);
}
this is my code. i get temp strings with segues and i m applying these codes in view did load.
all the NSStrings declared in .h files.
the flow is like this;
user enter the app,
select a button,
goes to the first table view controller
select a location,
clicks ok button and goes back to the first view controller with segue ( selectionString)
the label is set to the selectionString appropriately
user click next button,
goes to the select table view
select a location
clicks ok and goes back the first view controller now the second label is set to the selectionString appropriately but now the first one is deleted and the string become null
OK
Your app flow
Case1
User enter the app - Correct
Select a button - Correct
Goes to the First TableViewController select a location -
Correct
Clicks ok button - Correct
and Goes back to the first view controller with segue
(selectionString) the label is set to the selectionString
appropriately - Incorrect
Step 5 is incorrect, why?
Answer - Because you are again pushing the ViewController after the selection in tableViewController, where as your ViewController already exist in the stack, so here instead of using segue, you should just pop the viewcontroller with same reference taken from ViewController.
Case2
User click next button - Correct
Goes to the select table view select a location clicks ok - Correct
and goes back the first view controller now the second label is set to the selectionString appropriately but now the first one is deleted and the string become null - Incorrect
Step 3 is incorrect the same way as Case1.
Answer- Again you are actually not going back, you are going forward, so what happens is you are creating a new instance of ViewController on selection, which doesn't have the previous selected value.
Solution
Create NSString property in each respective tableViewController separately same as you have in ViewController.
When you segue tableViewController from ViewController, assign the property like
TableViewController *vc = [segue destinationViewController];
vc.tempFromLocationString=self.tempFromLocationString;
On selection in tableviewcontroller do the following
self.tempFromLocationString=selectionString;
[self.navigationController popViewController:YES];
Now instead of assigning value in ViewDidLoad in ViewController, do it in ViewWillAppear.
I hope it helps.
Maybe your strings are not NULL when you set your labels.
Try to put a breakpoint before those lines, and check your temp strings
if (tempFromLocationString!=NULL) {
//tempFromLocationString=#"asd";
fromLocationLabel.text=tempFromLocationString;
}
if (tempToLocationString!=NULL) {
toLocationLabel.text=tempToLocationString;
}
If they are not NULL try this:
if (tempFromLocationString && [tempFromLocationString length] > 0) {
fromLocationLabel.text=tempFromLocationString;
}

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

Conditional Segue navigation from UITableViewCell based on response to UIAlertView

My problem seems like a generic problem, yet can't seem to find an answer for it.
I have a situation where when the user taps on a custom UITableViewCell, I would like to display an alert and then based on the response to the alert, either stay on the same view (user selecting cancel) or display another view (if the user selects proceed). And I would like to do this using the storyboard feature & segues.
How would one go about this? Do you have to do this the old fashioned way?
#user, Just create the alertView the old fashion way; I do know of any storyboard feature to do this differently. Where storyboard can help is with the segues. You can call the segues programmatically. With you alert view cancel button you can just return (i.e. do nothing). For the other option, to display another view, you can programmatically call a segue to transition to the desired view. If you don't have the proper segue already defined for some other reason on your storyboard, just create a button out and use that to create the segue and name it. Name the segue by clicking on it in storyboard and use the attributes inspector to give it name (identifier). Then hide the button or put it out of the view. I typically put these type of button on the toolbar and use spacers to keep them out of the view. Here's some sample code:
Call the segue from the alert view delegate like this:
[self performSegueWithIdentifier: #"done" sender: self];
Also implement this method to do any necessary task to prepare for the segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"done"])
{
// [[segue destinationViewController] setManagedObjectContext:self.managedObjectContext];
// [[segue destinationViewController] setSelectedClient:selectedClient];
}
}
You can create segues directly from the startingViewController to multiple destinationViewControllers that can then be "performed" programmatically. You do not need to create any hidden buttons for them, which does seem like a hack.
OK I came up with a solution in keeping with the storyboard that I like.
Example:
My tableview has 2 sections, grouped, and cells are dynamic prototype. Section 0 contains one row/UITableViewCell & I don't want it to segue. Section 1 contains multiple cells that I want to trigger the segue & drill down into the detail.
In Storyboard:
I removed the segue linking the tableviewcell to the destination view controller.
I made a 'generic' segue linking the source view controller directly to the destination view controller.
In the attributes on the segue, I set the identifier ('EditTimePeriod') and set the type to Push (I presume Modal would work just the same).
In the source view controller:
In the prepareForSegue method I handled both the common 'AddTimePeriod' segue I control-dragged from my UIBarButtonItem (Add), along with the 'generic'(vc-->vc) 'EditTimePeriod' segue.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// handle the click of the 'Add' bar button item
if([segue.identifier isEqualToString:#"AddTimePeriod"]) {
TimePeriodViewController* tpvc = (TimePeriodViewController*)segue.destinationViewController;
tpvc.delegate = self;
// database & entity stuff for adding the new one to the mOC, etc
}
// handle the click of one of the 'editable' cells -
if([segue.identifier isEqualToString:#"EditTimePeriod"]) {
TimePeriodViewController* tpvc = (TimePeriodViewController*)segue.destinationViewController;
tpvc.delegate = self;
TimePeriod * newTP = [self.timePeriodArray objectAtIndex:self.tableView.indexPathForSelectedRow.row];
tpvc.timePeriod = newTP;
}
}
Then I implemented the tableView:didSelectRowAtIndexPath method, and put my condition in here. If the selected row was outside of section zero I called the EditTimePeriod segue manually, defining the sender as the selected tableviewcell:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(self.tableView.indexPathForSelectedRow.section!=0){
[self performSegueWithIdentifier:#"EditTimePeriod" sender:[tableView cellForRowAtIndexPath:indexPath]];
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
return;
}
would be nice to code the cell in section 0 so that it is not selectable in the first place!
Hope this helps though.
** and then 5 minutes later I took another look and realized I could just move the data from section 0 into the section header, which is more intuitive and wasn't being used anyway. leaving the design open for a standard segue from each tableviewcell without needing any condition/check. Was a good exercise anyway though :)

Resources