ios 创建一个 UIPopoverController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9751424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:22:49  来源:igfitidea点击:

Creating a UIPopoverController

objective-ciosuikituipopovercontroller

提问by user1253207

I'm creating a popover in Xcode 4.3. I don't get any error messages but when I click build and run and I press the button that is supposed to open the popover screen, the app crashes and highlights in green a line in my code and says:

我正在 Xcode 4.3 中创建一个弹出窗口。我没有收到任何错误消息,但是当我单击构建并运行并按下应该打开弹出窗口的按钮时,应用程序崩溃并以绿色突出显示我的代码中的一行并说:

Thread 1: breakpoint 2.1..

线程 1:断点 2.1..

What does this mean and how can I fix it?

这是什么意思,我该如何解决?

- (IBAction)popOver
{
    SecondView *secondview = [[SecondView alloc] init];
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:secondview];    

    [popover setDelegate:self];    

    [popover presentPopoverFromRect:CGRectMake(801, 401, 300, 200) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
    [popover setPopoverContentSize:CGSizeMake(300, 200)];   
}

Thanks in advance.

提前致谢。

回答by Lorenzo B

Few suggestions here...

这里有一些建议......

First, are you using Automatic Reference Counting (ARC)? if yes you need to have an instance variable to control your UIPopoverController. For example:

首先,您是否在使用自动引用计数 (ARC)?如果是,您需要有一个实例变量来控制您的UIPopoverController. 例如:

@property (strong, nonatomic) UIPopoverController* popover;

if you are not using ARC create a retainone:

如果您不使用 ARC 创建retain一个:

@property (retain, nonatomic) UIPopoverController* popover;

In the first case you have to do this because if not, ARC will release for you the just created popover at the end of your IBAction. In addition you do this to have a reference for your popover. See NOTE. This is valid also if you don't use ARC.

在第一种情况下,您必须这样做,因为如果没有,ARC 将在您的 IBAction 结束时为您释放刚刚创建的弹出窗口。此外,您这样做是为了为您的弹出框提供参考。见说明。如果您不使用 ARC,这也有效。

NOTEAt some point you could have the necessity to release the popover. For example in - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverControllerdo

注意在某些时候,您可能需要释放弹出框。例如在- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController

self.popover = nil;

Then, if your SecondViewis a UIViewControllercall it SecondViewController. But this is a simple naming suggestion.

然后,如果你SecondView是一个UIViewController调用它SecondViewController。但这是一个简单的命名建议。

Finally, the correct way for display the popover from the action sender (e.g a UIButton), instead of hardcode positions, could be:

最后,显示来自动作发送者(例如 a UIButton)的弹出框而不是硬编码位置的正确方法可能是:

- (IBAction)openPopover:(id)sender
{
    // create the popover here...

    UIButton* senderButton = (UIButton*)sender;

    [popover setPopoverContentSize:CGSizeMake(300, 200)];
    [popover presentPopoverFromRect:senderButton.bounds inView:senderButton permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}

回答by Nadella Ravindra

-(IBAction)showpop:(id)sender
{
UIPopoverController *pop = [[UIPopoverController alloc]initWithContentViewController:popoverview];
[pop setDelegate:self];
[pop presentPopoverFromRect:popbutton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

}

回答by Suran

The SecondView that you're passing into UIPopOverControlleris a UIView. Make it a UIViewController, because, what UIPopOverControllerexpects is a UIViewController.

您传入的 SecondViewUIPopOverController是一个UIView. 使它成为 a UIViewController,因为UIPopOverController期望的是 a UIViewController

-Suraj

-苏拉吉

回答by Omar Freewan

Make sure you simulate on iPad device

确保您在 iPad 设备上进行模拟

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
   // We are using an iPhone
    UIActionSheet *alertSheet = [[UIActionSheet alloc] initWithTitle:@"Where do you want to get your daily image?" delegate:(self) cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Library", nil];
   [alertSheet setTag:0];
   [alertSheet setDelegate:self];
   [alertSheet showFromTabBar:[[self tabBarController] tabBar]];
   [alertSheet release];
}else {
    // We are using an iPad
    /// here you should pass UIViewController inside the popover
    /// follow @flexaddicted notes to implement the popover
}

回答by Aman_ClickApps

Check below link for popover tutorial in ios

检查以下链接以获取 ios 中的 popover 教程

UIPopOverController Tutorial

UIPopOverController 教程