Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASNodeController+Beta] Provide an option to allow nodes to own their controllers. #61

Merged
merged 3 commits into from
May 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Source/ASNodeController+Beta.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@
#import <AsyncDisplayKit/ASDisplayNode.h>
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h> // for ASInterfaceState protocol

// Until an ASNodeController can be provided in place of an ASCellNode, some apps may prefer to have
// nodes keep their controllers alive (and a weak reference from controller to node)
#define INVERT_NODE_CONTROLLER_OWNERSHIP 0

/* ASNodeController is currently beta and open to change in the future */
@interface ASNodeController<__covariant DisplayNodeType : ASDisplayNode *> : NSObject <ASInterfaceStateDelegate>

#if INVERT_NODE_CONTROLLER_OWNERSHIP
@property (nonatomic, weak) DisplayNodeType node;
#else
@property (nonatomic, strong) DisplayNodeType node;
#endif

- (void)loadNode;

Expand Down
26 changes: 25 additions & 1 deletion Source/ASNodeController+Beta.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,30 @@
//

#import "ASNodeController+Beta.h"

#import "ASDisplayNode+FrameworkPrivate.h"

#if INVERT_NODE_CONTROLLER_OWNERSHIP

@interface ASDisplayNode (ASNodeController)
@property (nonatomic, strong) ASNodeController *asdkNodeController;
@end

@implementation ASDisplayNode (ASNodeController)

- (ASNodeController *)asdkNodeController
{
return objc_getAssociatedObject(self, @selector(asdkNodeController));
}

- (void)setAsdkNodeController:(ASNodeController *)asdkNodeController
{
objc_setAssociatedObject(self, @selector(asdkNodeController), asdkNodeController, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

#endif

@implementation ASNodeController

@synthesize node = _node;
Expand Down Expand Up @@ -49,6 +70,9 @@ -(void)setNode:(ASDisplayNode *)node
{
_node = node;
_node.interfaceStateDelegate = self;
#if INVERT_NODE_CONTROLLER_OWNERSHIP
_node.asdkNodeController = self;
#endif
}

// subclass overrides
Expand Down