我有两个视图控制器。我的第一个是我的菜单,其中包含我的高分和一个按钮,它对我的第二个视图控制器执行模态segue,这是我的游戏。每当我的玩家失去游戏,如果他击败他的高分,我希望它在菜单上更新。
现在,当我的玩家输掉游戏时,我创建了一个带有2个按钮的UIAlertView,第一个是主菜单,第二个是重启。这是我的简化代码,我试图通过委托来更新我的高分。
@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.
}
如果我只是需要一些帮助,或者如果我做的一切都完全出错并以错误的方式完成这项任务,请说出来。
答案 0 :(得分:1)
这不起作用,因为:
ViewController *vc=[[ViewController alloc]init];
vc.delegateHighScore=self;
实例化一个新的viewcontroller,它与你正在与之交互的那个完全无关。
我假设您正在使用故事板,因此,为您的viewcontroller创建一个标识符(在界面构建器上 - >选择您的viewcontroller - &gt;身份检查器标签 - &gt;写一个名称,其中显示了Storyboard ID)
然后添加此代码而不是之前的代码:
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];
vc.delegateHighScore = self;
修改强>
将此添加到您的按钮操作(但从界面构建器中删除segue并从viewDidLoad中删除此代码)
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];
vc.delegateHighScore = self;
[self presentModalViewController:vc animated:YES];
答案 1 :(得分:1)
由于在viewDidLoad方法中创建局部变量vc,因此这与您在创建模态segue的按钮方法中创建的变量不同。这不是设置代表的正确位置。使用您创建(或拥有)您正在调整的ViewController实例的任何引用,将自己设置为该按钮方法中的委托。如果您需要更多信息或代码示例,请发布该按钮方法,以便我了解您的情况。
编辑之后:然后你应该实现prepareForSegue:sender:并执行以下操作:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
[(ViewController *)[segue destinationViewController] setDelegate:self];
}