首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UIPopover问题

UIPopover问题
EN

Stack Overflow用户
提问于 2010-11-21 23:27:59
回答 1查看 1.1K关注 0票数 0

试图从已选择的tableView单元格中显示我的弹出窗口

代码语言:javascript
运行
复制
UITableViewCell *cell;
 UserProfile *switchV = [[UserProfile alloc] initWithNibName:nil bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:switchV];
  UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:navController];
 [pop presentPopoverFromRect:cell.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

switchV释放;

我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2010-11-22 02:47:42

表视图单元格中有一个按钮,当按下按钮时,要显示指向该单元格的弹出窗口。

首先,使用如下内容将按钮添加到cellForRowAtIndexPath中的单元格中:

(不必四舍五入)

代码语言:javascript
运行
复制
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(100, 0, 100, 30)];
[button setTitle:@"Button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(profileUser:) 
                         forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];

上面重要的一点是,在@选择器中,在profileUser之后有一个冒号(这告诉按钮将对自身的引用作为profileUser的第一个参数)。此引用可用于确定选择了哪个单元格。

profileUser:方法应该如下所示:

代码语言:javascript
运行
复制
-(void)profileUser:(UIButton *)button
{
    UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
    //first superview is cell.contentView
    //second superview is cell

    UserProfile *switchV = [[UserProfile alloc] initWithNibName:nil bundle:nil];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:switchV];
    UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:navController];

    [pop presentPopoverFromRect:cell.frame inView:self.view 
        permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    [switchV release];
    [navController release];

    self.popoverController = pop; //save in property for later release

    [pop release];
}

如果可能的话,将箭头方向保留为UIPopoverArrowDirectionAny,让它找出放置它的最佳位置。

编辑:

要显示弹出窗口,箭头指向按钮而不是单元格,请使用以下命令:

代码语言:javascript
运行
复制
[pop presentPopoverFromRect:button.frame inView:cell 
    permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

但是,根据屏幕上单元格的位置,弹出器可能看不出按钮下面的位置。除非你确信结果,否则使用“任意”而不是“向上”。

还请注意,您应该保存对popover控制器的引用,以便稍后发布(在dealloc中),否则该方法中的pop版本可能会导致崩溃。有关详细示例,请参见示例应用程序Popover

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4241054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档