我很少提問,但是這個令我感到沮喪,因爲我無法在任何地方找到答案!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