在Sinch的演示中有什么用SINUIViewController?

时间:2016-07-03 07:25:23

标签: ios sinch

我是Sinch的新手。我刚下载了用于调用的演示文件,我只是好奇这个文件的用途。特别是文件下面的最后几行。在我看来,这个视图控制器涉及在呼叫结束和开始时显示适当的视图控制器,但我不确定。

谢谢!

#import "SINUIViewController.h"

#import <objc/runtime.h>

// used for associated object references to simulate property-like storage for
// this category
static char sin_deferredDismissalKey;

@implementation SINUIViewController

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  if ([self.view window] == nil) {
    _isAppearing = NO;
    _isDisappearing = NO;
  }
}

- (void)viewWillAppear:(BOOL)animated {
  _isAppearing = YES;
  [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  _isAppearing = NO;
  [self dismissIfNecessary];
}

- (void)viewWillDisappear:(BOOL)animated {
  _isDisappearing = YES;
  [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
  [super viewDidDisappear:animated];
  _isAppearing = NO;
}

#pragma mark - Dismissal

- (void)dismiss {
  if ([self isDisappearing]) {
    return;
  } else if ([self isAppearing]) {
    [self setShouldDeferredDismiss:YES];
    return;
  }
  [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)dismissIfNecessary {
  if ([self shouldDeferrDismiss]) {
    [self setShouldDeferredDismiss:NO];
    dispatch_async(dispatch_get_main_queue(), ^{
      [self dismiss];
    });
  }
}

- (BOOL)shouldDeferrDismiss {
  return [self sin_getAssociatedBOOLForKey:&sin_deferredDismissalKey];
}

- (void)setShouldDeferredDismiss:(BOOL)v {
  [self sin_setAssociatedBOOL:v forKey:&sin_deferredDismissalKey];
}

#pragma mark -

- (BOOL)sin_getAssociatedBOOLForKey:(const void *)key {
  NSNumber *v = (NSNumber *)objc_getAssociatedObject(self, key);
  return v ? [v boolValue] : NO;
}

- (void)sin_setAssociatedBOOL:(BOOL)v forKey:(const void *)key {
  objc_setAssociatedObject(self, key, [NSNumber numberWithBool:v], OBJC_ASSOCIATION_COPY);
}

@end

1 个答案:

答案 0 :(得分:1)

这个类只是一个基类助手viewcontroller,可以帮助你更轻松地解除viewcontroller。一些viewcontroller将从这个类继承。

- (BOOL)sin_getAssociatedBOOLForKey:(const void *)key
- (void)sin_setAssociatedBOOL:(BOOL)v forKey:(const void *)key

以上方法使用名为Associated Object的技术来避免声明属性。您可以在此处详细了解Associated Object http://nshipster.com/associated-objects/