g989_1314125 2012-11-08 03:03 采纳率: 80%
浏览 2645
已采纳

在两个viewcontroller之间的delegation有问题

第一个viewcontroller是排行榜菜单,里面有一个按钮指示到第二个viewcontroller中,第二个是小游戏,如果玩家输了,最高分变动,就更新排行榜。在玩家完成游戏时建了带两个按钮的UIAlertView,一个是主菜单,另一个是重新开始,我的代码如下:我想通过delegation更新排行榜

  @protocol highScoreProtocol <NSObject>

        -(void)updateHighScore:(int) score;


        @end

        @interface ViewController : UIViewController <UIAlertViewDelegate> 
        @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]; 

        UIAlertView *losingScreen=[[UIAlertView alloc]initWithTitle:@"Game Over" message:[NSString stringWithFormat:@"Your Score Is %d", self.score] delegate:self cancelButtonTitle:@"Main Menu" otherButtonTitles:@"Restart", nil]; 
            }

        -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
              if (buttonIndex==0) {


            } else if (buttonIndex==1){

            }
        }

    @end

            @interface MenuVC : UIViewController <highScoreProtocol>
            @property (weak, nonatomic) IBOutlet UILabel *labelHighScore;
    @end


@implementation MenuVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    ViewController *vc=[[ViewController alloc]init];
    vc.delegateHighScore=self;

}

-(void)updateHighScore:(int)score{
    if (score>[self.labelHighScore.text integerValue]) { 
        self.labelHighScore.text=[NSString stringWithFormat:@"%d", score];
    }
    NSLog(@"does this method even run");


}

不知道哪出错了,希望大家帮忙。

  • 写回答

1条回答 默认 最新

  • Chengzi_963 2012-11-08 05:12
    关注

    这行代码不对

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

    定义了一个新的不相干的viewcontroller。

    如果你的情况是这样:定义一个viewcontroller的identifier,选择viewcontroller,……

    然后用这行代码:

    ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];
    vc.delegateHighScore = self;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧