[UIBarButtonItem superview]:发送到实例的无法识别的选择器

时间:2016-05-12 06:10:34

标签: ios objective-c

我正在使用segue的代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender
{
    menuVC *menuview = [segue destinationViewController];
    CGPoint centerOfButton = [sender.superview convertPoint:sender.center toView:nil];

    [self.circularTransitionHandler transitionWithDestinationViewController:menuview initialTransitionPoint:centerOfButton];
}

App崩溃了:

  

由于未捕获的异常终止应用' NSInvalidArgumentException',原因:' - [UIBarButtonItem superview]:无法识别的选择器发送到实例0x7ffb74293b10 '

2 个答案:

答案 0 :(得分:1)

您是否尝试访问点击按钮?因此,当您尝试让按钮超级查看时,应用程序会崩溃。很遗憾,UIBarButtonItem没有superview财产。您需要使用.view的{​​{1}}。

请更新您的代码:

UIBarButtonItem

请参阅我已更改的参数类型。

答案 1 :(得分:1)

`UIBarButtonItem` does not extend `UIView` thus it has no frame property. 
We can get center point of button like this:


    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIBarButtonItem *)sender
    {
        menuVC *menuview = [segue destinationViewController];
        UIView *barView = [sender valueForKey:@"view"];
        if (barView) {
        CGRect buttonFrame = [barView convertPoint: barView.bounds toView:self.view];   

        CGPoint centerOfButton = CGPointMake(buttonFrame.origin.x + buttonFrame.size.width/2,
                                           buttonFrame.origin.y + buttonFrame.size.height/2);

        [self.circularTransitionHandler transitionWithDestinationViewController:menuview 
                                                      initialTransitionPoint:centerOfButton];
         }
    }