此代码适用于 iOS4.3,不适用于 iOS5
以下代码在 iOS4.3 iPhone 和 iPad 以及 iOS5 iPhone 上按预期工作,但在 iOS5 iPad 上崩溃。当应用程序的先前运行被取消时,这是我在基于选项卡的应用程序中调用的第一个视图所调用的。用户可以继续之前的运行或重置。我尝试将此代码移至 -viewWillAppear 和 -viewDidAppear 割线,但无济于事。还有其他人经历过吗? (顺便说一句,我已经确认了 XIB 中的所有连接,以确保此 UniversalApp 的 iPad 部分没有问题。
- (void)viewDidLoad {
BOOL continueYesNo;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
continueYesNo = [prefs boolForKey:@"keyContinueMeeting"];
if (continueYesNo) {
NSString *message_continue = [[NSString alloc] initWithFormat:@"Do you want to Continue the Prior event"];
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:message_continue
delegate:self
cancelButtonTitle:@"Reset"
destructiveButtonTitle:@"Continue Meeting"
otherButtonTitles:nil];
[actionSheet showFromTabBar:self.tabBarController.tabBar]; // This is the line that crashes. The program checks for a terminated APP and status in preferences
}
else {
resetStuff.hidden = YES;
}
}
这是崩溃的文本 -
Current language: auto; currently objective-c
2011-10-12 21:02:55.530 浪费时间[ 3884:17603] * -[UIActionSheet showInView:] 中断言失败, /SourceCache/UIKit_Sim/UIKit-1912.3/UIActionSheet.m:4630 2011-10-12 21:02:55.532 应用程序名称[3884:17603] 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效参数不满足:视图” !=零' ** 首先抛出调用堆栈: (0x1d87052 0x1f18d0a 0x1d2fa78 0x2742db 0x90b47e 0x90b122 0x90b356 0x90b653 0x6fe1 0x60064e 0x61bb89 0x61b9bd 0x619f8a 0x619e2f 0x617ffb 0x61885a 0x601fbf 0x60221b 0x6030f1 0x571fec 0x577572 0x57172b 0x2b86 0x5389d6 0x5398a6 0x548743 0x5491f8 0x53caa9 0x2315fa9 0x1d5b1c5 0x1cc0022 0x1cbe90a 0x1cbddb4 0x1cbdccb 0x5392a7 0x53aa9b 0x2abd 0x2325) 终止调用抛出异常(gdb
块引用
这是委托 .h
#import <UIKit/UIKit.h>
@interface W_AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UITabBarController *rootController;
float floatVal;
NSDate *currentTime;
NSDate *previousTime;
NSDate *currentQTime;
NSDate *previousQTime;
BOOL continueMeeting;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *rootController;
@property (nonatomic, readwrite) float floatVal;
@property (nonatomic, readwrite) BOOL continueMeeting;
@property (nonatomic, retain) NSDate *currentTime;
@property (nonatomic, retain) NSDate *previousTime;
@property (nonatomic, retain) NSDate *currentQTime;
@property (nonatomic, retain) NSDate *previousQTime;
@end
和 .m
#import "W.h"
@implementation W_AppDelegate
@synthesize window;
@synthesize rootController;
@synthesize floatVal;
@synthesize currentTime;
@synthesize previousTime;
@synthesize currentQTime;
@synthesize previousQTime;
@synthesize continueMeeting;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:rootController.view];
[window makeKeyAndVisible];
return YES;
}
@end
The following code works as expected on iOS4.3 iPhone and iPad, and iOS5 iPhone, but crashes on iOS5 iPad. This is called for the first view that I call in a Tab based application, when the prior run of the application was cancelled. The user can continue the prior run or reset. I have tried moving this code to both the -viewWillAppear and -viewDidAppear secant, to no avail. Has anyone else experienced this? (Btw, I have confirmed all the connections in my XIBs to make sure it wasn't a problem with iPad parts of this UniversalApp.
- (void)viewDidLoad {
BOOL continueYesNo;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
continueYesNo = [prefs boolForKey:@"keyContinueMeeting"];
if (continueYesNo) {
NSString *message_continue = [[NSString alloc] initWithFormat:@"Do you want to Continue the Prior event"];
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:message_continue
delegate:self
cancelButtonTitle:@"Reset"
destructiveButtonTitle:@"Continue Meeting"
otherButtonTitles:nil];
[actionSheet showFromTabBar:self.tabBarController.tabBar]; // This is the line that crashes. The program checks for a terminated APP and status in preferences
}
else {
resetStuff.hidden = YES;
}
}
Here is the text of the crash -
Current language: auto; currently objective-c
2011-10-12 21:02:55.530 Wasted Time[3884:17603] * Assertion failure in -[UIActionSheet showInView:], /SourceCache/UIKit_Sim/UIKit-1912.3/UIActionSheet.m:4630
2011-10-12 21:02:55.532 APP NAME[3884:17603] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: view != nil'
** First throw call stack:
(0x1d87052 0x1f18d0a 0x1d2fa78 0x2742db 0x90b47e 0x90b122 0x90b356 0x90b653 0x6fe1 0x60064e 0x61bb89 0x61b9bd 0x619f8a 0x619e2f 0x617ffb 0x61885a 0x601fbf 0x60221b 0x6030f1 0x571fec 0x577572 0x57172b 0x2b86 0x5389d6 0x5398a6 0x548743 0x5491f8 0x53caa9 0x2315fa9 0x1d5b1c5 0x1cc0022 0x1cbe90a 0x1cbddb4 0x1cbdccb 0x5392a7 0x53aa9b 0x2abd 0x2325)
terminate called throwing an exception(gdb
Blockquote
Here's the Delegate .h
#import <UIKit/UIKit.h>
@interface W_AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UITabBarController *rootController;
float floatVal;
NSDate *currentTime;
NSDate *previousTime;
NSDate *currentQTime;
NSDate *previousQTime;
BOOL continueMeeting;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *rootController;
@property (nonatomic, readwrite) float floatVal;
@property (nonatomic, readwrite) BOOL continueMeeting;
@property (nonatomic, retain) NSDate *currentTime;
@property (nonatomic, retain) NSDate *previousTime;
@property (nonatomic, retain) NSDate *currentQTime;
@property (nonatomic, retain) NSDate *previousQTime;
@end
And the .m
#import "W.h"
@implementation W_AppDelegate
@synthesize window;
@synthesize rootController;
@synthesize floatVal;
@synthesize currentTime;
@synthesize previousTime;
@synthesize currentQTime;
@synthesize previousQTime;
@synthesize continueMeeting;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:rootController.view];
[window makeKeyAndVisible];
return YES;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试调用你的[super viewDidLoad];第一的。您的视图可能还没有设置得足以推动 actionSheet。
Try and call your [super viewDidLoad]; first. Your view probably isn't set up enough to push the actionSheet yet.
您是否为该项目启用了 ARC(自动引用计数)?如果没有 ARC,您就会泄漏 if 语句主分支中的对象。
启用 ARC 后,它将为您释放这些对象,这可能是您没有预料到的,这会导致问题。
编辑:您的标签栏似乎尚未加载(或者:已加载但现在已卸载)。如果 ARC 认为它不应该再存在的话,就会发生这种情况。当您调用 actionSheet 来呈现自身时,请检查以确保 self.tabBarController 指向真实的内容。
我可以这么说,因为抛出了异常,它或多或少是在说“你试图在零视图中呈现它”。那是程序员的错误。我的猜测是,您之前在其他地方泄漏了 tabBarController,现在您已经使用了 ARC,该泄漏会自动为您清理,并且 tabBarController 正在被释放。
Have you turned on ARC (Automatic Reference Counting) for this project? Without ARC, you're leaking objects in the main branch of your if statement.
With ARC enabled, it's going to be releasing those objects for you, which you probably didn't expect, and that's going to cause problems.
Edit: it looks like your tab bar isn't loaded yet (OR: was loaded and now has become unloaded). This would happen if ARC thought it shouldn't stick around any longer. Check to make sure
self.tabBarController
points to something real when you call the actionSheet to present itself.I can tell this because of the exception being thrown, it's saying "you're trying to present it in a nil view", more or less. That's a programmer error. My guess is, you were leaking the tabBarController somewhere else before, and now that you've used ARC, that leak is automatically cleaned up for you, and the tabBarController is getting deallocated.
从错误文本:
[UIActionSheet showInView:]
的view != nil
来看,视图为零。actionSheet
或self.tabBarController.tabBar
为 nil。添加一些 NSLogs 以验证两者都不为零。让事情变得更简单,将: 替换
为
How is your delegate setup?
From the error text:
view != nil
from[UIActionSheet showInView:]
, the view is nil. Either theactionSheet
orself.tabBarController.tabBar
is nil. Add some NSLogs to verify neither is nil.Make things a little esier, replace:
with
How is your delegate setup?