Reputation: 3222
I'm working on a project after my ex-colleague. The code was great except for one,
Main Controller
@interface MainController : UIViewController {
}
@property (nonatomic, strong) IBOutlet HeaderBar *headerBar;
- (void)setup;
- (IBAction)refreshHeader;
@end
@implementation MainController
- (void)setup{
HeaderBar *hb = [[HeaderBar alloc] initWithNibNames:@"HeaderBar"];
self.headerBar = hb;
}
- (IBAction)refreshHeader {
//Do something with headerBar's properties
//For example, [self.headerBar someiVar];
}
Then, there is a button as a subview of HeaderBar view. This button was tied with FirstResponder's method, which appears to be refreshHeader
The code works fine as he wanted. But, even I'm new to iOS Development and I'm not quite understand about the FirstResponder yet. I don't feel right with this design approach.
Since that button belongs to headerBar's view. It should tied to headerBar's method, right? (assume that headerBar is a controller too) Then, find a way to send a data/result/message from headerBar to MainController instead of send it to MainController directly by using firstResponder.
If that's so, how could I send data/result/message to one of MainController's method from a HeaderBar's method instead of using the approach which appearing in other part of the code. (Which I feel not good about it too, it looks weird. It looks like, create a relation between two controller, in a wrong way.)
Main Controller
- (void)anotherSetup{
FooterBar *fb = [[FooterBar alloc] initWithNibNames:@"FooterBar"];
fb.mainController = self;
self.footerBar = fb;
}
FooterBar
#import "MainController.h"
@interface FooterBar : UIViewController {
}
@property (nonatomic, strong) MainController *mainController;
- (void)someMethod;
@end
@implementation FooterBar
@synthesize mainController;
- (void)someMethod {
[self.mainController someMainControllerMethod];
}
I read some other related questions, most of them said that the other approach is to define Protocal and Delegate, Is it the best way to do this? Is it the only way? Instead of two controller communicate with each other.
Any suggestion is welcome, correct my misunderstanding is welcome too. I will have to work on this project for months and it's only at the second phase from five. So, I'd love to have least false design as possible.
Best Regards
P.S. I'm sorry if the code isn't correct, it isn't the actual code. Something may not correct, but, hope you guys can see the picture.
Upvotes: 0
Views: 1724
Reputation: 1164
you can use
self.parentViewController
it refere to the viewController that added the current viewController
Upvotes: 2
Reputation: 4092
Yes, this is the best way to do it. It's called Delegation Design Pattern. And that's actually having two controllers communicating with each other.
The only issue I see here is that you retain the MainController in FooterBar. You shouldn't do this, as it leads to retain cycle which blocks objects from being deallocated.(object A retains B, and obj B retains A, so their retain count will never be 0).
So instead of having (at Footerbar):
@property (nonatomic, strong) MainController *mainController;
you should have:
@property (nonatomic, assign) MainController *mainController;
Or to be more generic(if needed), you can create a protocol describing needed methods for delegate. And then make MainController implement that protocol.
//FooterBar.h
@protocol FooterBarDelegate <NSObject>
-(void)aNeededMethod;
@end
@interface FooterBar : UIViewController {
}
@property (nonatomic, assign) id<FooterBarDelegate> mainController;
- (void)someMethod;
@end
and then
//MainController.h
#import "FooterBar.h"
@interface MainController : UIViewController <FooterBarDelegate> {
}
Upvotes: 2