将值从一个类传递到另一个类
我正在设计一个老虎机项目,它有高分页面的第二个视图。在大多数情况下,除了获胜者从老虎机传递到高分页面之外,一切都正常。这是我在老虎机内的方法中的代码:
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];
}
这仍然是新事物,所以让我知道您的想法!谢谢!
I am designing a slot machine project that has a second view for the high score page. For the most part, everything is working except for the passing of the winner from slot machine to the high score page. Here is my code in a method within slot machine:
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];
}
And Here is the addNewHighScore method in HighScorePage:
-(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];
}
Still new at this, so let me know what you think! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您问我,您的高分数据应该位于每个人都可以访问的模型类中,例如单例。
当您的玩家获胜时,您的插槽 ViewController 应在此模型类中插入新的高分,然后,无论使用键值观察还是通知,高分视图都应自行更新。
If you ask me, your High Score data should be in a model class everyone could access, like a singleton for example.
When your player wins, your slot ViewController should insert the new High Score in this model class and then, using whether Key-Value Observing or Notifications, the High Score view should update itself.
在您的评论中,您询问如何将字符串从一个类传递到另一个类,答案如下 -
如果您想将字符串值从 B 类传递到 A 类,则在 A 类的 .h 文件中创建一个 NSMutableString 实例并定义它as property as -
然后在类 B 中访问该字符串作为类 A 的属性,并分配您想要从类 B 传递到类 A 的值。
希望这会对您有所帮助。另外,有关属性外观的更多信息 -
http://developer.apple.com/library/Mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163 -CH17-SW1
在此处输入代码
In your comment you have asked for how to pass a string from one class to another, here is the answer -
IF you want to pass string value from class B to class A then in class A's .h file create an NSMutableString instance and define it as property as -
then in class B access this string as class A's property and assign what value you want to pass from class B to class A.
Hope this will help you. Also for more info about property look -
http://developer.apple.com/library/Mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1
enter code here