0

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

Community
  • 1
  • 1
Jazzmine
  • 1,837
  • 8
  • 36
  • 54

1 Answers1

0

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.

Paul Lynch
  • 19,769
  • 4
  • 37
  • 41
  • Ok, that's cool. Thanks for the explanation! So you're saying, have my normal first view controller (a profile view) display via the AppDelegate but immediately, the profile view controller will check in it's viewdidload to see if the terms of service needs to be displayed? – Jazzmine Sep 05 '11 at 14:17
  • That's right. Details depend, as ever, on exactly what you need to do. – Paul Lynch Sep 05 '11 at 14:46