#define Appdelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
#define Appdelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
xxx.h
@protocol delegate명;
@property(nonatomic, retain) id<delegate명> delegate;
맨 밑에
@protocol delegate명 <NSObject>
- (void)delegate_method:(returnType)variable;
@end
xxx.m
@synthesize delegate=_delegate;
[_delegate release];
- (id)initWithDelegate:(id<delegate명>)delegate {
self = [self init];
if(self) {
[self setDelegate:delegate];
}
}
delegate보낼 method 안에는
if([self.delegate respondsToSelector:@selector(delegate_method명:)]) {
[self.delegate delegate_method명:variable];
}
delegate 구현
- (void)delegate_method:(returnType)variable {
NSLog(@"%@", variable);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//셀 갯수
//controllers = NSArray
return [self.controllers count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *FirstLevelCell = @"FirstLevelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
}
NSInteger row = [indexPath row];
SecondLevelViewController *controller = [controllers objectAtIndex:row];
cell.textLabel.text = controller.titie;
cell.imageView.image = controller.rowImage;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark -
#pragma mark Table View Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = [indexPath row];
SecondLevelViewController *nextController = [self.controllers objectAtIndex:row];
[self.navigationController pushViewController:nextController animated:YES];
}