2012-02-27 26 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]; 
} 
相關問題