gpt4 book ai didi

iphone - 为什么我在两个 vc 之间的委托(delegate)不起作用?

转载 作者:行者123 更新时间:2023-11-29 11:06:42 26 4
gpt4 key购买 nike

我有两个 View Controller 。我的第一个是我的菜单,其中包含我的高分和一个按钮,该按钮对我的第二个 View Controller (即我的游戏)执行模式转换。每当我的玩家超过他的高分而输掉游戏时,我希望它在菜单上更新。

现在,当我的玩家输掉游戏时,我创建了一个带有 2 个按钮的 UIAlertView,第一个是主菜单,第二个是重启。这是我尝试通过委托(delegate)更新我的高分的简化代码。

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

如果我只是需要一点帮助,或者如果我做的每件事都完全错误并且以错误的方式完成这项任务,请告诉我。

最佳答案

这不起作用,因为:

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

实例化一个新的 View Controller ,它与您正在与之交互的 View Controller 完全无关。

我假设您正在使用 Storyboard,因此,为您的 View Controller 创建一个标识符(在界面构建器上 -> 选择您的 View Controller -> 身份检查器选项卡 -> 在显示 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];

关于iphone - 为什么我在两个 vc 之间的委托(delegate)不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13280896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com