在两个viewcontroller之间的delegation有问题 -问答-阿里云开发者社区-阿里云

开发者社区> 爵霸> 正文

在两个viewcontroller之间的delegation有问题

2016-05-27 11:27:17 1366 1

第一个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)
  • 爵霸
    2019-07-17 19:17:30

    这行代码不对

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

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

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

    然后用这行代码:

    ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];
    vc.delegateHighScore = self;
    0 0
+关注
2
文章
9426
问答
问答排行榜
最热
最新
推荐问答
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载