UIPoover 问题

发布于 2024-10-03 11:48:04 字数 542 浏览 7 评论 0原文

尝试从已选择的 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 release];

我做错了什么?

Trying to display my popover from the tableView cell that has been selected

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

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喵星人汪星人 2024-10-10 11:48:04

您的表格视图单元格中有一个按钮,并且您希望在按下该按钮时显示一个指向该单元格的弹出窗口。

首先,使用如下所示将按钮添加到 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];

上面的一个重要点是,在@selector中,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];

但是,根据单元格在屏幕上的位置,弹出窗口可能看起来并不位于按钮正下方。除非您确定结果,否则请使用“任意”而不是“向上”。

另请注意,您应该保存对弹出窗口控制器的引用以供以后释放(在 dealloc 中),否则方法中的 [pop release] 可能会导致崩溃。有关详细示例,请参阅 示例应用程序弹出框

You have a button in a table view cell and you want to display a popover pointing to that cell when the button is pressed.

First, add the button to the cell in cellForRowAtIndexPath using something like this:
(doesn't have to be rounded rect)

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

An important point above is that in the @selector, there is a colon after profileUser (this tells the button to send a reference to itself as the first parameter to profileUser). This reference can be used to figure out which cell was selected.

The profileUser: method should be something like this:

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

If possible, leave the arrow direction as UIPopoverArrowDirectionAny and let it figure out the best place to put it.

Edit:
To show the popover with the arrow pointing up to the button instead of the cell, use this:

[pop presentPopoverFromRect:button.frame inView:cell 
    permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

However, depending on the position of the cell on the screen, the popover may not look right underneath the button. Use "Any" instead of "Up" unless you're sure of the results.

Also note you should save a reference to the popover controller for later release (in dealloc) otherwise the [pop release] in the method may cause a crash. For a detailed example, see the sample app Popovers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文