0

NewClass.h/.m ViewController.h/.m を持つ単一ビュー プロジェクトを作成しました。

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 は何も渡しません。誰か助けてください??????

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 に答える