我很少提问,但是这个令我感到沮丧,因为我无法在任何地方找到答案!Objective C - 获得一个类来返回一个值
我只需要调用GameChallenges.m中的函数并将值返回给视图控制器。 GameChallenges.m将被不同的视图控制器调用,这就是为什么它是分开的。请帮忙!
我有一个名为GameChallenges的单独的类文件。
这在它的函数/法:在.H
@class StatsViewController;
@interface GameChallenges : NSObject {
StatsViewController* statsController;
NSString* challengeTitle;
}
@property (nonatomic, retain) IBOutlet StatsViewController* statsController;
@property (assign) NSString* challengeTitle;
-(NSString*)checkChallenge:(int)challegeID;
@end
在.M
#import "GameChallenges.h"
#import "StatsViewController.h"
@implementation GameChallenges
@synthesize challengeTitle,statsController;
-(NSString*)checkChallenge:(int)challegeID{
if(challegeID==1){
self.challengeTitle = @"Some Text.";
return challengeTitle;
}else if(challegeID==2){
self.challengeTitle = @"Some Other Text.";
return challengeTitle;
}
}
从名为StatsViewController我打电话这种方法的视图控制器
in .h
@class GameChallenges;
@interface StatsViewController : UIViewController {
UILabel* challengeIDDescText;
}
@property (nonatomic, retain) IBOutlet UILabel* challengeIDDescText;
@property (nonatomic, retain) IBOutlet GameChallenges* challenges;
@end
在.M
[challenges checkChallenge:tempString];
challengeIDDescText.text = challenges.challengeTitle;
问题是什么?除了您非常规方法的使用外,我没有看到任何问题。 – 2011-03-07 15:40:07
我在您的问题帖子中看不到问题。但是,您发送的checkChallenge消息可能存在问题。你传递一个名为'tempString'的东西,它是(我怀疑)一个'NSString'实例。但'checkChallenge:'预计它的参数是'int',所以你基本上把'NSString'指针指向'int'。在此之后,人们无法期待正确的行为。 – hoha 2011-03-07 16:09:41
@hoha @fluchtpunkt 如果我简化了这一点。 如果在我的GameChallenges.h我有: ' @class StatsViewController; @interface GameChallenges:NSObject { \t \t StatsViewController * statsController; \t int count; } - (int)initCount; @property(assign)int count;' 而且在我的。m: ' - (int)initCount { \t count = 1; 返回计数; }' 然后在StatsViewController.m '的NSLog(@ “算:%I”,[挑战initCount]);' 它要么返回0零/零等它只是不能读取已经返回了什么。 我的问题是谁能告诉我如何成功返回一个值。谢谢 – 2011-03-07 16:24:33