I have a ViewController, and a UIView.
The UIView has a delegate, and the delegate function is set in the ViewController.
All I want to do, is have the delegate function defined in a separate file. So the UIView.m #imports the separate file, instead of all the ViewControllers which use the UIView.
I believe this is a standard procedure, but keep falling over myself trying to get it to work. 😐 Would really appreciate some help. Thanks.
myViewController.h
#import <UIKit/UIKit.h>
#import "myUIView.h"
@protocol ModalViewDelegate
-(void)didReceiveMessage:(NSString *)message;
@end
@interface myViewController : UIViewController <ModalViewDelegate>
@property (nonatomic, strong) myUIView *myUIViewItem;
@end
myViewController.m
#import "myViewController.h"
#import "myUIView.h"
@interface myViewController ()
@end
@implementation myViewController
@synthesize myUIViewItem;
- (void)didReceiveMessage:(NSString *)message { //<<< THIS IS WHAT
NSLog(@"Message from button: %@", message); //<<< NEEDS MOVING
}
- (void)viewDidLoad
{
…
myUIViewItem.delegate = self;
…
myUIView.h
#import <UIKit/UIKit.h>
@protocol ModalViewDelegate;
@interface myUIView : UIView {
id<ModalViewDelegate> delegate;
}
@property (nonatomic, strong) id<ModalViewDelegate> delegate;
myUIView.m
#import "myUIView.h"
#import "myViewController.h"
@implementation myUIView
@synthesize delegate;
...
[delegate didReceiveMessage:@"Data from UIView!"];
Based on your comment:
The way I was given was to use a subclass, and that’s been working great for me. In my iOS projects I have a class called BaseViewController, which is a subclass of UIViewController. I put lots of code in it related to HUD management, NSOperations management, etc. Then virtually all my view controllers are subclasses of it.