0

我创建了一个 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,它会出现“无大小”——即它有弹出框边框但里面根本没有内容。

当 setSize 在 vi​​ewWillAppear、viewDidAppear 或类似的东西中调用时(在 viewDidLoad 之后),它实际上并没有设置弹出框的大小。

4

1 回答 1

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];
于 2011-11-02T04:31:49.793 回答