【问题标题】:Changing text on UILabel from another View Controller从另一个视图控制器更改 UILabel 上的文本
【发布时间】:2017-06-26 19:04:55
【问题描述】:

我正在使用委托来尝试从 ViewController2 中的 UILabel 更改文本。

在 ViewController1.h 我有:

@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 里面我有:

#import "SignInViewController.h"


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

在viewDidLoad中:

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

我在 ViewController2.m 中的方法:

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

我的问题是即使我从第一个视图控制器调用上面的方法也没有被调用。

【问题讨论】:

    标签: ios objective-c xcode


    【解决方案1】:

    这有点乱,很抱歉......并且您正在 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;
    

    【讨论】:

      【解决方案2】:

      您可以直接调用此方法,无需创建委托。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-11
        • 2016-09-02
        • 1970-01-01
        相关资源
        最近更新 更多