gpt4 book ai didi

iPhone 开发 - 当我更改方向时我的应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 21:00:37 27 4
gpt4 key购买 nike

我遇到了这个问题,我的应用程序出现 EXC_BAD_ACCESS 错误并卡住/停止。我使用模拟器的“向左旋转”和“向右旋转”选项来模拟方向变化行为。导致此错误的可能原因有哪些?由于我没有获得有关错误的详细信息,因此无法追踪它。

我所有的 Controller 类都有:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait); // Return YES for supported orientations
}

有什么想法、指点吗?谢谢。

<小时/>

但是我的代码没有调用 [UIDevice setOrientation:] 方法。

这是我在调试窗口中得到的跟踪:

objc_msgSend
-[UIWindow _handleDeviceOrientationChange:]
_nsnote_callback
__CFXNotificationPost
_CFXNotificationPostNotification
-[NSNotificationCenter postNotificationName:object:userInfo:]
-[UIDevice setOrientation:]
-[UIApplication handleEvent:withNewEvent:]
_[UIApplication sendEvent:]
_UIApplicationHandleEvent
SendEvent
PurpleEventTimerCallBack
CFRunLoopRunSpecific
CFRunLookRunInMode
GSEventRunModel
GSEventRun
-[UIApplication _run]
UIApplicationMain
main

请注意,当我的 View 包含选项卡栏时,会出现错误。由于没有人回复,我想这不是一个常见问题,而且我找不到解决此问题的任何资源。

Can i somehow tell my application to run in single orientation (portrait), and don't send events for orientation change.? This might solve my problem, but this is the alternate that i'm looking for.

<小时/>

谢谢保罗,您的评论很有帮助,我能够解决问题,但需要保留我不需要的 View 的内存。这是发生的事情:

我有一个 View ( View A),它打开另一个 View ( View B)。应用程序委托(delegate)方法负责打开 View A 和 View B。应用程序不会在 View A 上崩溃。我从窗口中删除 View A,并释放它的 Controller 对象,然后将 View B 添加到窗口。我相信这就是我做错事的地方。你能帮我写一下代码吗?

下面是释放 View A并打开 View B的代码:

- (void)openViewB {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:window cache:YES];

// Remove viewControllerA from the window's view
[window removeFromSuperview];

// !-- Commenting out this block fixes the issue --!
// Release memory wired for viewControllerA view
if(viewControllerA) {
[viewControllerA release];
viewControllerA = nil;
}

// We call window:addSubView to add the viewB to the main window.
// You can use this call to add other views as child views not only to windows
// but to other views as well. (UIWindow is a subclass of UIView).
[window addSubview:viewControllerB.view];
[UIView commitAnimations];

// Override point for customization after application launch
[window makeKeyAndVisible];
}

我使用标签栏应用程序模板启动了应用程序,viewControllerB 的类型为 UITabBarController。我肯定在这里做错了什么。有什么指点吗?

最佳答案

由于您设置不正确而导致系统库代码崩溃的情况并不罕见。这可能是您的 UIWindow 或其内容 View 或您的 View Controller 实例未保留或以某种方式释放。

如果你的 Controller 不在身边,shouldrotate 方法将无济于事。

rpetrich 的建议对于识别哪个对象被释放可能仍然有用。

对于特别棘手的问题,您可以将release、retain和dealloc方法(记录并调用super)添加到您的可疑类中,然后查看是什么释放了它。记录 -retaincount 以进行跟踪(我仅将其用于诊断目的,而不是在运输应用程序中进行内存管理)。

关于iPhone 开发 - 当我更改方向时我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/475911/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com