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 t开发者_JAVA技巧hink! Thanks!
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.
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
精彩评论