简体   繁体   中英

Cannot reference delegate protocols in implimentation

I have a UIViewController with a UIPickerView that creates a string that I want to pass along to my RootViewController ( BL_MainViewController ).

My tactic was to use the delegate pattern but I can't figure out where I'm going wrong here. If my RootViewController is created using a Storyboard, how do I tell it BL_MainViewController.BL_SetTimerViewController = self and where do I set that in the implementation (guess:ViewDidLoad)?

BL_SetTimerViewController.h (the child VC presented by modal segue in IB)

@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 (The 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;
}

In your 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;
    }
}

Super simple test app for delegate(note that many thing are left out as it's quickly thrown together), this works for me.(note that delegate is weak, not assign. If you are targeting iOS 5+, use weak for delegates)

SimpleProtocol.h

@protocol SimpleProtocol <NSObject>

- (void)updateText:(NSString *)text;

@end

ViewController.m

#import "ViewController.h"

#import "SecondViewController.h"
#import "SimpleProtocol.h"

@interface ViewController ()<SimpleProtocol>

@property (nonatomic, weak) IBOutlet UILabel *label;

@end

@implementation ViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    SecondViewController *vc = (SecondViewController *)segue.destinationViewController;
    vc.delegate = self;
}

- (void)updateText:(NSString *)text
{
    self.label.text = text;
}

@end

SecondViewController.m

#import "SimpleProtocol.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerFire:) userInfo:nil repeats:NO];
}

- (void)timerFire:(NSTimer *)timer
{
    NSString *text = [NSString stringWithFormat:@"Test %@", @(arc4random())];
    [self.delegate updateText:text];
    [self.navigationController popViewControllerAnimated:YES];
}

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM