UIPopoverController 麻烦

发布于 2025-01-08 07:01:24 字数 1288 浏览 1 评论 0原文

我正在尝试使用以下代码创建一个弹出窗口:

//.h
@interface LoginViewController : UIViewController <UIPopoverControllerDelegate>
....
@end

//.m
- (IBAction)popoverTest:(id)sender
{
    UIViewController *popoverContent = [[UIViewController alloc] init];
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];

    UILabel *nameLabel = [[UILabel alloc] init];  //edited, fixed UILabel allocation
    nameLabel.text = @"Person's name";
    [nameLabel sizeToFit];
    nameLabel.frame = CGRectMake(10, 10, nameLabel.frame.size.width, nameLabel.frame.size.height);
    [myView addSubview:nameLabel];

    popoverContent.view = myView;

    popoverContent.contentSizeForViewInPopover = CGSizeMake(300, 300);

    UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
    popoverController.delegate = self;

    [popoverController presentPopoverFromRect:((UIButton *)sender).frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    NSLog(@"ran all code");
}

我创建了一个 UIView,将标签作为子视图,然后将我的视图分配给 UIViewController.view。然后我创建了一个弹出窗口控制器,调整了弹出窗口控制器的大小,设置了委托并从按钮的框架中呈现它。

我收到 SIGABRT 并且应用程序崩溃了。

我有什么遗漏的吗?

编辑:我修复了 UILabel 分配。问题始终存在。

I'm trying to create a popover with the following code:

//.h
@interface LoginViewController : UIViewController <UIPopoverControllerDelegate>
....
@end

//.m
- (IBAction)popoverTest:(id)sender
{
    UIViewController *popoverContent = [[UIViewController alloc] init];
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];

    UILabel *nameLabel = [[UILabel alloc] init];  //edited, fixed UILabel allocation
    nameLabel.text = @"Person's name";
    [nameLabel sizeToFit];
    nameLabel.frame = CGRectMake(10, 10, nameLabel.frame.size.width, nameLabel.frame.size.height);
    [myView addSubview:nameLabel];

    popoverContent.view = myView;

    popoverContent.contentSizeForViewInPopover = CGSizeMake(300, 300);

    UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
    popoverController.delegate = self;

    [popoverController presentPopoverFromRect:((UIButton *)sender).frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    NSLog(@"ran all code");
}

I created a UIView, put a label as a subview and then assigned my view to the UIViewController.view. Then I created a popover controller, sized the popover controller, set the delegate and presented it from the button's frame.

I receive a SIGABRT and the app crashes.

Is there something I'm missing?

EDIT: I fixed the UILabel allocation. The problem is always there.

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

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

发布评论

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

评论(3

笑忘罢 2025-01-15 07:01:24

你的代码很奇怪。

例如,为什么要创建一个视图并将其添加到弹出窗口的内容视图中?

然后,你必须注意内存泄漏。你的代码中有很多这样的东西。

也就是说,这里是一个在 UIPopoverController 中显示 UIViewcontroller 的简单示例。

- (IBAction)yourAction:(id)sender

    UIButton* senderButton = (UIButton*)sender;

    UIViewController* yourController = [[UIViewController alloc] init];

    UIPopoverController* pop = [[UIPopoverController alloc] initWithContentViewController:yourController];
    pop.delegate = self;
    pop.popoverContentSize = CGSizeMake(300, 300);    

    self.popover = pop;

    [pop presentPopoverFromRect:senderButton.bounds inView:senderButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    pop.passthroughViews = nil;

    [yourController release];
    [pop release];
}

其中 self.popover 是具有保留策略的 @property。通过这种方式,在 UIPopoverControllerDelegate 方法(或任何您想要的地方)中,您可以释放弹出窗口和/或关闭它。

希望有帮助。

PS 检查代码,因为我是手写的。

编辑

通常,当您创建弹出窗口时,其内容视图控制器是自定义的UIViewControllerUINavigationController(在这种情况下,您想利用其导航栏)。

例如,您可以创建一个自定义的 UIViewController

//.h
@interface CustomUIViewController : UIViewController

@end

//.m
@implementation CustomUIViewController 

// other code here...

- (void)viewDidLoad
{
   // here you are sure that the view has been loaded in memory (alternatively override loadView method), so
   UIView* greenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
   green.backgroundColor = [UIColor greenColor];
   [self.view addSubview:greenView];
   [greenView release];
}

@end

并在弹出窗口中使用它,而不是简单的 UIViewController

- (IBAction)yourAction:(id)sender

    UIButton* senderButton = (UIButton*)sender;

    CustomUIViewController* yourController = [[CustomUIViewController alloc] init];

    // same as before...
}

Your code is quite strange.

For example why do you create a view and do you add it to the content view of your popover?

Then, you have to make attention to memory leaks. There a lot of them in your code.

That said, here a simple example for display a UIViewcontroller within a UIPopoverController.

- (IBAction)yourAction:(id)sender

    UIButton* senderButton = (UIButton*)sender;

    UIViewController* yourController = [[UIViewController alloc] init];

    UIPopoverController* pop = [[UIPopoverController alloc] initWithContentViewController:yourController];
    pop.delegate = self;
    pop.popoverContentSize = CGSizeMake(300, 300);    

    self.popover = pop;

    [pop presentPopoverFromRect:senderButton.bounds inView:senderButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    pop.passthroughViews = nil;

    [yourController release];
    [pop release];
}

where self.popover is a @property with a retain policy. In this manner in UIPopoverControllerDelegate methods (or wherever you want), you can release your popover and/or dismiss it.

Hope it helps.

P.S. Check the code because I've written by hand.

Edit

Usually when you create a popover, its content view controller is or a custom UIViewController or a UINavigationController (in this case you want to take advantage of its navigation bar).

For example, instead of the simple UIViewController, you could create a custom one

//.h
@interface CustomUIViewController : UIViewController

@end

//.m
@implementation CustomUIViewController 

// other code here...

- (void)viewDidLoad
{
   // here you are sure that the view has been loaded in memory (alternatively override loadView method), so
   UIView* greenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
   green.backgroundColor = [UIColor greenColor];
   [self.view addSubview:greenView];
   [greenView release];
}

@end

and use it within a a popover

- (IBAction)yourAction:(id)sender

    UIButton* senderButton = (UIButton*)sender;

    CustomUIViewController* yourController = [[CustomUIViewController alloc] init];

    // same as before...
}
¢蛋碎的人ぎ生 2025-01-15 07:01:24

解决了我的问题来自这个问题。

简短的回答:ARC不不保留 UIPopoverController。

我把它作为一个实例变量并且工作得很好。

solved my issue from this question.

short answer: ARC doesn't retain the UIPopoverController.

i made it an instance variable and works just fine.

何以畏孤独 2025-01-15 07:01:24

问题似乎如下:

UILabel *nameLabel;
nameLabel.text = @"Person's name";

nameLabel 未初始化。它指向随机内存,当通过文本属性调用 -setText: 时,您的应用程序会崩溃。

除此之外,您还有一些内存泄漏。请记住:如果您调用alloc,则必须在某个地方release它。
(我不确定这如何与新的自动分配/释放一起使用)。

The problem seems to be the following:

UILabel *nameLabel;
nameLabel.text = @"Person's name";

nameLabel is uninitialized. It points to random memory, and when calling -setText: through the text-property, your application crashes.

Apart from this, you have a few memory leaks. Remember: If you call alloc, you have to release it somewhere.
(I'm not sure how this works with the new automatic alloc/release).

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