UIPopover Error:'-[UIPopoverController init] is not a valid initializer. You must call -[UIPopoverController initWithContentViewController:].'

StackOverflow https://stackoverflow.com/questions/21508747

Domanda

I'm trying to create a UIPopover, yet I can't. Here is the code I am using:
iAPAddOn.h:

#import "CodeaAddon.h"
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "InAppPurchasePopoverViewController.h"

id IAPAddOnInstance;

@interface IAPAddOn : UIPopoverController<CodeaAddon>
{
    UIPopoverController* popover;
}

@property (weak, nonatomic) CodeaViewController *codeaViewController;

@end

iApAddOn.m:

#import "lua.h"
#import "IAPAddOn.h"

@implementation IAPAddOn

#pragma mark - Initialisation

- (id)init
{
    self = [super init];
    if (self)
    {
        IAPAddOnInstance = self;

        InAppPurchasePopoverViewController* popoverController = [[InAppPurchasePopoverViewController alloc] initWithNibName:@"InAppPurchasePopover" bundle:nil];
        popover = [[UIPopoverController alloc] initWithContentViewController:popoverController];
    }
    return self;
}

#pragma mark - CodeaAddon Delegate

- (void) codea:(CodeaViewController*)controller didCreateLuaState:(struct lua_State*)L
{
    NSLog(@"IAPAddOn Registering Functions");

    lua_register(L, "showIAPPopOver", showIAPPopOver);

    self.codeaViewController = controller;
}

#pragma mark - Init

static int showIAPPopOver(struct lua_State *state) {
    [IAPAddOnInstance showIAPPopOverAction];

    return 0;
}

- (void) showIAPPopOverAction {
    [popover presentPopoverFromRect:CGRectMake(100, 100, 100, 100) inView:self.codeaViewController.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:true];
}

@end

It results with a crash on startup and logging this in the console:

2014-02-02 01:44:37.131 StackIt[42088:70b] *** Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController init] is not a valid initializer. You must call -[UIPopoverController initWithContentViewController:].'
*** First throw call stack:
(
    0   CoreFoundation                      0x037725e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x031878b6 objc_exception_throw + 44
    2   CoreFoundation                      0x037723bb +[NSException raise:format:] + 139
    3   UIKit                               0x00a86712 -[UIPopoverController init] + 59
    4   StackIt                             0x00006fae -[IAPAddOn init] + 78
    5   StackIt                             0x00002c19 -[AppDelegate application:didFinishLaunchingWithOptions:] + 617
    6   UIKit                               0x004b2355 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 309
    7   UIKit                               0x004b2b95 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1536
    8   UIKit                               0x004b73a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    9   UIKit                               0x004cb87c -[UIApplication handleEvent:withNewEvent:] + 3447
    10  UIKit                               0x004cbde9 -[UIApplication sendEvent:] + 85
    11  UIKit                               0x004b9025 _UIApplicationHandleEvent + 736
    12  GraphicsServices                    0x04d342f6 _PurpleEventCallback + 776
    13  GraphicsServices                    0x04d33e01 PurpleEventCallback + 46
    14  CoreFoundation                      0x036edd65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    15  CoreFoundation                      0x036eda9b __CFRunLoopDoSource1 + 523
    16  CoreFoundation                      0x0371877c __CFRunLoopRun + 2156
    17  CoreFoundation                      0x03717ac3 CFRunLoopRunSpecific + 467
    18  CoreFoundation                      0x037178db CFRunLoopRunInMode + 123
    19  UIKit                               0x004b6add -[UIApplication _run] + 840
    20  UIKit                               0x004b8d3b UIApplicationMain + 1225
    21  StackIt                             0x0000297d main + 141
    22  libdyld.dylib                       0x03ce770d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Any help would be wonderful.
Thank you!

È stato utile?

Soluzione

Looks like you should have:

- (id)init
{
    InAppPurchasePopoverViewController* purchaseController = [[InAppPurchasePopoverViewController alloc] initWithNibName:@"InAppPurchasePopover" bundle:nil];

    self = [super initWithContentViewController:purchaseController];
    if (self)
    {

    }
    return self;
}

Because currently your class isn't respecting it's superclass requirements and is creating an additional popover controller which is not used properly. Everywhere you use popover should really be using self.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top