How can I pass an NSString from one View Controller to another?

大憨熊 提交于 2019-12-06 11:51:42

Your second view controller needs to have its received property set to a value that represents the first view controller.

With storyboard-based projects, this is typically done in prepareForSegue:. This method will be called in your first view controller before the segue to your second view controller is performed.

(In my opinion you would be better off passing just the string to your second view controller rather than a pointer to the whole view controller as this reduces dependency and is the only information your second view controller really needs, but let's not complicate things.)

Here are the steps I think you need to get this working:

  • Give the segue from your first view controller to your second a name in the storyboard. For this example, I'll call it mySegue.
  • Import "ViewController2.h" in "ViewController.m" - your first view controller will need to know that the second view controller has a received property.
  • Add a prepareForSegue: method like so in your first view controller:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"mySegue"])
        {
            ViewController2 *targetVC = (ViewController2*)segue.destinationViewController;
            targetVC.received = self;
        }
    }
    

Hopefully it's pretty clear what this code is doing.

Your mistake is based on a fundamental misunderstanding of properties. Properties are merely syntactic sugar for dealing with the boilerplate and implementation details of getters and setters. Although setting _textToPassToOtherVC will indeed make the property return that value, it does not "inform" the other, because it's reference to "received" is set to nil by default, and never set by you.

If anywhere before you actually check the value of this shared text, you have the lines:

ViewController *myVC1; 
ViewController2 *myVC2;

// ...some initialization.

myVC2.received = myVC1;
// Now the IBAction will display the correct text.

Everything will work.

Try using an NSNotification. When you need to send the string, post a NSNotification with the object!

Put when you want to send the string (after you give the string a value of course):

    yourString = @"Hello";

    [[NSNotificationCenter defaultCenter] postNotificationName:@"sendString" object:yourString];

and in your second View Controller:

-(void)viewDidLoad:
{
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ThingYouWantToDoWithString:) name:@"sendString" object:nil];
}

- (void)ThingYouWantToDoWithString:(NSNotification *)notification{
        // Make sure you have an string set up in your header file or just do NSString *theString = [notification object] if your using ARC, if not allocate and initialize it and then do what I just said
        theString = [notification object];
        NSLog("%@", theString);
        [self performSelector:@selector(OtherThings:) object:theString];
}

Hope this helps!

Have you checked that your self.received is not null/nil?

Set a breakpoint in the code to see if self.received is actually initialized (if it's not, then that's why you don't see anything):

- (IBAction)textButton:(id)sender {

    // Breakpoint here
    NSLog (@"Text in VC1 from VC2 is: %@", self.received.textToPassToOtherVC);

    textDisplay.text = self.received.textToPassToOtherVC;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!