将值从一个类传递到另一个类



我正在设计一个老虎机项目,该项目具有高分页面的第二视图。在大多数情况下,除了获胜者从老虎机到高分页面的传递之外,一切都在工作。下面是我在老虎机方法中的代码:

if(win == YES)  {
    NSString *msg = nil;
    if(playerField.text.length > 0) {
        msg = [[NSString alloc] initWithFormat:@"%@", playerField.text];
    }
    NSLog(@"DEBUG");
    [(HighScorePage *)self.view addNewHighScore:msg];
    [self performSelector:@selector(playWinSound) withObject:nil afterDelay:.5];
    [msg release];
}
下面是HighScorePage中的addNewHighScore方法:
-(void)addNewHighScore:(NSString *)player   {
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
int i = 0;
for (NSArray *count in dynPlayerArray) {
    [tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]];
}
[tempArray addObject:player];
[[self highScores] beginUpdates];
[[self highScores] insertRowsAtIndexPaths:(NSArray *)tempArray withRowAnimation:UITableViewRowAnimationNone];
[[self highScores] endUpdates];
[tempArray release];    

}

仍然是新的,所以让我知道你的想法!谢谢!

如果你问我,你的High Score数据应该在一个每个人都可以访问的模型类中,比如单例。

当你的玩家赢了,你的槽视图控制器应该插入新的高分在这个模型类,然后,使用键值观察或通知,高分视图应该更新自己。

在你的评论中,你问如何将字符串从一个类传递到另一个类,这里是答案-

如果你想从类B传递字符串值到类A,那么在类A的。h文件中创建一个NSMutableString实例,并将其定义为属性-

NSString *stringVar;
@property (nonatomic, retain) NSString *stringVar;
and also synthesize this property in class A's .m file for setter & getter as-
@ synthesize stringVar;

然后在类B中访问这个字符串作为类A的属性,并将你想从类B传递给类A的值赋给类A

希望这对你有帮助。有关属性的更多信息,请查看-

http://developer.apple.com/library/Mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1 enter code here

相关内容

最新更新