开发者

Can appDelegate be a modal view delegate?

开发者 https://www.devze.com 2023-04-02 07:15 出处:网络
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.

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 = UIModalTransitionSty开发者_如何学PythonleCrossDissolve;
    [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


The error is because MyAppAppDelegate isn't a UIViewController subclass, and therefore can't handle presentModalViewController: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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号