2014-01-13 33 views
5
呈現的viewController

我想提出上SkViewUIActivityViewController但Xcode中給了我這個錯誤:上SKScene

爲「GAMEOVER」不可見@interface聲明選擇 「presentViewController:動畫:完成:」

- (void)shareScore { 

    //add view 
    UIView *Sview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 512, 512)]; 
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"shareScoreImg.png"]]; 
    image.frame = Sview.frame; 
    [Sview addSubview:image]; 

    //add label 
    CGRect fframe = self.view.frame; 

    UILabel *score = [[UILabel alloc] initWithFrame:fframe]; 
    score.text = @"9999"; 
    score.textAlignment = NSTextAlignmentCenter; 
    score.textColor = [UIColor darkGrayColor]; 
    score.center = CGPointMake(250, 440); 
    score.font = [UIFont fontWithName:@"Pixel LCD7" size:50]; 
    [Sview addSubview:score]; 


    //capture view 
    UIGraphicsBeginImageContextWithOptions(Sview.bounds.size, Sview.opaque, 0.0); 
    [Sview.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    UIActivityViewController* activityViewController = 
    [[UIActivityViewController alloc] initWithActivityItems:@[screenshot] 
             applicationActivities:nil]; 

    [self presentViewController:activityViewController animated:YES completion:nil]; 

} 

我可以如何呈現一個預設的viewController上SKScene? 謝謝。

+0

你不能,'UIView'或任何其他視圖沒有'presentViewController:animated:completion:'方法。該方法只適用於'UIViewController' – rckoenes

+0

@rckoenes那麼我怎樣才能將遊戲的分數分享給Facebook等等? –

+0

通過ViewController或窗口。 – rckoenes

回答

20

我們可以用「presentModalViewController」通過使用此代碼訪問根視圖控制器

UIViewController *vc = self.view.window.rootViewController; 
    [vc presentViewController: activityViewController animated: YES completion:nil]; 

現在它工作正常!

+0

大聲笑,你基本上用我的答案。 – Fogmeister

+11

不是。既然你沒有放任何代碼。 Mc.Lover提供了一個代碼。我比他更喜歡他的回答。大聲笑? – GeneCode

+0

這是答案人! – Paul

6

IIRC您創建的第一個SKSceneSKView之內。

This SKView is in UIViewController

您可以使用屬性或委派或任何您喜歡的方式訪問SKSceneSKView中的UIViewController方法。甚至使用通知。

然後在UIViewController上,您可以毫無問題地呈現新的視圖控制器。

+0

@ Mc.Lover它看起來像你試圖從'SKView'呈現,你仍然無法做到這一點。你需要從'UIViewController'出現。 – Fogmeister

+0

反對的任何理由? – Fogmeister

+0

代碼在哪裏? – Paul