Search code examples
iosobjective-ciphonecore-motiontarget-action

CoreMotion [ViewController updateReferenceAttitude:] - why is the selector not recognised?


I’m new to CoreMotion. My first program shows deviceManager changing values for pitch, roll and yaw but crashes when it tries to set referenceAttitude

    - (void)updateReferenceAttitude
    {
        self.refAttitude = self.manager.deviceMotion.attitude;
        NSLog(@"you clicked a button that took a 3D snapshot");
    }

In debug, the statement in my method

    [myButton addTarget:self action:@selector(updateReferenceAttitude:) forControlEvents:UIControlEventTouchUpInside];

crashes with the following

    'NSInvalidArgumentException', reason: '-[ViewController updateReferenceAttitude:]: unrecognized selector sent to instance 0x14875a00'

To test it, I use Xcode 8.3.3 with an iPhone 5 C running iOS 10.3.3.

Can anyone explain why this selector is not recognised ?


Solution

  • The problem is fixed and had little to do with CoreMotion. The method

    - (void)updateReferenceAttitude
    

    should be

    - (void)updateReferenceAttitude:(UIButton *(sender
    

    I had included a UIButton to reset the reference attitude while the app is running but was calling the same method automatically after ViewDidLoad launches.

    The problem was solved by using a statement for the initial reference instead of calling the method.

        if (self.refAttitude == nil)
        {
            self.refAttitude = self.manager.deviceMotion.attitude;  // initial run
            //        [self updateReferenceAttitude];
        }