2017-06-26 8 views
0

私は委任を使用して、ViewController2のUILabelからテキストを変更しようとしています。 ViewController1.hでUILabelのテキストを他のView Controllerから変更する

私が持っている:

@protocol WelcomeDelegate 

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

@end 


@interface ViewController1 : UIViewController 

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

インサイド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]; 
} 

インサイドViewController2.hを私は持っている:のviewDidLoadで

#import "SignInViewController.h" 


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

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

ViewController2.mで私の方法:

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

私の問題は、私が最初のビューコントローラからそれを呼び出す場合でも、上記のメソッドが呼び出さ取得されていないということです。

答えて

0

これはちょっと面倒です.....ごめんなさい.....そしてViewController2でViewController1を再インスタンス化しています.... そのラベルを一度設定する必要がある場合は、すべてのデリゲート/プロトコルの情報を削除してくださいそして、ばかりに直接メソッドを呼び出します。

- (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]]; 
    }]; 
} 

、その後、原因のViewController2.hにそのメソッドを公開する:

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

あなたは直接このメソッドのデリゲートを作成する必要を呼び出すことはできません。

ViewController2 *obj=[[ViewController2 alloc] init]; 
[obj updateLabelWithString:@"title"]; 
[self.navigationController pushViewController:obj animated:YES]; 
関連する問題