当我在tableview中点击一行时,关闭popover

时间:2012-02-27 12:00:20

标签: iphone ios objective-c xcode4 uipopovercontroller

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

}

以上代码无效。

3 个答案:

答案 0 :(得分:9)

无法从同一个类中删除popover,因为popover是从类popoverclass.m呈现的,而您的表位于ClassPopDismiss.m

因此,最好的选择是在ClassPopDismiss.h中使用自定义委托方法:

// ClassPopDismiss.h
@protocol DismissDelegate <NSObject>

-(void)didTap;

@end

并在id <DismissDelegate> delegate;部分设置@interface

致电didTap告诉PopOverClass点击tableView

// 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;

使用协议方法时:

-(void)didTap
{
    //Dismiss your popover here;
    [popover dismissPopoverAnimated:YES];
}

答案 1 :(得分:0)

if(popover != nil) {
    [popover dismissPopoverAnimated:YES];
}

你正在做的是分配一个新的popover并立即解雇它,这不起作用。

答案 2 :(得分:0)

此代码的小代码更新(imho):

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