0

我正在开发我的第一个基础 iOS 应用程序。我确定我在这里做错了什么,但我似乎无法找出问题所在。

我有一个包含一些 IBActions 的应用程序,它做两件事:

1)通过 NSLog 输出一些简单的文本(让我知道操作有效) -这工作正常(它通过 NSLog 输出我的文本)

2) 调用自定义类中的方法,该方法还应输出 NSLog 语句。-这不起作用(没有通过 NSLog 输出文本)

我还在为在哪里创建我的类的实例而苦苦挣扎,以便在我的代码中的其他地方可以访问它们。

这是代码:

//
//  ViewController.h
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Player.h"
#import "Orc.h"

@interface ViewController : UIViewController {
    // Variables that you want to access globally go here?
    Player *wizard;
    Orc *grunty;

}

-(IBAction)takePie:(id)sender;
-(IBAction)castFireball:(id)sender;
-(IBAction)attack:(id)sender;



@end

//
//  ViewController.m
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import "ViewController.h"
#import "Player.h"

@interface ViewController ()

@end

@implementation ViewController

-(IBAction)takePie:(id)sender{
    // Player attempts to take the pie
    // If Orc's health is > 0, don't let the player take the pie
    // If the Orc's health is <= zero, let the player take the pie
    NSLog(@"IBAction - player attempted to take the pie.");
    [wizard takePie];

}

-(IBAction)castFireball:(id)sender{
    NSLog(@"IBAction - player cast fireball.");
    [wizard castFireball];
}
-(IBAction)attack:(id)sender{
    NSLog(@"IBAction - player attacked.");
    [wizard attackOrc];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    Player *wizard = [[Player alloc]init];
    Orc *grunty = [[Orc alloc]init];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

//
//  Player.h
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Player : NSObject

@property int maxHealth;
@property int currentHealth;
@property int armorClass;
@property int meleeToHitModifier;


-(void)setHeathBar;
-(void)attackOrc;
-(void)castFireball;
-(void)takePie;


@end

//
//  Player.m
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import "Player.h"

@implementation Player

@synthesize maxHealth;
@synthesize currentHealth;
@synthesize armorClass;
@synthesize meleeToHitModifier;

-(void)setHealthBar {
    NSLog(@"Player health is 15");
    return;

}

-(void)attackOrc {
    // roll a d20
    // add the "to hit" modifier
    // compare the result to the orc's armor class
    NSLog(@"Player - Player attached the Orc");
    return;

}
-(void)castFireball{
    NSLog(@"Player - Player casts Fireball");
    return;

}
-(void)takePie{
    NSLog(@"Player - Player attempts to take pie");
    return;

}

@end

注意:我还定义了一个 Orc 类,但它看起来与显示的播放器类完全相同。

在此先感谢您的帮助!

——埃迪

4

1 回答 1

0

你的 ViewController.h (你应该用一些更独特的名字来称呼它,仅供参考)应该是这样的:

//
//  ViewController.h
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Player.h"
#import "Orc.h"

@interface ViewController : UIViewController

@property Player *wizard;
@property Orc *grunty;

-(IBAction)takePie:(id)sender;
-(IBAction)castFireball:(id)sender;
-(IBAction)attack:(id)sender;

@end

然后在您的 ViewController.m 中,您的 viewDidLoadMethod 应该如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.wizard = [[Player alloc]init];
    self.grunty = [[Orc alloc]init];

}

这至少应该确保您有一个可以调用方法的 Player 的实例化实例,因此NSLog应该可以访问您的实例。

于 2013-04-30T01:59:40.730 回答