2017-06-26 4 views
0

Ich verwende Delegierung, um zu versuchen, den Text von einem UILabel in ViewController2 zu ändern.Ändern von Text auf UILabel von einem anderen View Controller

In ViewController1.h ich habe:

@protocol WelcomeDelegate 

-(void) updateLabelWithString:(NSString*)string; 

@end 


@interface ViewController1 : UIViewController 

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

Innen ViewController1.m:

- (void)presentWelcomeAlert { 

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
ViewController2* contentVC = [storyboard instantiateViewControllerWithIdentifier:@"ViewController2ID"]; 

[self.delegate updateLabelWithString:[NSString stringWithFormat:@"Welcome to the 11Health Family %@! We are here to accompany and support you through the journey as an ostomate. Our new Hydration Tracker is ready to follow your hydration levels. Tap 'Continue' to begin!", [dcsContext sharedContext].activeParticipant.firstName]]; 

UIViewController *rootVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; 
[rootVC presentViewController:contentVC animated:YES completion:nil]; 
} 

Innen ViewController2.h ich habe:

#import "SignInViewController.h" 


@interface WelcomePopoverViewController() <UIViewControllerTransitioningDelegate> 
{ 
    SignInViewController *signInViewController; 
} 

Im viewDidLoad:

viewController1 = [[ViewController1 alloc] init]; 
[viewController1 setDelegate:self]; 

Meine Methode in ViewController2.m:

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

Mein Problem ist, dass das Verfahren, das oben genannt zu werden, ist nicht einmal, wenn ich es von der ersten View-Controller aufrufen.

Antwort

0

Das ist ein bisschen chaotisch, sorry zu sagen ..... und Sie sind ViewController1 in ViewController2 .... wieder instanziieren. Wenn Sie nur diese Bezeichnung einmal festlegen müssen, dann alle delegate/Protokoll Zeug Graben und rufen Sie einfach die Methode direkt:

- (void)presentWelcomeAlert { 
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    ViewController2* contentVC = [storyboard instantiateViewControllerWithIdentifier:@"ViewController2ID"]; 

    UIViewController *rootVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; 
    [rootVC presentViewController:contentVC animated:YES completion:^ { 
    [contentVC updateLabelWithString:[NSString stringWithFormat:@"Welcome to the 11Health Family %@! We are here to accompany and support you through the journey as an ostomate. Our new Hydration Tracker is ready to follow your hydration levels. Tap 'Continue' to begin!", [dcsContext sharedContext].activeParticipant.firstName]]; 
    }]; 
} 

und dann von Ursache aussetzen diese Methode in ViewController2.h:

-(void) updateLabelWithString:(NSString*)string; 
0

Sie können diese Methode direkt aufrufen keine Notwendigkeit zu delegieren erstellen.

ViewController2 *obj=[[ViewController2 alloc] init]; 
[obj updateLabelWithString:@"title"]; 
[self.navigationController pushViewController:obj animated:YES]; 
Verwandte Themen