Frage

I am trying to update a label on view controller (DropPinVC) by pressing a button on different view controller 2 (TypeLocation).

In DropPinVC.m I have:

- (void)viewDidLoad
{
    TypeLocation *VC2 = [[TypeLocation alloc] initWithNibName:@"TypeLocation" bundle:nil];
    VC2.myVC2 = self;
    [super viewDidLoad];
}

In TypeLocation.h I have:

@property (nonatomic, strong) DropPinVC *myVC2;

In TypeLocation.m I have:

-(IBAction) switchValueChanged
{

    if (self.TASwitch.on){
    TypeLocation = @"Tourist Attraction";
    [[NSUserDefaults standardUserDefaults] setObject:TypeLocation forKey:@"Type Location"];
}
else {
    TypeLocation = @"Unsafe Location";
    [[NSUserDefaults standardUserDefaults] setObject:TypeLocation forKey:@"Type Location"];

    DropPinVC *myVC2 = [[DropPinVC alloc] initWithNibName:@"DropPinVC" bundle:nil];

    NSString *updatedLabel = [[NSUserDefaults standardUserDefaults] stringForKey:@"Type Location"];

    NSLog(@"Updated Label = %@", updatedLabel);

    myVC2.TypeLabel.text = updatedLabel;

    [self closeScreen];

}

However, the label on view controller 1 does not update. Does anybody know a solution to this problem? Thank you for your help!

War es hilfreich?

Lösung

You need to pass the string, updateLabel, to a string property you create in DropPinVC, and have that controller populate its label in viewDidLoad. Your approach doesn't work because the view hasn't loaded yet at the time you try to set the label's text, so myVC2.TypeLabel will be nil.

Also, you're not using your property, myVC2 in TypeLocation. this line,

DropPinVC *myVC2 = [[DropPinVC alloc] initWithNibName:@"DropPinVC" bundle:nil];

should be,

self.myVC2 = [[DropPinVC alloc] initWithNibName:@"DropPinVC" bundle:nil];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top