我有一个带有UIViewControllerUIPickerView,它创建了一个想要传递给我的RootViewController(BL_MainViewController)的字符串。

我的策略是使用委托模式,但我不知道我在哪里出错。如果我的RootViewController是使用情节提要板创建的,如何告诉它BL_MainViewController.BL_SetTimerViewController = self以及如何在实现中将其设置为(guess:ViewDidLoad)?

BL_SetTimerViewController.h (IB中的模态segue呈现的子VC)

@protocol BL_SetTimerViewControllerDelegate

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

@end

@interface BL_SetTimerViewController : UIViewController{
  ... // some ivars
}

@property (assign, nonatomic) id <BL_SetTimerViewControllerDelegate> delegate;

@end

BL_SetTimerViewController.m
@implementation BL_SetTimerViewController
...
@synthesize delegate;
...
- (IBAction)setTimerAndDismissViewController:(id)sender {

    // does some stuff, then:

    [self.delegate updateLabelWithString:@"TEST"];
    [self dismissViewControllerAnimated:YES completion:nil];

}

BL_MainViewController.h (Root VC)
#import "BL_SetTimerViewController.h"

@interface BL_MainViewController : UIViewController <BL_SetTimerViewControllerDelegate>{
...
}


@end

BL_MainViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    // do some stuff here
    // presumably also assign the delegate protocol?
}

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

最佳答案

在您的BL_MainViewController.m中

- (void)viewDidLoad {
    [super viewDidLoad];

}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.pLabel.text=GlobleSting;
}

-(void)updateLabelWithString:(NSString *)string {
    GlobleSting = string; //declare in .h file
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"YoursegueIdentifir"]) {
        BL_SetTimerViewController *settimerVC =
        segue.destinationViewController;
        settimerVC.delegate = self;
    }
}

关于ios - 在实现中无法引用委托(delegate)协议(protocol),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25561386/

10-12 04:40