私は2つのビューコントローラーを持っています。私の最初は私のハイスコアと私のゲームである私の2番目のビューコントローラーへのモーダルセグエを実行するボタンを含む私のメニューです。プレーヤーがハイスコアを破った場合にゲームに負けるたびに、メニューで更新してほしい。
現在、プレーヤーがゲームに負けた場合、2つのボタンを備えたUIAlertViewを作成します。最初のボタンはメインメニューで、2番目のボタンは再起動です。これは、委任を介してハイスコアを更新しようとした単純化されたコードです。
@protocol highScoreProtocol <NSObject>
-(void)updateHighScore:(int) score;
@end
@interface ViewController : UIViewController <UIAlertViewDelegate> //i have this delegate implemented because i have a uiialertview
@property (nonatomic) int score;
@property (nonatomic, weak) id <highScoreProtocol> delegateHighScore;
@implementation ViewController
@synthesize score=_score;
@synthesize delegateHighScore=_delegateHighScore;
-(void)lostGame{
[self.delegateHighScore updateHighScore:self.score]; //this is where i try to call the method that should update my high score if necessary but this doesn't actually work
UIAlertView *losingScreen=[[UIAlertView alloc]initWithTitle:@"Game Over" message:[NSString stringWithFormat:@"Your Score Is %d", self.score] delegate:self cancelButtonTitle:@"Main Menu" otherButtonTitles:@"Restart", nil]; //once the user loses the game i have an alert view show giving the option to either restart the game or go to the main menu where the high score is
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
//here i'm segueing back to my main menu because he would have pressed the 'main menu' button [self performSegueWithIdentifier:@"MainMenu" sender:self];
} else if (buttonIndex==1){
//here i just reset my attributes and reset my level because he would have pressed the 'restart button'
}
}
@end
@interface MenuVC : UIViewController <highScoreProtocol>
@property (weak, nonatomic) IBOutlet UILabel *labelHighScore; //the labelhighscore is the highscore number
@end
@implementation MenuVC
- (void)viewDidLoad
{
[super viewDidLoad];
ViewController *vc=[[ViewController alloc]init];
vc.delegateHighScore=self;//here is set the delegate as myself which i think i'm supposed to do for some reason
}
-(void)updateHighScore:(int)score{
if (score>[self.labelHighScore.text integerValue]) {
self.labelHighScore.text=[NSString stringWithFormat:@"%d", score];
}
NSLog(@"does this method even run");
// this is the method that updates the highscore which I want to run
// but it doesn't, notice I even made an 'nslog' to see if the method
// even runs but I never ever even got a log out in the debugger,
// so this method never runs.
}
少しだけ助けが必要な場合、またはすべてを完全に間違って行っていて、このタスクを間違った方法で行っている場合は、言ってください。