Popover没有大小

时间:2011-10-27 05:48:41

标签: objective-c size uipopovercontroller popover

我创建了一个popover类,我可以轻松调用它来获得基本的弹出窗口 - 你给它数据并设置大小,它应该完成剩下的工作。

直到iOS5,这个工作正常,现在弹出窗口,但只有边框,没有空格或内容。

我进行了搜索和搜索,你可以提出的任何想法都会很棒。

@protocol BasicPickerDelegate
 - (void)basicPickerItemSelected:(NSMutableDictionary *)thisDic; 
@end


@interface BasicPickerController : UITableViewController {

 // data

 IBOutlet NSMutableArray *theData;

 // stuff to set 

 IBOutlet int myWidth;
 IBOutlet int myHeight;

 IBOutlet int myPopoverNum;

 // delegate
 id<BasicPickerDelegate> delegate;      

}

 @property (nonatomic, retain) IBOutlet NSMutableArray *theData;

 @property (nonatomic, assign) IBOutlet int myWidth; 
 @property (nonatomic, assign) IBOutlet int myHeight;
 @property (nonatomic, assign) IBOutlet int myPopoverNum;

 @property (nonatomic, assign) id<BasicPickerDelegate> delegate;

 - (void)setSize;
 - (void)checkData;

@end

然后setSize函数是viewDidLoad

- (void)setSize {

    if (!myWidth || !myHeight) {

      NSLog(@"WIDTH AND HEIGHT NOT SET, SETTING DEFAULTS");
      myWidth = 100;
      myHeight = 100;

    }

    self.contentSizeForViewInPopover = CGSizeMake(myWidth, myHeight);
}

然后我称之为:

- (IBAction)showBasicPopover:(id)sender {

    // Show Basic Popover - can be reused

    if (basicPicker != nil) {

        self.basicPicker = nil;
        [self.basicPicker release];

    }

    self.basicPicker = [[[BasicPickerController alloc] initWithStyle:UITableViewStylePlain] autorelease];

    basicPicker.delegate = self;

    self.basicPickerPopover = [[[UIPopoverController alloc] 
                                    initWithContentViewController:basicPicker] autorelease];      

    // Give popover the data it needs

    NSMutableDictionary *sizeDic = [self returnPopoverSize:[sender tag]];

    self.basicPicker.myHeight = [[sizeDic objectForKey:@"height"] intValue];
    self.basicPicker.myWidth = [[sizeDic objectForKey:@"width"] intValue];
    self.basicPicker.myPopoverNum = [sender tag];

    [basicPicker viewDidLoad];

    self.basicPicker.theData = [self returnPopoverData:[sender tag]];

    NSLog(@"giving popover dic (%d) with %d derps", [sender tag], [self.basicPicker.theData count]);

    // Set display settings and show popover

    [basicPicker viewWillAppear:YES];

    CGRect popoverRect = [self.view convertRect:[sender frame] 
                                       fromView:[sender superview]];

    [self.basicPickerPopover presentPopoverFromRect:popoverRect 
                                             inView:self.view 
                           permittedArrowDirections:UIPopoverArrowDirectionAny 
                                           animated:YES];

}

当我运行它时,会出现WIDTH AND HEIGHT NOT SET, SETTIN DEFAULTS对话框。由于某种原因,它不会读取它给出的值。虽然有一些摆弄,即使我可以让它读取它,但不认为它们是有效的并且覆盖它们。

编辑:基本上如此:

在viewDidLoad中调用setSize时,它不知道宽度和高度是多少。所以它设置了默认值。

如果未在viewDidLoad中调用setSize,则会出现“无大小” - 即它具有弹出边框但根本没有内容。

当在viewWillAppear,viewDidAppear或类似的东西(在viewDidLoad之后)调用setSize时,它实际上并没有设置弹出窗口的大小。

1 个答案:

答案 0 :(得分:0)

终于想通了。

我需要在创建弹出窗口后指定宽度/高度变量,但初始化之前

以下修订代码:

self.basicPicker = [[[BasicPickerController alloc] initWithStyle:UITableViewStylePlain] autorelease];

// Give popover the data it needs

basicPicker.delegate = self;

NSMutableDictionary *sizeDic = [self returnPopoverSize:[sender tag]];

self.basicPicker.myHeight = [[sizeDic objectForKey:@"height"] intValue];
self.basicPicker.myWidth = [[sizeDic objectForKey:@"width"] intValue];
self.basicPicker.myPopoverNum = [sender tag];

// Now init

self.basicPickerPopover = [[[UIPopoverController alloc] 
                            initWithContentViewController:basicPicker] autorelease];      

[basicPicker viewDidLoad];