【问题标题】:Assertion failure in -[UIActionSheet showInView:]-[UIActionSheet showInView:] 中的断言失败
【发布时间】:2012-03-02 16:35:56
【问题描述】:

有人知道这意味着什么以及如何解决吗?

* -[UIActionSheet showInView:] 中的断言失败,/SourceCache/UIKit_Sim/UIKit-1912.3/UIActionSheet.m:4630 2012-03-02 17:12:34.643 Strategy & Risk[66854:15803] * 终止应用程序由于 未捕获的异常“NSInternalInconsistencyException”,原因: '无效参数不满足:view != nil'

更新:它只是偶尔发生:

更新 2:我的应用中没有标签栏或工具栏

更新 3:我已更改代码以使用 showInView: 并且收到完全相同的错误消息。

- (void)displayAddEntityActionSheet {

    //Convert the tap location to this view's coordinate systems
    CGRect buttonTappedRect = [self.currentNodeView convertRect:self.currentNodeView.frame toView:self.view];

    UIActionSheet *actionSheet;

    switch (self.currentNode.nodeType) {
        case NodeTypeEntity:
            actionSheet = [[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil 
                                        destructiveButtonTitle:nil otherButtonTitles:@"Entity", @"Objective", nil] autorelease];

            [actionSheet showFromRect:buttonTappedRect inView:self.view animated:YES];
            break;
        case NodeTypeObjective:
            actionSheet = [[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil 
                                        destructiveButtonTitle:nil otherButtonTitles:@"Risk", @"KPI", nil] autorelease];

            [actionSheet showFromRect:buttonTappedRect inView:self.view animated:YES];
            break;
        case NodeTypeRisk:
            actionSheet = [[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil 
                                        destructiveButtonTitle:nil otherButtonTitles:@"KRI", @"Control", nil] autorelease];

            [actionSheet showFromRect:buttonTappedRect inView:self.view animated:YES];
            break;
        case NodeTypeControl:
            actionSheet = [[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil 
                                        destructiveButtonTitle:nil otherButtonTitles:@"KCI", nil] autorelease];

            [actionSheet showFromRect:buttonTappedRect inView:self.view animated:YES];
            break;
        default:
            break;
    }
}

【问题讨论】:

  • 你能告诉我们你创建actionSheet的代码吗?
  • 除非您发布一些代码,否则我们无话可说。
  • @TheLearner 请参考我的回答,我认为这个回答会帮助你解决这个断言失败。

标签: iphone objective-c cocoa-touch


【解决方案1】:

如果您的应用程序中有标签栏,则应使用以下方法显示操作表。

[actionsheet showFromTabBar:]

[actionsheet showFromToolbar:]

您面临的此断言失败是因为您的应用程序中可能有一个标签栏,而在 iPhone 中您可能正在使用以下方法显示操作表:

[actionsheet showInView:]

如果是这样,那么你应该使用:

[actionsheet showFromTabBar:]

希望对你有所帮助。

EDIT-2:

[actionsheet showFromBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES];

EDIT-3:

UIActionSheet Crashes on iPad / not iPhone

http://forums.macrumors.com/showthread.php?t=997125

【讨论】:

  • 我不希望它出现在标签栏或工具栏中
  • @TheLearner:它不会从 tabBar 出现,它会正常显示,但在系统级别通知操作表它后面有一个标签栏。
  • 那么它会出现在正确的位置而不指定任何矩形坐标?
  • 我刚刚检查了我的应用中没有标签栏或工具栏,只有导航栏
  • 那么你可以使用[actionsheet showFromBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES];
【解决方案2】:

终于发现了导致错误的原因,下面的代码被多次调用,这反过来又调用了我原来帖子中的方法,这个视图控制器从未被释放,但即使它是我作为观察者没有删除它对于这些通知:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNewEntity:) name:@"addSubEntity" object:nil];
    }

    return self;
}

所以我已经修复了反复调用 init 的错误,为了安全起见,我还调用了 removeObserver:以防万一,虽然这个视图控制器被重复使用,所以它永远不会被调用。

故事的寓意:在释放之前 removeObserver!

- (void)viewDidUnload
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

【讨论】:

    【解决方案3】:

    你还没有说你确定self.view != nil。似乎很清楚这是首先要做的事情。或者检查何时挂起调试器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 2017-08-07
      • 2012-08-01
      • 2015-09-01
      相关资源
      最近更新 更多