1

I am trying to pass an NSString (message) from ViewController (sender) to MyViewController (receiver).

I created an instance of MyViewController in ViewController called testViewContoller, through which I am sending the NSString using the setTitle: method:

MyViewController *testViewController = [[MyViewController alloc] init];
[testViewController setTitle:message];

Here's MyViewController.h:

- (void)setTitle:(NSString*)title;

Here's MyViewController.m:

- (void)setTitle:(NSString*)title {

_testField.text = title;

}

I am not completely sure as to why this isn't working, but I think it has to do with viewDidLoad loading before setTitle: is called.

Any help you can provide would be greatly appreciated.

Thank you.

jszumski
  • 7,430
  • 11
  • 40
  • 53
ibfocused
  • 19
  • 2
  • You have to instantiate the view controller using the storyboard – William Falcon Apr 24 '13 at 01:52
  • Not sure what do you mean by not working. Put an NSLog for title in setTitle method. – user523234 Apr 24 '13 at 02:45
  • Look up 'prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender' – El Tomato Apr 24 '13 at 04:23
  • The problem with this approach is, when you initialized the method and called the method "setTitle", _testField.text ,an UIKit property, which is not been initialized and setup until viewDidLoad method has been executed. You have to use property to set title and then use that property in your viewDidLoad method as follows _textField.text = self.title; – ldindu Jul 06 '13 at 11:40

3 Answers3

0

Where is _testField created? If it is in viewDidLoad, then it is nil when you call setTitle, which means it is a no op. viewDidLoad is called when the view is added to a window, which means that if you just call init on a view controller viewDidLoad may never be called.

I would define a property on MyViewController that is the desired field text and then have _testField.text = title in viewDidLoad:

MyViewController *testViewController = [[MyViewController alloc] init];
testViewController.testFieldText = message;

- (void)viewDidLoad {
     [super viewDidLoad];

     // do something to initialize _testField here

     _testField.text = title;
}

Also be aware that UIViewController already has a property called title and overriding setTitle: might have side effects that you aren't anticipating.

jszumski
  • 7,430
  • 11
  • 40
  • 53
0

This is basically related to when the memory is allocated to the _testField. If you are creating the _testField in viewDidLoad programmatically

MyViewController *testViewController = [[MyViewController alloc] init];
testViewController.testFieldText = message;

These two lines would not help as memory has not yet been allocated to the _testField so the text cannot come up.Even if you create it on the story board and assign the string in method

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    {

            SecondViewController *obj = segue.destinationViewController;
            [obj setTitle:@"New Title"];
    }

This would also be of no help as the memory is again not allocated. The simple solutions to this problem is:

  1. Either access the view property of the MyViewController before setting the _testField for example:

    MyViewController    *obj = [[MyViewController alloc] init];
    [self.navigationController pushViewController:obj animated:YES];
    [obj setTitle:@"New Title"];
    
  2. Set the string in a @property in the MyViewController, and reassign the string from the property to the _testField.

And yes, setTitle is a property of UIViewController, if you customize it will mess with the default property. play safe... :)

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
  • Through the use of NSLogs I was able to notice that ViewDidLoad happens before setTitle, but the values only show after setTitle happens. – ibfocused Apr 24 '13 at 19:11
  • If you add any view related call only then it would get called first else, we need to save the string in memory location in the new viewcontroller. – Ravi Dalmia Apr 27 '13 at 19:56
-1

In first ViewController you have use this following

SecondViewcontroller *MyViewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"SecondViewcontroller"];
MyViewController.Title = @"Welcome";
[self presentViewController:MyViewController animated:YES completion:Nil];

In secondViewcontroller use the following

secondViewcontroller.h file to edit

@interface SecondViewcontroller : UIViewController
    @property (strong, nonatomic) NSString *Title;
@end

then print the Title value to secondviewcontroller

  • Downvote for the same reasons as http://stackoverflow.com/questions/20267387/pass-a-value-from-one-viewcontroller-to-another-in-objective-c/33476332#33476332 especially since the answers on this question already answer the question and provide a lot of information already. – Popeye Nov 02 '15 at 12:09