在iOS 5中使用setContentViewController时应用程序崩溃

时间:2011-11-14 11:09:07

标签: iphone cocoa-touch ios5

我正在使我的应用程序与iOS 5兼容,但应用程序崩溃了我使用setContentViewController的代码。

这是我的代码。

[[ChoicesViewController sharedChoices] setCurrentValue:[[(UIButton *)sender titleLabel]  text]];

[self.choicesPopoverController setContentViewController:[ChoicesViewController sharedChoices]];

CGFloat popoverHeight = 44.0f * [[[ChoicesViewController sharedChoices] choices] count];

[self.choicesPopoverController setPopoverContentSize:CGSizeMake(380.0f, MIN(400.0f, popoverHeight))];

if ([self.choicesPopoverController isPopoverVisible]) {

    [self.choicesPopoverController dismissPopoverAnimated:YES];

} else {

    [self.choicesPopoverController presentPopoverFromRect:[(UIButton *)sender frame]

                                                   inView:self.view

                                 permittedArrowDirections:UIPopoverArrowDirectionAny

                                                 animated:YES];

}

这是共享选择返回的内容:

static ChoicesViewController *_sharedChoices = nil;

 +(id)sharedChoices 
   {

    if (!_sharedChoices) 

        {
        _sharedChoices = [[[self class] alloc] init];
    }

    return _sharedChoices;
    }

//当我评论下面的代码时,应用程序不会在iOS 5中崩溃,但UIPopover也没有显示。如果我取消注释它将在iOS 5中崩溃。

-(UIPopoverController *)choicesPopoverController 
{
if (!choicesPopoverController) 
     {

        choicesPopoverController = [[UIPopoverController alloc] initWithContentViewController:self];
     }

  return choicesPopoverController;
 }

2 个答案:

答案 0 :(得分:1)

你说你设置断点并发现这一行是问题所在:

[self.choicesPopoverController setContentViewController:[ChoicesViewController sharedChoices]];

但那里有一些事情发生。如果将该行更改为

,它会在哪里崩溃
id controller = self.choicesPopoverController;
id shared = [ChoicesViewController sharedChoices];
[controller setContentViewController:shared];

答案 1 :(得分:0)

最后我找到了解决方案:

而不是写

[self.choicesPopoverController setContentViewController:[ChoicesViewController sharedChoices]];

我做了

choicesPopoverController = [[UIPopoverController alloc] initWithContentViewController:[ChoicesViewController sharedChoices]];

并注释掉了这段代码

/ *

-(UIPopoverController *)choicesPopoverController 
  {
    if (!choicesPopoverController) 
      {

          choicesPopoverController = [[UIPopoverController alloc] initWithContentViewController:self];
      }

   return choicesPopoverController;
 }

* /

现在它在iOS 5中没有崩溃。