appDelegate 可以是模态视图委托吗?
当我的应用程序启动时,当设置首选项表明用户尚未接受使用条款时,我试图显示服务条款模式视图。
因此,在 ApplicationDidFinishLaunchingWithOptions 中的 appDelegate 中,我有以下代码:
if (TOSAcceptedPrefValue) { //has not been accepted
// Create the root view controller for the navigation controller
TermsOfServiceController *termsOfServiceController = [[TermsOfServiceController alloc]
initWithNibName:@"TermsOfServiceController" bundle:nil];
// Create the navigation controller and present it modally.
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:termsOfServiceController];
termsOfServiceController.delegate = self;
navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[TermsOfServiceController release];
NSLog(@"1");
}
但是,Xcode 指示 termsOfServiceController.delegate = self 是“从不兼容类型 'MyAppAppDelegate *' 分配给 'id'”。
我想我在 AppDelegate header:
@protocol TOSModalViewDelegate
- (void)didAcceptTermsOfService:(NSString *)message;
- (void)didRejectTermsOfService:(NSString *)message;
@end
@interface MyAppAppDelegate : NSObject <UIApplicationDelegate, TOSModalViewDelegate> ...
和 modalview header:
@protocol ModalViewDelegate ;
@interface TermsOfServiceController : UIViewController {
id<ModalViewDelegate> delegate; ...
...
@property (nonatomic, assign) id<ModalViewDelegate> delegate;
中完全实现了模态协议,并且在 modalview 实现文件中综合了它。
根据这个示例,我移动了我的代码在 AppDelegate.m 文件中,在窗口实例化之后,但仍然有来自 Xcode 的警告。
该警告会导致应用程序崩溃并出现以下错误:
2011-09-05 08:34:12.237 MyApp[4416:207] TOSAcceptedPrefValue = 0 2011-09-05 08:34:13.732 MyApp[4416:207] displayWelcomeScreenPrefValue = 0 2011-09-05 08:34:42.889 MyApp[4416:207] -[MyAppAppDelegate presentModalViewController:animated:]:无法识别的选择器发送到实例 0x552b430 2011-09-05 08:34:42.892 MyApp[4416:207] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[MyAppAppDelegate presentModalViewController:animated:]:无法识别的选择器发送到实例 0x552b430”
所以我的问题是,是否可以显示模态从应用程序委托查看,如果是这样,我应该更改什么才能实现它。
感谢您的帮助
I'm trying to have a Terms of Service modal view display when my application launches, when the settings preference indicates the user hasn't accepted the terms of use.
So in my appDelegate in the ApplicationDidFinishLaunchingWithOptions, I have this code:
if (TOSAcceptedPrefValue) { //has not been accepted
// Create the root view controller for the navigation controller
TermsOfServiceController *termsOfServiceController = [[TermsOfServiceController alloc]
initWithNibName:@"TermsOfServiceController" bundle:nil];
// Create the navigation controller and present it modally.
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:termsOfServiceController];
termsOfServiceController.delegate = self;
navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[TermsOfServiceController release];
NSLog(@"1");
}
However, Xcode is indicating that termsOfServiceController.delegate = self is "Assigning to 'id' from imcompatible type 'MyAppAppDelegate *' ".
I think I fully implement the modal protocol in my AppDelegate header:
@protocol TOSModalViewDelegate
- (void)didAcceptTermsOfService:(NSString *)message;
- (void)didRejectTermsOfService:(NSString *)message;
@end
@interface MyAppAppDelegate : NSObject <UIApplicationDelegate, TOSModalViewDelegate> ...
and in the modalview header:
@protocol ModalViewDelegate ;
@interface TermsOfServiceController : UIViewController {
id<ModalViewDelegate> delegate; ...
...
@property (nonatomic, assign) id<ModalViewDelegate> delegate;
and I synthesize it in the modalview implemenation file.
Per this example, I moved my code in the AppDelegate.m file to after the window get instantiated but still have the warning from Xcode.
The warning results in an app crash with this error:
2011-09-05 08:34:12.237 MyApp[4416:207] TOSAcceptedPrefValue = 0
2011-09-05 08:34:13.732 MyApp[4416:207] displayWelcomeScreenPrefValue = 0
2011-09-05 08:34:42.889 MyApp[4416:207] -[MyAppAppDelegate presentModalViewController:animated:]: unrecognized selector sent to instance 0x552b430
2011-09-05 08:34:42.892 MyApp[4416:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyAppAppDelegate presentModalViewController:animated:]: unrecognized selector sent to instance 0x552b430'
So my question is, is it possible to display a modal view from the appdelegate and if so, what should I change to make it happen.
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误是因为
MyAppAppDelegate
不是UIViewController
子类,因此无法处理presentModalViewController:animated:
。所以不,你的应用程序委托不能呈现 modalViewController,它必须由真实的视图控制器呈现。这并不难做到,只需创建一个在
viewDidLoad
中显示您的术语,并在模态控制器退出时做出适当响应,以执行您下一步需要执行的任何操作。The error is because
MyAppAppDelegate
isn't aUIViewController
subclass, and therefore can't handlepresentModalViewController:animated:
.So no, your app delegate can't present a modalViewController, it has to be presented by a real view controller. This isn't hard to do, just create one that shows your terms in
viewDidLoad
, and responds appropriately when the modal controller exits, to do whatever you need to do next.