two view controllers and reusability with delegate
Posted
by
netcharmer
on Stack Overflow
See other posts from Stack Overflow
or by netcharmer
Published on 2010-12-30T10:27:20Z
Indexed on
2010/12/30
10:54 UTC
Read the original article
Hit count: 148
Newbie question about design patterns in objC. I'm writing a functionality for my iphone app which I plan to use in other apps too. The functionality is written over two classes - Viewcontroller1 and Viewcontroller2. Viewcontroller1 is the root view of a navigation controller and it can push Viewcontroller2. Rest of the app will use only ViewController1 and will never access Viewcontroller2 directly. However, triggered by user events, Viewcontroller2 has to send a message to the rest of the app. My question is what is the best way of achieving it?
Currently, I use two level of delegation to send the message out from Viewcontroller2. First send it to Viewcontroller1 and then let Viewcontroller1 send it to rest of the app or the application delegate. So my code looks like -
//Viewcontroller1.h
@protocol bellDelegate
-(int)bellRang:(int)size;
@end
@interface Viewcontroller1 : UITableViewController <dummydelegate> {
id <bellDelegate> delegate;
@end
//Viewcontroller1.m
@implementation Viewcontroller1
-(void)viewDidLoad {
//some stuff here
Viewcontroller2 *vc2 = [[Viewcontroller2 alloc] init];
vc2.delegate = self;
[self.navigationController pushViewController:vc2
animated:YES];
}
-(int)dummyBell:(int)size {
return([self.delegate bellRang:size]);
}
//Viewcontroller2.h
@protocol dummyDelegate
-(int)dummyBell:(int)size;
@end
@interface Viewcontroller2 : UITableViewController {
id <dummyDelegate> delegate;
@end
//Viewcontroller2.m
@implementation Viewcontroller2
-(int)eventFoo:(int)size {
rval = [self.delegate dummyBell:size];
}
@end
© Stack Overflow or respective owner