Reputation: 51
So I have two scenes...the first one has 2 input text fields. I enter information into the first field before proceeding to the next scene. The next scene generates information which I need to use back in the first scene to enter in the second text field. Every time i segue back to the first scene the first string of information is cleared. I cant simply use the back button, i need to use prepareforsegue so im curious if there is any way to input my text information in scene 1, segue to scene 2 (generate other information) and segue back to scene 1 without losing the information previously entered?
I hope this is enough information. Thanks in advance.
EDITED
Here is some of my code.
inputMilesViewController.h (FIRST VIEW)
@property (weak, nonatomic)IBOutlet UIButton *myTodayButton;
(myTodayButton segues to dvc - prior to segue myTodayButton.titleLabel.text equals "TODAY")
dvc.m (SECOND VIEW)
- (IBAction)myNewSelectDate:(id)sender {
inputMilesViewController *classInstance = [[inputMilesViewController alloc] init];
[classInstance changeButtonText:[_myNewDatePicker date]];
[self dismissViewControllerAnimated:YES completion:nil];
}
inputMilesViewController.m (FIRST VIEW)
-(void) changeButtonText:(NSDate*) dateForInput{
NSLog(@"The button is titled %@", self.myTodayButton.titleLabel.text);
NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE, MMM d,''yy"];
NSString *formattedDateString = [formatter stringFromDate:dateForInput];
... ... (here is where I try to change the text of the button to read as formattedDateString however the NSLog indicates that the button text is now (null).
Upvotes: 1
Views: 632
Reputation: 1197
NEW EDIT
Add the import at the top of inputMilesViewController.m
// At the top with the other #imports
#import "dvc.h"
also I recommend learning Object Orientation before going into iOS. Its a key part of it. Its not hard, but definitely essencial.
Add this code to your project.
In inputMilesViewController.m
add
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
[segue.destinationViewController setClassInstance];
}
In dvc.h
add this:
@property (weak, nonatomic) inputMilesViewController *classInstance;
In dvc.m
add this:
@synthesize classInstance = _classInstance;
- (IBAction)myNewSelectDate:(id)sender {
// Removed this line
//inputMilesViewController *classInstance = [[inputMilesViewController alloc] init];
[self.classInstance changeButtonText:[self.myNewDatePicker date]];
[self dismissViewControllerAnimated:YES completion:nil];
}
So as I said in the comments, what this code will do is give your string to the original fisrtViewController, and not a new one that you just created. To achieve this you create the property classInstance
and make it a reference to the original viewController.
Quick tip: Make you class names start with uppercase so you can differentiate them from variables. So in this case: InputMilesViewController
and Dvc
.
Also it seems to me you are not very familiar with object oriented programming. I recommend you try to learn more about that and about the MVC model. Both are key part of programming for iOS.
If it still doesnt work tell me.
Hope it helps.
Upvotes: 1
Reputation: 535945
I agree with Pedros. It sounds like you're doing this wrong. You may be using a segue to get from vc1 to vc2 and then another segue to get from vc2 to vc1 - and that is wrong! It's wrong because a real segue creates a new view controller instance. So you are not coming back to the same vc1 you left earlier; instead, you're making a completely new vc1 and going on to that! That's why the fields are empty; it's a totally new clean view controller.
Instead, what you want to do is do a true segue out and an "exit" (or "unwind") segue coming back. This is a new iOS 6 feature. I have some examples here:
https://github.com/mattneub/Programming-iOS-Book-Examples
In particular, look at the four examples whose names begin "ch19p560" and "ch19p561". These show how to hand information from vc1 to vc2 as you segue, and then how to hand information from vc2 back to the same vc1 instance as you unwind back. And they illustrate unwinding to various degrees (e.g. jumping back several view controllers) and in various situations.
Upvotes: 1