Search code examples
objective-ciosuiinterfaceorientation

the return value of a UIInterfaceOrientation object is equal to a boolean var?


This is a rather basic question regarding the return value from a simple UIInterfaceOrientation object, I try this code:

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
BOOL orientacion = interfaceOrientation;
return orientacion;
}

and the conversion does it, so I thought a UIInterfaceOrientation object is equal to a boolean var?? is that a implicit typo or really UIInterfaceOrientation is equal to a boolean value..


Solution

  • UIInterfaceOrientation is an enum, which essentially means it's an integer. Integers can be assigned to booleans. Many things can--booleans simply equate to true or false. If a boolean is set equal to 0 or nil, it is false. If it is set to anything other than 0 or nil (or some other #defined equivalent) it will be true. Since UIInterfaceOrientation is an enum (an integer), if it is equal to 0 the boolean will be false. If it is anything but 0 it will be true.

    The values of UIInterfaceOrientation:

    typedef enum {
        UIDeviceOrientationUnknown,
        UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
        UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
        UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
        UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
        UIDeviceOrientationFaceUp,              // Device oriented flat, face up
        UIDeviceOrientationFaceDown             // Device oriented flat, face down
    } UIDeviceOrientation;
    

    The first on this list will equal 0. The next 1, the next 2, etc. So UIDeviceOrientationUnknown will set the boolean to false; anything else will set it to true.


    In any case, you're not using this function correctly. The code inside this function needs to read:

    if((interfaceOrientation == someOrientationYouWantToWork) || (interfaceOrientation == someOtherOrientationYouWantToWork)
    {
        return YES;
    }
    else
    {
        return NO;
    }
    

    Set someOrientationYouWantToWork etc to values from the enums I posted above. Whichever orientations you want to work, return YES for. Else it will return NO.