[iPad SDK] Popover中にTableViewを入れて、タップしたらPopoverを閉じたい

今作っている リズムくん for iPad で、拍子記号を Popover で選択できるようにしようとしている。
Popover中にTableViewを入れて、タップしたらPopoverを閉じるようにしたい。
検索したら、すぐ stackoverflow の投稿が見つかった。
iphone – Dismiss the pop over when I tap a row in tableview – Stack Overflow
が、これが 2012/06/29 の時点では書きかけのような状態で、とても分かりづらかったのでEditしてみた。
peer reviewされた後に公開されるらしい。どんな人がreviewするのだろう。

stackoverflowの方は元の回答を尊重してそれを修正する形で書いたけれども、こちらには自分のメモをそのまま載せてみる。
Popover中に入れたClassPopDismiss 中で、TableViewのタップをハンドルしてそれを自分を生成したPopOverClassの方に伝える。
伝えるために DismissDelegate というプロトコルを定義している。

// popoverclass.h
@interface popoverclass:UIViewController <DismissDelegate>

UIPopoverController *popover;


//popoverclass.m
-(IBAction)ClickNext {
     ClassPopDismiss *classPopDismiss = [[[ClassPopDismiss alloc]init]autorelease];
     classPopDismiss.delegate = self;
     popover = [[UIPopoverController alloc]initWithContentViewController:classPopDismiss];
     popover.delegate = self;
     [popover presentPopoverFromRect:CGRectMake(50, -40, 200, 300) inView:self.view];
}

-(void)didTap {
     [popover dismissPopoverAnimated:YES];
}

// ClassPopDismiss.h
@protocol DismissDelegate <NSObject>
-(void)didTab;
@end

@interface ClassPopDismiss 
id <DismissDelegate> delegate;

// ClassPopDismiss.m

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     [delegate didTap];
}

返信を残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

This site uses Akismet to reduce spam. Learn how your comment data is processed.