Changing label's text in another view controller

objective-c, uilabel, uiviewcontroller

Solution

You could use the delegate pattern

First create your delegate protocol

@class SecondViewController;

@protocol SecondViewControllerDelegate

-(void) updateLabelWithString:(NSString*)string

@end

@property (weak, nonatomic) id<SecondViewControllerDelegate>delegate;

In your `IBAction` connected to your `UIButton`

[self.delegate updateLabelWithString:yourString];

in FirstViewController.h

#import "SecondViewController.h"

@interface FirstViewController : UIViewController <SecondViewControllerDelegate>

in FirstViewController.m

-(void) updateLabelWithString:(NSString*)string {
   label.text = string;
} 

then when you create your controller instance, set FirstViewController as the delegate for your mainViewController

controller.delegate = self;

Problem

I have one view controller named FirstViewController, and a second named SecondViewController. I present second view controller with ``` UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"mainController"]; [self presentViewController:controller animated:YES completion:nil]; ``` In SecondViewController's .m, I want to change the text of a UILabel in FirstViewController. However, the label's text isn't updating. How would I make it so that the FirstViewController's label is updated when a UIButton is pressed in SecondViewController?

Original source

Related problems