为什么在执行方法didMoveToView时自身的地址会发生变化?

时间:2016-04-24 23:40:50

标签: objective-c

我有一个让我困惑的Mac OSX Objective C项目。 Xcode版本7.3,部署目标10.11。使用IB构建的项目只是显示动画正弦波,而是作为精灵。我想控制波的速度,并且为了简单起见,只需使用无线电矩阵选择慢,中,快。有四个源代码文件:

//AppDelegate.h
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (weak) IBOutlet NSMatrix *radioRef;
@end

//AppDelegate.m
#import "AppDelegate.h"
#import "SineWave.h"

@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@property (weak) IBOutlet SKView *skView;
@end

@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    [self.radioRef selectCellWithTag:1];
    SKScene *scene;
    CGSize size     = CGSizeMake(360, 216);
    scene           = [SineWave sceneWithSize:size];
    scene.scaleMode = SKSceneScaleModeFill;
    [self.skView presentScene:scene];
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}
@end

//SineWave.h
#import <SpriteKit/SpriteKit.h>
@interface SineWave : SKScene
@property (weak) IBOutlet NSWindow *window;
@property (weak) IBOutlet SKView *skView;
@property NSArray *walkFrames;

- (IBAction)radioAction:(id)sender;
@property (weak) IBOutlet NSMatrix *radioRef;
- (void)      didMoveToView : (SKView *) view;
- (NSArray *) animationFramesForImageNamePrefix: (NSString*) baseImageName frameCount: (NSInteger) count;
@end

//SineWave.m
#import "SineWave.h"
@implementation SineWave
- (void)didMoveToView:(SKView *)view {
    CGFloat ThisSpeed = (CGFloat) self.radioRef.selectedCell.tag;
    if (ThisSpeed < 1.0)
        ThisSpeed = 1.0;

    self.walkFrames = [self animationFramesForImageNamePrefix:@"SinWave" frameCount:45];
    // Create the sprite with the initial frame.
    SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithTexture:[self.walkFrames objectAtIndex:0]];
    sprite.position      = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    [self addChild:sprite];
    SKAction *animateFramesAction = [SKAction animateWithTextures:self.walkFrames timePerFrame:0.05];
    [sprite runAction:[SKAction repeatActionForever:animateFramesAction]];
    sprite.speed = ThisSpeed;
}

- (NSArray *)animationFramesForImageNamePrefix: (NSString*) baseImageName frameCount: (NSInteger) count {
    /* Loads a series of frames from files stored in the app bundle, returning them in an array. */
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:count];
    for (int i = 1; i<= count; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%@%03d.png",baseImageName, i];
        SKTexture *t = [SKTexture textureWithImageNamed:imageName];
        [array addObject:t];
    }
    return array;
}

- (IBAction)radioAction:(id)sender {
    CGSize size     = CGSizeMake(360, 216);
    SKScene *scene  = [SineWave sceneWithSize:size];
    scene.scaleMode = SKSceneScaleModeFill;
    [self.skView presentScene:scene];
}
@end

这些是连接:     sKView     辅助功能:link和title = none     引用Outlets:skView - &gt;正弦波和代表     辅助功能参考:link和title = none

无线电矩阵     Outlets:委托,格式化程序,菜单和nextKeyView = none     已发送操作:操作 - &gt;正弦波和射电动作     辅助功能:link和title = none     引用Outlets:radioRef - &gt;正弦波和代表     收到的操作:所有元素(performClick ... takeStringValueFrom)= none     辅助功能参考:link和title = none

对象(类SineWave)     奥特莱斯:Radio Ref     skView:Sk View     窗口:窗口     引用Outlets:无     收到的行动:radioAction - &gt; Radio Ref

构建和执行精灵的代码工作,显示是在视图中滚动的一个很好的正弦波(文件SineWave.h中的skView,第6行),但只有一个速度。

无线电矩阵标签Slow = 1,Medium = 3,Fast = 6,希望产生1.0,3.0和6.0的ThisSpeed值。令我困惑的是,radioRef的值为nil,而执行是在方法didMoveToView中。我怎么能/应该在方法didMoveToView?

中获得无线电矩阵的选定单元格

1 个答案:

答案 0 :(得分:0)

  

为什么在执行方法didMoveToView?

执行时,self的地址会发生变化

它没有,你有2个或更多不同的对象。

您在sceneWithSize:中调用的方法applicationDidFinishLaunching:会创建一个对象。如果你在IB中的IBOutlet中连接了SineWave.h,那么你的xib中也有一个SineWave对象 - 所以你有两个对象。

HTH