1

Normally I set up my delegates like normal:

@interface CAKGameViewController : UIViewController <CAKGameSceneDelegate>

then follow up in the CAKGameScene.h with

@protocol CAKGameSceneDelegate;

@interface CAKGameScene : SKScene <SKPhysicsContactDelegate>
...(properties, etc)
@protocol CAKGameSceneDelegate <NSObject>

@optional
- (void)getStarted;
@end

But as most of us know as a property you have to setup shop like this:

@property (nonatomic, weak, readwrite) id<CAKGameSceneDelegate> myDelegate;

My question is, I want to use the actual SKSceneDelegate (self.delegate, not (self.myDelegate) and go like this:

@interface CAKGameViewController : UIViewController <SKSceneDelegate>

Now this is fine and I can use self.delegate to reference the controller, but the problem is I can't seem to figure out how to customize the CAKGameViewController, i.e. setup the protocol further, or customize the protocol, for my methods and/or properties. :(

Any assistance would be helpful.

Thanks

rezwits
  • 835
  • 7
  • 12
  • I tried using info from this post: http://stackoverflow.com/questions/732701/how-to-extend-protocols-delegates-in-objective-c but to no avail. – rezwits Sep 18 '15 at 00:08
  • and info from this: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html – rezwits Sep 18 '15 at 00:09

2 Answers2

0

I finally found a suitable workaround to use and assign the SKScene delegate. The trick is to cast (of course right)!?

You interface the GameViewController and conform to protocols

in CAKGameViewController.h

@interface CAKGameViewController : UIViewController
<SKSceneDelegate, CAKGameSceneDelegate>

in CAKGameScene.h

@protocol CAKGameSceneDelegate <NSObject>
// any @property;
// or - (void)methods;
@end

in CAKGameViewController.m you assign the delegate using self:

self.scene.delegate = self;  //this is the Apple delegate property

then whenever you want to use

something = self.delegate.property; // <or>
[self.delegate method]; // from the CAKGameSceneDelegate protocol

you cast

something = ((id<CAKGameSceneDelegate>)self.delegate).property; // <or>
[((id<CAKGameSceneDelegate>)self.delegate) method];  // from non-apple protocol

what I did was just make a #define

#define delegateHelper ((id<CAKGameSceneDelegate>)self.delegate)

then I can:

something = delegateHelper.property;
[delegateHelper method];

I just figure if you are going to have a property called delegate, like most cocoa/objective-c objects do, to make things work, the delegate should be the delegate, you shouldn't have to use myDelegate, or something silly. delegateHelper may not be much better, but for me I can live with this workaround.

rezwits
  • 835
  • 7
  • 12
0

You can extend the original delegate in the protocol definition:

@protocol CAKGameSceneDelegate <SKSceneDelegate>
    //extend SKSceneDelegate with new functions here
@end

Or in Swift:

protocol CAKGameSceneDelegate: SKSceneDelegate
{
    //extend SKSceneDelegate with new functions here
}

You do not have to redefine delegate if you do it this way.

avance
  • 1,993
  • 2
  • 21
  • 23