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

发布于 2024-10-31 02:38:14 字数 1029 浏览 6 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裂开嘴轻声笑有多痛 2024-11-07 02:38:14

如果您问我,您的高分数据应该位于每个人都可以访问的模型类中,例如单例。

当您的玩家获胜时,您的插槽 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.

会发光的星星闪亮亮i 2024-11-07 02:38:14

在您的评论中,您询问如何将字符串从一个类传递到另一个类,答案如下 -

如果您想将字符串值从 B 类传递到 A 类,则在 A 类的 .h 文件中创建一个 NSMutableString 实例并定义它as property as -

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 的值。

希望这会对您有所帮助。另外,有关属性外观的更多信息 -

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 -

NSString *stringVar;

@property (nonatomic, retain) NSString *stringVar;

and also synthesize this property in class A's .m file for setter & getter as-

@ synthesize stringVar;

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-SW1enter code here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文