我正在开发我的第一个基础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类,但它看起来与显示的播放器类完全相同。
提前感谢您的帮助!
- 艾迪
答案 0 :(得分:0)
您的ViewController.h(您应该通过一些更独特的名称,FYI调用)应该如下所示:
//
// 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
。