【发布时间】:2012-11-08 00:58:52
【问题描述】:
我有两个视图控制器。我的第一个是我的菜单,其中包含我的高分和一个按钮,它对我的第二个视图控制器执行模态搜索,这是我的游戏。每当我的玩家击败他的高分而输掉游戏时,我希望它在菜单上更新。
现在,当我的玩家输掉游戏时,我创建了一个带有 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.
}
如果我只是需要一点帮助,或者如果我做的每件事都完全错误并且以错误的方式执行此任务,请说。
【问题讨论】:
-
vc 为零吗?我不知道该 init 方法是否正确创建了您的视图控制器。
-
你如何从 MenuVC 呈现 ViewController,你能显示那个代码吗?
-
我只有一个按钮,可以模态地连接到它
标签: iphone ios xcode delegates