Calling presentViewController in main ViewControll

2019-06-03 23:35发布

I'm building "spritekit" game, i need to present another viewController than main viewcontroller. I tried to perform this with delegates and with notification center to send ask to present new view controller. Both ways correctly called this function inside main viewController(tuViewController) from "SkScene" (tuGameOver). The point is to correctly present FacebookLikeViewDemoViewController

-(void)showShareScreen{
    NSLog(@"VIEWCONTROLER RECEIVED");
    FacebookLikeViewDemoViewController *helpVC = [[FacebookLikeViewDemoViewController alloc]initWithNibName:@"FacebookLikeViewDemoViewController" bundle:nil];
    [self presentViewController: helpVC animated: YES completion:nil];
}

When this function is executed i got NSlog and after that terminating with uncaught exception of type NSException SIGABRT Error.

I also tried to modify this function to :

-(void)showShareScreen{
    NSLog(@"VIEWCONTROLER RECEIVED");

    UIViewController *vc = [[UIApplication sharedApplication] keyWindow].rootViewController;
        FacebookLikeViewDemoViewController *helpVC = [[FacebookLikeViewDemoViewController alloc]init];
    [vc presentViewController: helpVC animated: YES completion:nil];
}

And with this, i didn't get crash, and something appears but this is blank black screen.

FacebookLikeViewDemoViewController is typical viewController Class, pinned up to view Controller in storyboard. Any ideas?

2条回答
爷、活的狠高调
2楼-- · 2019-06-04 00:05

try this code :

 FacebookLikeViewDemoViewController *helpVC = [[FacebookLikeViewDemoViewController alloc]initWithNibName:@"FacebookLikeViewDemoViewController" bundle:nil];
        [self.navigationController pushViewController:helpVC animated:YES] ;
查看更多
不美不萌又怎样
3楼-- · 2019-06-04 00:26

Mc.Lover (chuckle) came up with a solution for presenting a VC in SKScene. Below is the gist of his solution. I have added it to the touchesBegan: method so you can easily test it.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    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];

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

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

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

His original Q&A is here.

查看更多
登录 后发表回答