创建 UIButton 时 iPhone 应用程序在 loadView 方法中崩溃
为什么每当加载此视图时,以下内容都会使我的应用程序崩溃?
-(void)loadView {
UIButton *chooseSubjectButton = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure] retain];
[chooseSubjectButton addTarget:self action:@selector(chooseSubject) forControlEvents:UIControlEventTouchUpInside ];
chooseSubjectButton.frame = CGRectMake(15.0f, 205.0f, 296.0f, 51.0f);
[self.view addSubview:chooseSubjectButton];
}
任何帮助将不胜感激。
谢谢!
Why does the following crash my app whenever this view is loaded?
-(void)loadView {
UIButton *chooseSubjectButton = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure] retain];
[chooseSubjectButton addTarget:self action:@selector(chooseSubject) forControlEvents:UIControlEventTouchUpInside ];
chooseSubjectButton.frame = CGRectMake(15.0f, 205.0f, 296.0f, 51.0f);
[self.view addSubview:chooseSubjectButton];
}
Any help would be greatly appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我的猜测是,您的应用程序实际上并没有崩溃。它只是不断重复调用
loadView
方法,因为您错过了加载视图。在向 self.view 添加任何内容之前,调用[super loadView]
或创建一个视图并将其指定为self.view
。方便的方法是使用[super loadView]
。编辑:这个答案看起来是错误的。请考虑访问可以调用[super loadView]吗? @Denis Vert 在评论中指出。
My guess is, your app is not crashing actually. It just keeps calling the
loadView
method repeatedly because you've missed to load the view. Call[super loadView]
or create a view and assign it asself.view
before you add anything to self.view. The convenient way would be to use[super loadView]
.EDIT: This answer looks to be wrong. Please consider visiting Is it ok to call [super loadView]? as @Denis Vert pointed out in the comments.
正如 @Simon 所指出的,您缺少
[super loadView]
提示了另一个问题。您是否正在加载此 viewController 以及关联的 .xib 文件?如果是这样,您可能打算在
-viewDidLoad
而不是-viewLoad
中执行此操作。只是一个想法,因为我过去对此感到困惑。
Your lack of
[super loadView]
as noted by @Simon prompts another question.Are you loading this viewController with an associated .xib file? If so, you probably mean to be doing this in
-viewDidLoad
rather than-viewLoad
.Just a thought, because I've confused myself about that in the past.
好吧,这里已经给出的答案有相当多的混乱......
首先,你不调用
[super loadView];
是正确的,请参阅- (void )loadView
:如果您确实打算使用
loadView
(即以编程方式创建视图,而不是从NIB),那么在您的实现中,您必须分配视图
您的控制器的属性。另外,您不需要保留按钮的使用方式,因为您将其添加为主视图的子视图(此后您的主视图将拥有它)。
基于这些考虑,您的方法将如下所示:
当然,如果您使用 NIB 来定义根视图,则需要重写
-viewDidLoad:
进行其他配置。希望这有帮助。
Ok, there is quite a bit of confusion in the answers already given here...
First, you're correct to not call
[super loadView];
, see- (void)loadView
:If you really mean to use
loadView
(i.e. creating your view programmatically, and not from a NIB), then in your implementation you must assign theview
property of your controller.Also, you do not need to retain your button the way you're using it, because you're adding it as a subview of your main view (your main view will own it after that).
Based on these considerations, your method would look like this:
Of course, if you use a NIB to define your root view, you need to override
-viewDidLoad:
for additional configuration.Hope this helps.
尝试将该代码放入
viewDidLoad
中。初始化代码可以创建无限的堆栈跟踪。虽然我不确定创建该按钮是否会触发崩溃。如果我错了,有人纠正我
请参阅:iPhone SDK:loadView和viewDidLoad有什么区别?
Try placing that code in
viewDidLoad
. Init-codes can create an infinite stack trace. Though I'm not sure if creating that button might trigger the crash.Anyone correct me if I'm wrong
See this: iPhone SDK: what is the difference between loadView and viewDidLoad?
希望这有帮助
Hope this helps
对于阅读此线程的其他人来说,Octy 正确地说你永远不应该调用 [super loadView]。我在本页读过一些关于它的辩论。阅读 Octy 的答案中发布的文档,第 28 页,它明确指出您不应调用此方法。甚至还解释了原因。这样做将使用默认名称(与类名相同)查找笔尖。即使这个笔尖不存在,也是对处理能力的浪费。只需创建一个新的 UIView 并将其设置为 self.view 即可。 (请参阅本页上的示例)
只是想确保未来的读者知道这一点。我花了一段时间才弄清楚。
Just for anyone else reading this thread, Octy is correct in saying you should never call [super loadView]. I've read some debate on this page about it. Read the documentation posted in Octy's answer, on page 28 its clearly stated that you should not call this method. It's even explained why. Doing so will look for a nib using the default name (same as the classname). Even if this nib doesn't exist, it's a waste of processing power. Simply create a new UIView and set it as self.view. (see examples on this page)
Just wanted to make sure future readers knew this. Took me a while to figure it out.
您将陷入无限循环,因为您在 loadView 中引用 self.view 。当你调用 self.view 时,self.view 为 nil,它会导致 loadView 再次被调用。创建循环。
如果你调用 [super loadView] 它会初始化 self.view 所以你不会看到问题。
所以,实例化 self.view myself 就可以了。
希望这有帮助。
You are getting an infinite loop because you are referencing self.view in loadView. When you call self.view, and self.view is nil, it'll cause loadView to be called again. Creating the loop.
If you call [super loadView] it will initialize self.view so you dont see the problem.
So, instantiate self.view yourself and you'll be fine.
Hope this helps.