创建 UIButton 时 iPhone 应用程序在 loadView 方法中崩溃

发布于 2024-11-09 22:49:41 字数 455 浏览 4 评论 0原文

为什么每当加载此视图时,以下内容都会使我的应用程序崩溃?

-(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 技术交流群。

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

发布评论

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

评论(7

拥抱影子 2024-11-16 22:49:41

我的猜测是,您的应用程序实际上并没有崩溃。它只是不断重复调用 loadView 方法,因为您错过了加载视图。在向 self.view 添加任何内容之前,调用 [super loadView] 或创建一个视图并将其指定为 self.view。方便的方法是使用[super loadView]

- (void)loadView {

    [super loadView];
    // Your Code Here
}

编辑:这个答案看起来是错误的。请考虑访问可以调用[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 as self.view before you add anything to self.view. The convenient way would be to use [super loadView].

- (void)loadView {

    [super loadView];
    // Your Code Here
}

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.

水中月 2024-11-16 22:49:41

正如 @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.

吃不饱 2024-11-16 22:49:41

好吧,这里已经给出的答案有相当多的混乱......

首先,你不调用 [super loadView]; 是正确的,请参阅 - (void )loadView

如果您重写此方法以手动创建视图,则应该这样做并将层次结构的根视图分配给视图属性。 (您创建的视图应该是唯一的实例,并且不应与任何其他视图控制器对象共享。)此方法的自定义实现不应调用 super。

如果您确实打算使用loadView(即以编程方式创建视图,而不是从NIB),那么在您的实现中,您必须分配视图 您的控制器的属性。

另外,您不需要保留按钮的使用方式,因为您将其添加为主视图的子视图(此后您的主视图将拥有它)。

基于这些考虑,您的方法将如下所示:

    -(void)loadView {
        CGRect frame = [[UIScreen mainScreen] applicationFrame]; // adjust to your needs
        UIView *rootView = [[UIView alloc] initWithFrame:frame];

        UIButton *chooseSubjectButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        chooseSubjectButton.frame = CGRectMake(15.0f, 205.0f, 296.0f, 51.0f);
        [chooseSubjectButton addTarget:self action:@selector(chooseSubject) forControlEvents:UIControlEventTouchUpInside];

        [rootView addSubview:chooseSubjectButton];

        self.view = rootView;
        [rootView release];
    }

当然,如果您使用 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 override this method in order to create your views manually, you should do so and assign the root view of your hierarchy to the view property. (The views you create should be unique instances and should not be shared with any other view controller object.) Your custom implementation of this method should not call super.

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 the view 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:

    -(void)loadView {
        CGRect frame = [[UIScreen mainScreen] applicationFrame]; // adjust to your needs
        UIView *rootView = [[UIView alloc] initWithFrame:frame];

        UIButton *chooseSubjectButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        chooseSubjectButton.frame = CGRectMake(15.0f, 205.0f, 296.0f, 51.0f);
        [chooseSubjectButton addTarget:self action:@selector(chooseSubject) forControlEvents:UIControlEventTouchUpInside];

        [rootView addSubview:chooseSubjectButton];

        self.view = rootView;
        [rootView release];
    }

Of course, if you use a NIB to define your root view, you need to override -viewDidLoad: for additional configuration.

Hope this helps.

世态炎凉 2024-11-16 22:49:41

尝试将该代码放入 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?

夜夜流光相皎洁 2024-11-16 22:49:41
-(void)loadView 
{
[super 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];
}

希望这有帮助

-(void)loadView 
{
[super 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];
}

Hope this helps

云醉月微眠 2024-11-16 22:49:41

对于阅读此线程的其他人来说,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.

笑咖 2024-11-16 22:49:41

您将陷入无限循环,因为您在 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.

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