UIButton UIPopUpController 以编程方式

发布于 2024-12-22 06:43:44 字数 892 浏览 3 评论 0原文

难以显示 UIButton 项目。我想在我的 UIPopUpController 中放置几个​​ UIButton。我正在故事板中完成所有操作,并且我需要以编程方式完成此部分。

- (void)showPopover:(id)sender
{
    if(![popoverController isPopoverVisible])
    {
        myPopOver = [[PopUp alloc] init];
        popoverController = [[UIPopoverController alloc] initWithContentViewController:myPopOver] ;
        [popoverController  setPopoverContentSize:CGSizeMake(300, 200.0f)];
        permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        CGRect rect = (CGRect)[sender frame];
        rect.origin.y = self.view.frame.size.height - rect.size.height;           
        [popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    }
    else
    {
        [popoverController dismissPopoverAnimated:YES];
    }
}

having difficulty getting UIButton Items to show.. I want to place several UIButtons within my UIPopUpController. I'm doing everything in a storyboard and I need to do this section programmatically.

- (void)showPopover:(id)sender
{
    if(![popoverController isPopoverVisible])
    {
        myPopOver = [[PopUp alloc] init];
        popoverController = [[UIPopoverController alloc] initWithContentViewController:myPopOver] ;
        [popoverController  setPopoverContentSize:CGSizeMake(300, 200.0f)];
        permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        CGRect rect = (CGRect)[sender frame];
        rect.origin.y = self.view.frame.size.height - rect.size.height;           
        [popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    }
    else
    {
        [popoverController dismissPopoverAnimated:YES];
    }
}

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

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

发布评论

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

评论(1

可是我不能没有你 2024-12-29 06:43:44

您可以使用 UINavigationController(它已经有一个导航栏,您可以在其中附加按钮项)并将其添加为 UIPopoverController 的内容。 UINavigationController 可以使用 initWithRootViewController 方法进行实例化。在这种情况下,控制器将是您的 Popup 类。例如,在 showPopover 方法中,您可以执行以下操作:

PopUp* myPopOver = [[PopUp alloc] init];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:myPopOver];
popoverController = [[UIPopoverController alloc] initWithContentViewController:navController];
// do other stuff here to present you UIPopoverController
[myPopOver release];
[navController release];

现在,在 PopUp 类中的 viewDidLoad 方法中,您可以自定义 UINavigationController 导航栏。例如:

- (void)viewDidLoad {
  [super viewDidLoad];

  UIBarButtonItem *aButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(somethingSelector:)];          
  self.navigationItem.rightBarButtonItem = aButtonItem;
  [aButtonItem release];
}

其中somethingSelector 如下所示:

- (void)somethingSelector:(id)sender
{
   // your custom actions
}

编辑

或者,您可以避免使用 UINavigationController 并在 PopUp 类中创建 UIToolbar。在 UIToolbar 内部,您可以附加 UIBarButtonItems。请参阅 UIToolbar 类参考

You could use a UINavigationController (it has already a navigation bar where you can attach buttons items) and add it as the content of your UIPopoverController. The UINavigationController can be istantiated using the initWithRootViewController method. In this case the controller would be your Popup class. For example, inside your showPopover method, you could do the following:

PopUp* myPopOver = [[PopUp alloc] init];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:myPopOver];
popoverController = [[UIPopoverController alloc] initWithContentViewController:navController];
// do other stuff here to present you UIPopoverController
[myPopOver release];
[navController release];

Now, inside your PopUp class, within viewDidLoad method, you can customize the UINavigationController navigationBar. For example:

- (void)viewDidLoad {
  [super viewDidLoad];

  UIBarButtonItem *aButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(somethingSelector:)];          
  self.navigationItem.rightBarButtonItem = aButtonItem;
  [aButtonItem release];
}

where somethingSelector is like the following:

- (void)somethingSelector:(id)sender
{
   // your custom actions
}

EDIT

Alternatively you can avoid to use the UINavigationController and create UIToolbar inside your PopUp class. Inside the UIToolbar you can attach UIBarButtonItems. See UIToolbar Class Reference

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