从 UIButton 而不是 UIBarButtonItem 显示 WEPopover

Posted

技术标签:

【中文标题】从 UIButton 而不是 UIBarButtonItem 显示 WEPopover【英文标题】:Displaying WEPopover from UIButton instead of UIBarButtonItem 【发布时间】:2011-10-04 13:48:19 【问题描述】:

我在我的应用程序中使用 WEPopover 来弹出一个包含一些按钮的弹出控制器,当我将 wepopover 控制器操作放入 tabbarIteam 时它可以工作,但是当我单击 UIButton 时我需要弹出窗口。如何做到这一点这是我的 barbuttonitem 代码用于显示弹出框。

-(IBAction)_clickbtnAccount:(id)sender

    if (!self.popoverController)
    
        UIViewController *contentViewController = [[pageAccount alloc]initWithNibName:@"pageAccount" bundle:nil];
        self.popoverController = [[[popoverClass alloc] initWithContentViewController:contentViewController] autorelease];
        self.popoverController.delegate = self;
        self.popoverController.passthroughViews = [NSArray arrayWithObject:self.navigationController.navigationBar];

        [self.popoverController presentPopoverFromBarButtonItem:sender
                                       permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown) 
                                                       animated:YES];
        [contentViewController release];
    
    else
    
        [self.popoverController dismissPopoverAnimated:YES];
        self.popoverController = nil;
    


-(IBAction)_clickbtnAccount:(id)sender

    if (!self.popoverController)
    
        UIButton *senderButton = (UIButton *)sender;
        [self.popoverController presentPopoverFromRect:[senderButton frame]
                                                inView:@"pageAccount"
                              permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown) 
                                              animated:YES];        
    
    else
    
        [self.popoverController dismissPopoverAnimated:YES];
        self.popoverController = nil;
    

【问题讨论】:

【参考方案1】:

所以您不想显示来自UIBarButtonItemWEPopover,而是来自UIButton,对吧?! 只需使用

- (void)presentPopoverFromRect:(CGRect)rect 
                    inView:(UIView *)view 
  permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections 
                  animated:(BOOL)animated;

而不是

- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item 
           permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections 
                           animated:(BOOL)animated;

并传入你的UIButton

以你的代码为例:

-(IBAction)_clickbtnAccount:(id)sender

    if (!self.popoverController) 

        UIViewController *contentViewController = [[pageAccount alloc]initWithNibName:@"pageAccount" bundle:nil];
        self.popoverController = [[[popoverClass alloc] initWithContentViewController:contentViewController] autorelease];
        self.popoverController.delegate = self;
        self.popoverController.passthroughViews = [NSArray arrayWithObject:self.navigationController.navigationBar];

        UIButton *senderButton = (UIButton *)sender;
        [self.popoverController presentPopoverFromRect:[senderButton frame]
                        inView:self.view
                                        permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown) 
                  animated:YES];

        [contentViewController release];
     else 
        [self.popoverController dismissPopoverAnimated:YES];
        self.popoverController = nil;
    

【讨论】:

谢谢你的回答,但你能解释一下如何通过我的按钮的矩形吗? -(IBAction)_clickbtnAccount:(id)sender if (!self.popoverController) UIButton *senderButton = (UIButton *)sender; [self.popoverController presentPopoverFromRect:[senderButton frame] inView:@"pageAccount" allowedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown) 动画:YES]; else [self.popoverControllerdismissPopoverAnimated:YES]; self.popoverController = nil; 你必须传递一个UIView(而不是@"pageAccount"),例如self.view. 是的,.... 表示您未包含在更新问题中的原始代码。我再次修改了我的答案 - 试一试。 天哪,再次没有按钮点击事件【参考方案2】:

WEPopover 主要用于在弹出框内显示多个选项,并使用 UITableview 进行大量控制。

这就是我使用 WEPopOver 在按钮单击时显示弹出窗口的方式

-(void)showPopOver:(id)sender
    if (self.popoverController) 
            [self.popoverController dismissPopoverAnimated:YES];
            self.popoverController = nil;
         
        PopOverTable *contentViewController = [[PopOverTable alloc] initWithStyle:UITableViewStylePlain];
        contentViewController.delegatePopoverItemSelectedDelegate=self;
            rectForPopover = [self.view convertRect:btn.bounds fromView:btn];
                self.popoverController = [[[popoverClass alloc] initWithContentViewController:contentViewController] autorelease];

                if ([self.popoverController respondsToSelector:@selector(setContainerViewProperties:)]) 
                    [self.popoverController setContainerViewProperties:[self improvedContainerViewProperties]];
                
                self.popoverController.delegate = self;
                [self.popoverController presentPopoverFromRect:rectForPopover  
                                                        inView: self.view 
                                      permittedArrowDirections:(UIPopoverArrowDirectionAny)
                                                      animated:YES];
        

【讨论】:

delegatePopoverItemSelectedDelegate:这不是结构或联合和 rectforpopover 未分类错误 dude..它是我的 PopOverTable 类的代表。使用您自己的班级代表。您不是要在 popOver 中显示表格吗?【参考方案3】:

使用公认的解决方案,我在 ios 7 下遇到了 WEPopover 的旋转问题:基本上它指向导航栏左侧 UIBarButtonItem 中的自定义按钮,但旋转时弹出窗口移动到屏幕右侧并在进一步的旋转中停留在那里。

【讨论】:

以上是关于从 UIButton 而不是 UIBarButtonItem 显示 WEPopover的主要内容,如果未能解决你的问题,请参考以下文章

如何仅处理 UIButton 的图像内容,而不是按钮本身

使用 UIButton 扩展 UITextView 而不是根据内容自动扩展

如何使用子类而不是预期类?

tableviewcell 内的 UIButton 触发单元格而不是按钮 [重复]

UILabel 和 UIButton - 截断标签而不是按钮

如何从 UIButton 到选项卡栏项?