简体   繁体   中英

Passing values from one ViewController to another in Objective-C

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.

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.

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... :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM