1

I have two view controllers. My first is my menu which contains my highscore and a button which performs a modal segue to my second view controller which is my game. Whenever my player loses the game if he beat his highscore I want it to update on the menu.

Right now, when my player loses the game, I create a UIAlertView with 2 buttons, the first is main menu and the second is restart. Here is my simplified code with my attempting to update my high score via delegation.

        @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.
}

If I just need a little help, or if I'm doing everything completely wrong and going about this task the wrong way, please say.

4

2 回答 2

1

由于您在 viewDidLoad 方法中创建了一个局部变量 vc,因此这与您在创建模态 segue 的按钮方法中创建的变量不同。那不是设置委托的正确位置。使用您创建(或拥有)的任何引用,将您自己设置为该按钮方法中的委托,该引用对您正在使用的 ViewController 的实例进行引用。如果您需要更多信息或代码示例,请发布该按钮方法,以便我了解您的操作方式。

编辑后:然后你应该实现 prepareForSegue:sender: 并这样做:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    [(ViewController *)[segue destinationViewController] setDelegate:self];
}
于 2012-11-08T01:23:20.810 回答
1

这不起作用,因为:

ViewController *vc=[[ViewController alloc]init];
vc.delegateHighScore=self;

实例化一个新的视图控制器,它与你正在交互的控制器完全无关。

我假设您正在使用情节提要,因此,为您的视图控制器创建一个标识符(在界面构建器上-> 选择您的视图控制器-> 身份检查器选项卡-> 在上面写着情节提要 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];
于 2012-11-08T01:06:57.007 回答