简体   繁体   中英

Objective-C passing data

I've done a single-view project that has 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

The question is that program doesn't work. I don't see "it works". ViewController sees a method of obj, but NewClass doesn't pass anything. Please can anybody help me??????

yes, because something is missing.

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

and voilá ...!


don't forget to release the self.obj after you've finished with it, if you are not using ARC .

You don't create the object. Try this:

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

I have uploaded an example where:

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

}

and there is no such statement "self.player = [[NewClass alloc] init]" It can be configured?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM