0

我已经完成了一个具有 NewClass.h/.m ViewController.h/.m 的单视图项目:

新类.h:

#import <Foundation/Foundation.h>

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

新类.m

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

视图控制器.h:

enter code here

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

视图控制器.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 没有传递任何东西。请问有人能帮帮我吗??????

4

3 回答 3

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];
}

……


如果您不release使用.self.objARC

于 2012-07-24T15:17:46.137 回答
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 = [[NewClass alloc] init]”这样的语句可以配置吗?

于 2012-07-29T09:08:12.230 回答
0

您不创建对象。尝试这个:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.obj = [[NewClass alloc] init];
    [self.obj String2];
}
于 2012-07-24T15:18:47.630 回答