2012-02-27 79 views
2

我正在使用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

// ClassPopDismiss.h 
@protocol DismissDelegate <NSObject> 

-(void)didTap; 

@end 
@interface部分

并设置id <DismissDelegate> delegate;

致电didTap告诉PopOverClasstableView被窃听。

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

在你popoverclass.h

@interface PopOverClass : UIViewController <DismissDelegate> 

在你popoverclass.m,不要忘记分配委托self。像:

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

虽然使用协议方法:

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

你在做什么是分配一个新的popover并立即解雇它,这是行不通的。

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]; 
}