2012-02-27 7 views
2

Popoverコントローラでは、ボタンをクリックしたときにPopoverコントロールを作成し、Popoverクラスのテーブルビューを表示するクラスに移動します。テーブルビューの行をタップするとポップオーバーを消します

ここでは、テーブルビューの行をタップするとポップアップを解除します。上記のコードは動作していない

//popoverclass.h 
UIPopoverController *popover; 
@property(nonatomic,retain)IBOutlet UIPopoverController *popover; 

//popoverclass.m 
-(IBAction)ClickNext 
{ 
    ClassPopDismiss *classCourse = [[ClassPopDismiss alloc] init]; 
    popover = [[UIPopoverController alloc] initWithContentViewController:classCourse]; 
    popover.delegate = self; 
    [popover presentPopoverFromRect:CGRectMake(50,-40, 200, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES]; 
    [classCourse release]; 

} 

//ClassPopDismiss.m 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    PopOverClass *objclass=[[PopOverClass alloc]init]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    [objclass.popover dismissPopoverAnimated:YES]; 

} 

は、ここに私のコードです。

答えて

9

ポップオーバーがクラスpopoverclass.mから提示され、テーブルがClassPopDismiss.mにあるため、同じクラスからポップオーバーを解除することはできません。

だから最良のオプションは、あなたの ClassPopDismiss.hでカスタムデリゲートメソッドを持つことです。あなたの @interfaceセクションで

// ClassPopDismiss.h 
@protocol DismissDelegate <NSObject> 

-(void)didTap; 

@end 

とSET id <DismissDelegate> delegate;

tableViewをタップすると、didTapと呼んでください。あなたのpopoverclass.h

// ClassPopDismiss.m 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    [delegate didTap]; 
} 

:あなたのpopoverclass.m

@interface PopOverClass : UIViewController <DismissDelegate> 

selfにデリゲートを割り当てることを忘れないでください。等:

ClassPopDismiss *classpop = [[ClassPopDismiss alloc]init]; 
classpop.delegate=self; 

プロトコル法を用いながら、このため

-(void)didTap 
{ 
    //Dismiss your popover here; 
    [popover dismissPopoverAnimated:YES]; 
} 
0
if(popover != nil) { 
    [popover dismissPopoverAnimated:YES]; 
} 

あなたがやっていることは、新しいポップオーバーを割り当ててすぐに解除することです。それは機能しません。

0

小さなコードの更新(私見):

// firstViewController.h 
@interface firstViewController : UIViewController <SecondDelegate> 
{ 
    SecondViewController *choice; 
} 

// firstViewController.m 
- (void)choicePicked(NSString *)choice 
{ 
    NSLog(choice); 
    [_popover dismissPopoverAnimated:YES]; //(put it here) 
    _popover = nil; (deallocate the popover) 
    _choicePicker = nil; (deallocate the SecondViewController instance) 
} 

// secondViewController.h (the one that will give back the data) 
@protocol SecondDelegate <NSObject> 
- (void)choicePicked:(NSString *)choice; 
@end 
@interface secondViewController : UITableViewController 
@property (nonatomic, assign) id <SecondDelegate> delegate; 
@end 

// secondViewController.m 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *selection = [_yourArray objectAtIndex:indexPath.row]; 
    [[self delegate] choicePicked:selection]; 
} 
関連する問題