试图从已选择的tableView单元格中显示我的弹出窗口
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释放;
我做错了什么?
发布于 2010-11-22 02:47:42
表视图单元格中有一个按钮,当按下按钮时,要显示指向该单元格的弹出窗口。
首先,使用如下内容将按钮添加到cellForRowAtIndexPath中的单元格中:
(不必四舍五入)
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:方法应该如下所示:
-(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,让它找出放置它的最佳位置。
编辑:
要显示弹出窗口,箭头指向按钮而不是单元格,请使用以下命令:
[pop presentPopoverFromRect:button.frame inView:cell
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];但是,根据屏幕上单元格的位置,弹出器可能看不出按钮下面的位置。除非你确信结果,否则使用“任意”而不是“向上”。
还请注意,您应该保存对popover控制器的引用,以便稍后发布(在dealloc中),否则该方法中的pop版本可能会导致崩溃。有关详细示例,请参见示例应用程序Popover。
https://stackoverflow.com/questions/4241054
复制相似问题