【问题标题】:Popover with no size没有大小的弹出框
【发布时间】:2011-10-27 05:48:40
【问题描述】:

我创建了一个 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 时,它​​实际上并没有设置弹出框的大小。

【问题讨论】:

  • 这是否使用故事板?
  • 你试过[self.basicPickerPopover setPopoverContentSize:self.basicPicker.view.frame.size];了吗? (展示前)

标签: objective-c size uipopovercontroller popover


【解决方案1】:

终于想通了。

我需要在创建弹出框之后分配宽度/高度变量,但初始化它之前。

修改后的代码如下:

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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 2018-02-21
    • 2012-08-28
    相关资源
    最近更新 更多