2012-07-24 34 views
0

我做了一個單一視圖項目有NewClass.h/.M ViewController.h/.M:Objective-C的傳球數據

NewClass.h:

#import <Foundation/Foundation.h> 

@interface NewClass : NSObject 
-(void)String2; 
@end 

NewClass.m

#import "NewClass.h" 
@implementation NewClass 
-(void)String2 
{ 
NSLog(@"it works"); 
} 
@end 

ViewController.h:

enter code here 

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

@end 

ViewController.m:

enter code here 

#import "ViewController.h" 
#import "NewClass.h" 

@interface ViewController() 
@property (strong) NewClass *obj; 
@end 

@implementation ViewController 
@synthesize obj = _obj; 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
[self.obj String2]; 
} 

- (void)viewDidUnload 
{ 
[super viewDidUnload]; 

} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} else { 
    return YES; 
} 
} 
@end 

的問題是,程序不能正常工作。我沒有看到「它的作品」。 ViewController看到一個obj的方法,但NewClass不傳遞任何東西。請任何人都可以幫助我??????

+0

請注意'String2'應該只是'string2';方法以小寫字母開頭。 – bbum 2012-08-08 14:56:06

回答

3

是的,因爲缺少某些東西。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    _obj = [[NewClass alloc] init]; // create an instance of your class 
    NSLog (@"my class is at : %p", _obj); // just for your sake, we are checking the pointer of the instance 

    [self.obj String2]; 
} 

...!


,如果你不使用ARC不要忘記releaseself.obj你完成它之後。

+0

非常感謝,它的工作原理))))) – user1549057 2012-07-25 15:01:59

+0

歡迎您。 :) – holex 2012-07-25 15:46:15

+0

因爲這不是'init'方法,所以應該使用'self.obj = [[[NewClass alloc] init] autorelease];'所以任何鍵值觀察者都會收到狀態變化的通知。 – bbum 2012-08-08 14:52:24

0

您不創建對象。嘗試:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.obj = [[NewClass alloc] init]; 
    [self.obj String2]; 
} 
+0

謝謝!但我認爲我是通過「@property(strong)NewClass * obj」來完成的。我知道了,我也必須分配一個內存)))) – user1549057 2012-07-26 16:54:46

0

我已上載的實例,其中:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 

    if (!self.context) { 
     NSLog(@"Failed to create ES context"); 
    } 

    GLKView *view = (GLKView *)self.view; 
    view.context = self.context; 
    [EAGLContext setCurrentContext:self.context]; 

    self.effect = [[GLKBaseEffect alloc] init]; 

    GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(0, 480, 0, 320, -1024, 1024); 
    self.effect.transform.projectionMatrix = projectionMatrix; 

    self.player = [[SGGSprite alloc] initWithFile:@"Player.png" effect:self.effect];  
    self.player.position = GLKVector2Make(self.player.contentSize.width/2, 160); 
    [self.player String2]; 
    self.children = [NSMutableArray array]; 
    [self.children addObject:self.player];  

} 

和不存在這樣的陳述 「self.player = [[ALLOC的NewClass] INIT]」 它可以被配置?