initWithCoder not working as expected in Xcode 6? - objective-c

I am using Sprite Kit engine. I used to write code in initWithSize but since the update, initWithSize is not called automatically, instead viewDidLoad is called. I got to know that instead of initWithSize, initWithCoder had to be used, so i changed the code from:
-(id) initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor blueColor];
SKSpriteNode *paddle = [SKSpriteNode spriteNodeWithImageNamed:#"paddle"];
paddle.position = CGPointMake(150, 100);
paddle.physicsBody.dynamic = NO;
[self addChild:paddle];
}
return self;
}
to:
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.backgroundColor = [SKColor blueColor];
SKSpriteNode *paddle = [SKSpriteNode spriteNodeWithImageNamed:#"paddle"];
paddle.position = CGPointMake(150, 100);
paddle.physicsBody.dynamic = NO;
[self addChild:paddle];
}
return self;
}
Now, the color of the scene turns blue, but i am not able to add sprites to the scene. Neither do I get any error while building.
Are there some changes that I should make in initWithCoder? (If yes, please specify and elaborate)
Can initWithCoder be used with EXACTLY the same code as we did in initWithSize?
PS: Heres the full code I have in GameScene.m and I do not have any other scene.
#import "GameScene.h"
#implementation GameScene
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
SKSpriteNode *paddle = [SKSpriteNode spriteNodeWithImageNamed:#"paddle"];
paddle.position = CGPointMake(150, 100);
paddle.physicsBody.dynamic = NO;
[self addChild:paddle];
}
return self;
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
#end
And I just copied and pasted the code from initWithSize to initWithCoder. It doesn't seem to create the paddle object.
Please help!

It may be a method signature mismatch?
Instead of instancetype it is defined as: - (id)initWithCoder:(NSCoder *)decoder
I was just looking up if I could have the type safety of instance type in my keyed coding implementations and found your question.
Others say it works. It would be worth a test.
Why does initWithCoder not return instancetype?

Related

Methods in another scene not called

The 'thePlay' gets presented but does not display the redColor background. It remains black. Can someone advice on what I am supposed to do.
More Clarification: All the scenes and quickMenu class are imported on each other
This method execute transition from this scene(gameScene) using a button in quickMenu class (subclass of SKNode)
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches){
if (self.gameEnded && quickMenu.touchable) {
SKNode *theNode = [quickMenu nodeAtPoint:[touch locationInNode:quickMenu]];
if ([theNode.name isEqualToString:#"theMenu"]){
SKScene *thePlay = [[SKScene alloc] initWithSize:self.size];
SKTransition *doors = [SKTransition doorsOpenHorizontalWithDuration:1.0];
[self.view presentScene:thePlay transition:doors];
}
}
}
}
this is the first method in another scene (thePlay scene),
-(void)didMoveToView:(SKView *)view
{
self.backgroundColor = [SKColor redColor];
NSLog(#"this method called");
}
I tried the init method, did not show the redColor background as well, as shown below...
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor redColor];
NSLog(#"init method called");
}
Am I missing something?

Animating custom property

I'm trying to animate a custom property, and as I've seen on several sources it seems this would be the way to go, but I'm missing something. Here's the CALayer subclass:
#implementation HyNavigationLineLayer
#dynamic offset;
- (instancetype)initWithLayer:(id)layer
{
self = [super initWithLayer:layer];
if (self) {
HyNavigationLineLayer * other = (HyNavigationLineLayer*)layer;
self.offset = other.offset;
}
return self;
}
-(CABasicAnimation *)makeAnimationForKey:(NSString *)key
{
// TODO
return nil;
}
- (id<CAAction>)actionForKey:(NSString *)event
{
if ([event isEqualToString:#"offset"]) {
return [self makeAnimationForKey:event];
}
return [super actionForKey:event];
}
+ (BOOL)needsDisplayForKey:(NSString *)key
{
if ([key isEqualToString:#"offset"]) {
return YES;
}
return [super needsDisplayForKey:key];
}
- (void)drawInContext:(CGContextRef)ctx
{
NSLog(#"Never gets called");
}
#end
I believe this is the only relevant method on my view:
#implementation HyNavigationLineView
+ (Class)layerClass
{
return [HyNavigationLineLayer class];
}
#end
And, finally, in my view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
// Instantiate the navigation line view
CGRect navLineFrame = CGRectMake(0.0f, 120.0f, self.view.frame.size.width, 15.0f);
self.navigationLineView = [[HyNavigationLineView alloc] initWithFrame:navLineFrame];
// Make it's background transparent
self.navigationLineView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.0f];
self.navigationLineView.opaque = NO;
[[self.navigationLineView layer] addSublayer:[[HyNavigationLineLayer alloc] init]];
[self.view addSubview:self.navigationLineView];
}
The thing is that the drawInContext method is not called at all, although layerClass is. Am I missing something to make the layer draw?
Solved it. Need to call setNeedsDisplay
- (void)viewDidLoad
{
[super viewDidLoad];
// Instantiate the navigation line view
CGRect navLineFrame = CGRectMake(0.0f, 120.0f, self.view.frame.size.width, 15.0f);
self.navigationLineView = [[HyNavigationLineView alloc] initWithFrame:navLineFrame];
// Make it's background transparent
self.navigationLineView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.0f];
self.navigationLineView.opaque = NO;
[[self.navigationLineView layer] addSublayer:[[HyNavigationLineLayer alloc] init]];
[self.view addSubview:self.navigationLineView];
[self.navigationLineView.layer setNeedsDisplay];
}

Which method gets called every time a subclassed uibutton gets called?

I subclassed a UIButton, and I want to give all instances of my newButton the same background image, among other things. I thought I found the right init method in initWithCoder, but it just gets called for the first newButton. I did a little test by changing the text size to something massive, and only the first newButtons text size was changed.
I can use drawRect:, and it works just fine. But I was told that is not a good method to use.
Does anyone know which method will get called, so that I can make some adjustments?
#implementation LCHButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setDefaults];
}
return self;
}
-(id)init
{
self = [super init];
if (self) {
[self setDefaults];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setDefaults];
}
return self;
}
-(void)setDefaults
{
[self setBackgroundImage:[[UIImage imageNamed:#"submitBtn.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont fontWithName:#"Museo-500" size:17];
}
#end
This will change only the first button

Subclass of UIView. How to add through Interface Builder?

I have a subclass of UITextField and want to be able to add it to the view in IB. What I did is added UITextField and changed its class to my subclass in Identity tab of Inspector. But I can only see UITextField on the view.
Code:
#interface ExtendedTextField : UITextField {
}
//implementation
#implementation ExtendedTextField
- (void)baseInit
{
UIImage * curImage = [UIImage imageNamed:#"tfbg.png"];
[self baseInitWithImage : curImage];
}
- (void)baseInitWithImage : (UIImage *) aImage
{
aImage = [aImage stretchableImageWithLeftCapWidth:29 topCapHeight:29];
self.background = aImage;
self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
UIView * curLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, self.frame.size.height)];
self.leftView = curLeftView;
self.rightView = curLeftView;
[curLeftView release];
self.leftViewMode = UITextFieldViewModeAlways;
self.rightViewMode = UITextFieldViewModeAlways;
[self setTextColor:[UIColor greenColor]];
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self baseInit];
}
/*
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self baseInit];
}
return self;
}
*/
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self)
{
[self baseInit];
}
return self;
}
- (id)initWithFrame:(CGRect)frame withImage:(UIImage*)aImage
{
self = [super initWithFrame:frame];
if (self) {
[self baseInitWithImage : aImage];
}
return self;
}
- (void)dealloc {
[super dealloc];
}
#end
EDIT: I noticed several things:
-- if I put [super initWithFrame:...] instead of [super initWithCoder:...] inside
(id)initWithCoder:(NSCoder *)aDecoder
it works well.
-- Now I am using awakefromnib instead of initwithcoder and the only thing that get changed in textfield is textColor.
Could someone explain why so?
Solution I needed to set border style to none. BS overlayed bg image.
While showing your .xib in Xcode, reveal the right pane, select your view and in the third tab, change Classto whatever you want it to be.

Cocos2d: CCSprite initWithFile in CCSprite subclass crashes

I have cocos2d project with custom CCSprite subclass:
MyCustomSprite.h:
#import "cocos2d.h"
#interface MyCustomSprite : CCSprite
#end
MyCustomSprite.m:
#import "MyCustomSprite.h"
#implementation MyCustomSprite
- (id)init
{
self = [super initWithFile:#"index.png"];
return self;
}
#end
For some strange reason, this code will crash with "EXC_BAD_ACCESS".
But in spite of this, if i init super as ususal and then write code from CCSprite's initWithFile and initWithTexture, it will work fine:
self = [super init];
if (self) {
// Code from CCSprite.m - initWithFile
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage: #"index.png"];
CGRect rect = CGRectZero;
rect.size = texture.contentSize;
// Code from CCSprite.m - iniWithTexture
[self setTexture:texture];
[self setTextureRect:rect];
return self;
}
What's the reason that the first example crashes, and second not and what's the difference between them?
Thanks for your answers!
Ok, the reason is bad CCSprite design. If we look to CCSprite.h, we can find:
-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect
{
NSAssert(texture!=nil, #"Invalid texture for sprite");
// IMPORTANT: [self init] and not [super init];
if( (self = [self init]) )
}
And thats the reason. Instead of [super init] this method calls [self init], and creates recursion ([self init]-[super InitWithFile:]-[self initWithTexture]-[self init]-...).
.
So, the simplest way to solve this problem - just re-name your init method to something else (for example "initialize") and call it instead of init: [[MyCustomSprite
alloc] initialize].
Another approach that may work is just replace your code by this one:
- (id)init
{
self = [super init];
if (self != nil)
{
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:#"my_image_name.png"];
CGRect rect = CGRectZero;
if (texture != nil)
{
rect.size = texture.contentSize;
}
[self setDisplayFrame:[CCSpriteFrame frameWithTexture:texture rect:rect]];
}
return self;
}
And a simpler way, if you are using CCSpriteFrameCache, would be just setting the display frame by
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"my_image_name.png"]
Hope to help somebody!