UIPopoverController 麻烦
我正在尝试使用以下代码创建一个弹出窗口:
//.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码很奇怪。
例如,为什么要创建一个视图并将其添加到弹出窗口的内容视图中?
然后,你必须注意内存泄漏。你的代码中有很多这样的东西。
也就是说,这里是一个在
UIPopoverController
中显示UIViewcontroller
的简单示例。其中
self.popover
是具有保留策略的@property
。通过这种方式,在UIPopoverControllerDelegate
方法(或任何您想要的地方)中,您可以释放弹出窗口和/或关闭它。希望有帮助。
PS 检查代码,因为我是手写的。
编辑
通常,当您创建弹出窗口时,其内容视图控制器是自定义的
UIViewController
或UINavigationController
(在这种情况下,您想利用其导航栏)。例如,您可以创建一个自定义的 UIViewController
并在弹出窗口中使用它,而不是简单的
UIViewController
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 aUIPopoverController
.where
self.popover
is a@property
with a retain policy. In this manner inUIPopoverControllerDelegate
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 aUINavigationController
(in this case you want to take advantage of its navigation bar).For example, instead of the simple
UIViewController
, you could create a custom oneand use it within a a popover
解决了我的问题来自这个问题。
简短的回答: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.
问题似乎如下:
nameLabel
未初始化。它指向随机内存,当通过文本属性调用-setText:
时,您的应用程序会崩溃。除此之外,您还有一些内存泄漏。请记住:如果您调用
alloc
,则必须在某个地方release
它。(我不确定这如何与新的自动分配/释放一起使用)。
The problem seems to be the following:
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 torelease
it somewhere.(I'm not sure how this works with the new automatic alloc/release).