2016年3月31日 星期四

iOS筆記:判斷裝置種類及取得Size資訊 & Super用法

這邊練習如何得知目前device的方向以及種類.

在ViewDidLoad中透過traitCollection屬性判斷裝置的種類.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    if (self.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
        NSLog(@"Pad");
    } else if (self.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
        NSLog(@"Phone");
    }
}

實作- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator,這個方法是當width或是height的regular或compact有變化時呼叫.

iPad的長寬都是regular, 因此iPad方向變化時這個method不會被呼叫.

- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];

    if (newCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
        NSLog(@"Compact width");
    }
    if (newCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular) {
        NSLog(@"Regular width");
    }
    if (newCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
        NSLog(@"Compact Length");
    }
    if (newCollection.verticalSizeClass == UIUserInterfaceSizeClassRegular) {
        NSLog(@"Regular Length");
    }
}

實作- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator:當width或是height的解析度改變時呼叫, 可以在這個方法中透過UIDevice來判斷裝置是橫向還是直向.

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if (orientation == UIDeviceOrientationLandscapeLeft) {
        NSLog(@"橫向, 頂端在左邊");
    }
    if (orientation == UIDeviceOrientationLandscapeRight) {
        NSLog(@"橫向, 頂端在右邊");
    }
    if (orientation == UIDeviceOrientationPortraitUpsideDown) {
        NSLog(@"直向但上下顛倒");
    }
    if (orientation == UIDeviceOrientationPortrait) {
        NSLog(@"直向");
    }
    if (orientation == UIDeviceOrientationUnknown) {
        NSLog(@"方向未知");
    }
}

Super

Assuming that MyClass is a subclass of BaseClass, the following happens when you call

  1. MyClass *mc = [[MyClass alloc] init];
  2. [MyClass alloc] allocates an instance of MyClass.
    The init message is sent to this instance to complete the initialization process.
    In that method, self (which is a hidden argument to all Objective-C methods) is the allocated instance from step 1.
  3. [super init] calls the superclass implementation of init with the same (hidden) self argument. (This might be the point that you understood wrongly.)
  4. In the init method of BaseClass, self is still the same instance of MyClass. This superclass init method can now either
    • Do the base initialization of self and return self, or
    • Discard self and allocate/initialize and return a different object.
  5. Back in the init method of MyClass: self = [super init] is now either
    • The MyClass object that was allocated in step 1, or
    • Something different. (That’s why one should check and use this return value.)
  6. The initialization is completed (using the self returned by the superclass init).
    So, if I understood your question correctly, the main point is that

[super init]
calls the superclass implementation of init with the self argument, which is a MyClass object, not a BaseClass object.

沒有留言:

張貼留言