2

I'm trying to move an NSString between two View Controllers and after searching around all convoluted ways, the easiest and most straight-forward way I want to get used to was to write an initWithName function in the Receiving VC and calling it in the Sending VC. It does move it successfully but I want it to get executed before ViewDidLoad loads the textViewer so that it shows right after the tab button is pressed. Here's the code from the sending VC:

- (void)textViewDidEndEditing:(UITextView *)textView
{   
    if ([textView.text isEqualToString: @""]) {
        textView.text = @"*Paste the machine code in question here*";
    }
    SecondViewController *theVCMover = [[SecondViewController alloc] initWithName: textView.text];
    [self.navigationController pushViewController:theVCMover animated:YES]; //Is this really necessary if I'm not going to segue it directly, I'm just waiting for the user to press the next tab
    gotItLabel.text = @"Got it! Ready for action...";
}

And here's the code on the receiving VC:

 - (id)initWithName:(NSString *)theVCMovee {
    self = [super initWithNibName:@"SecondViewController" bundle:nil];
    if (self) {
        rawUserInput = theVCMovee;
        CleanerText.text = rawUserInput;
    }
    return self;
} 
- (void)viewDidLoad {
        [super viewDidLoad];
    CleanerText.text = rawUserInput;
        NSLog(@"Got the other tab's text and it's %@ ", rawUserInput);

}
Seamus Campbell
  • 17,816
  • 3
  • 52
  • 60
  • `viewDidLoad` is called after the view is loaded, and the view is loaded the first time something tries to access it, like calling `viewController.view`, or having a parent view controller laying out its children (or the window displaying its root view controller's view). So as long as you don't try to access the `view` property yourself, and no layout is happening yet on screen, you should be safe to set up all parameters you want before `viewDidLoad` is called (i.e. between the `init…` and the `push…` in your code. – Guillaume Mar 25 '13 at 16:52
  • Are you using Automatic Reference Counting (ARC), and how is `rawUserInput` defined? (Can we see the .h?) – NSGod Mar 25 '13 at 16:54
  • Yeah, using ARC and here you go: "@property (strong, nonatomic) NSString *rawUserInput;" – FrontendSchmacktend Mar 25 '13 at 17:42
  • I also tried moving the string directly using "theVCMover.CleanerText.text = textView.text;" but again, nothing. Does that seem normal or is something wrong with my code? – FrontendSchmacktend Mar 25 '13 at 17:43

2 Answers2

1

Your code is mostly fine, but you'll find that as you have more complex view controllers that you won't necessarily want to write custom initializers to do every bit of property setting. Note that if CleanerText is a UI element that you're loading from your nib, it doesn't help to set CleanerText.text in your init method—it's not loaded until -viewDidLoad is called.

You don't have to do everything in init, though, if you declare properties for rawUserInput or other variables you want to set. You can then just go:

SecondViewController *theVCMover = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
theVCMover.rawUserInput = textView.text;
theVCMover.otherProperty = otherValue;
....

And the rest of your code works the same.

Seamus Campbell
  • 17,816
  • 3
  • 52
  • 60
  • Alright thanks for the tip on the multiple mover bit but then again, how can I load that moved string into the textViewer instantly after the user loads the tab. I can just make a "Load" button and all but I want to learn 'cause this seems quite important to me. – FrontendSchmacktend Mar 25 '13 at 17:40
  • I also tried moving the string directly using "theVCMover.CleanerText.text = textView.text;" but again, nothing. Does that seem normal or is something wrong with my code? – FrontendSchmacktend Mar 25 '13 at 17:42
  • This code is fine. If you're not seeing the text you expect, something else is the issue. `CleanerText` is an `IBOutlet` that is connected in `SecondViewController.xib`? – Seamus Campbell Mar 25 '13 at 18:41
  • Yeah, did that too...here are images of my project files: http://cl.ly/2o0V2Z2L3P16 – FrontendSchmacktend Mar 25 '13 at 19:42
0

You can't (reliably) call methods on an instance until init has finished executing, so this pattern is "safe" and is how it's supposed to work.

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152