EDIT: edited for clarity
Disclaimer: I'm new and pretty bad. But I have tried very hard and read lots of stuff to figure this out, but I have not...
I think my whole delegate pattern would work, except I can't figure out how to set the delegate property of ViewController to self in the MatchLetter class. The reason is because I can't figure out how to call code there. It's not a view controller, so viewDidLoad or prepareForSegue won't work.
This is what I've got:
ViewController.h
#import <UIKit/UIKit.h>
@class ViewController;
@protocol letterMatchProtocol <NSObject>
- (BOOL) isLetterMatch:(char) firstLetter;
@end
@interface ViewController : UIViewController
@property (nonatomic, weak) id <letterMatchProtocol> delegate;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
char c = 'a';
// This is the method I want to delegate to MatchLetter, to have a BOOL returned
BOOL returnValue = [self.delegate isLetterMatch:c];
}
@end
MatchLetter.h
#import <Foundation/Foundation.h>
#import "ViewController.h"
@interface Delegate : NSObject <letterMatchProtocol>
@end
MatchLetter.m
#import "MatchLetter.h"
@implementation Delegate
// this is the code I think I need to run here, to set the delegate property...
// ViewController *viewController = [ViewController new];
// viewController.delegate = self;
// ... so that isLetterMatch can be run here from ViewController.m
// But I don't know where to put this code, or how to get it to run before the ViewController
// especially since there are no segues or views to load.
- (BOOL) isLetterMatch:(char)firstLetter {
if (firstLetter == 'a') {
return YES;
}
else {
return NO;
}
}
@end
Can somebody please tell me the best way to proceed? Thanks for reading