警告:尝试使用SLComposeViewController显示视图不在窗口层次结构中的* on * [英] Warning: Attempt to present * on * whose view is not in the window hierarchy with SLComposeViewController

查看:194
本文介绍了警告:尝试使用SLComposeViewController显示视图不在窗口层次结构中的* on *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在游戏中推动twitter图像时,我想显示共享图像(SLComposeViewController的模态显示)。

但是,当我按下twitter按钮时,显示屏不显示,日志显示为标题。

然后,我搜索了类似的问题,但我不知道应该怎么做我做...
[ViewDidLoad]和[ViewDidAppear]是重要的事情,我知道。
以下是我的代码。

When I push the twitter image in the game, I want to show share image(SLComposeViewController's modal display).
But, when I push the twitter button, the display doesn't show and the log says like the title.
Then, I searched similar issues but I am not sure what should I do... [ViewDidLoad] and [ViewDidAppear] is important thing, I know. Below is my code.

ViewController.m

ViewController.m

#import "ViewController.h"
#import "NewGameScene.h"
#import <Social/Social.h>
#import "GameOverScene.h"

@interface ViewController ()
{
  GameOverScene *gameOver;
}
@end

@implementation ViewController

//Loads the view onto our main class
- (void)loadView
{
self.view  = [[SKView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
}

//Executes when view finishes loading
- (void)viewWillLayoutSubviews
 { 
 //extracted important code.
 [super viewDidLoad];
 gameOver = [[GameOverScene alloc]init];
 gameOver.delegate = self;
 }

-(void)showShareScreen
{
NSLog(@"showShareScreen");
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *tweetSheet = [SLComposeViewController                                                
    poseViewControllerForServiceType:SLServiceTypeTwitter];
    [tweetSheet setInitialText:@"TestTweet from the Game !!"];
     NSLog(@"self = %@",self);
    [self presentViewController:tweetSheet animated:YES completion:nil];

}
else {
    NSLog(@"not sls type twitter");
}
}
@end

MainScene.m



MainScene.m

#import "MainScene.h"
#import "GameOverScene.h"
#import "Player.h"
#import "Obstacle.h"
#import "ViewController.h"

static GameOverScene *gameOver;
-(void)die{

    //Create our game over scene with the current scene's dimensions
    GameOverScene *gameOver = [[GameOverScene alloc] initWithSize:self.size];
    ViewController *vc = [[ViewController alloc] init];
    gameOver.delegate = vc;
    NSLog(@"vc = %@",vc);

    [gameOver didMoveToView:self.view];

    //Present the game over scene with the fade in transition
    [self.scene.view presentScene:gameOver transition:transition];

}];
}

GameOverScene.m

GameOverScene.m

#import "GameOverScene.h"
#import "NewGameScene.h"
#import "MainScene.h"
#import <Social/Social.h>


//Screen is touched
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

//Same as in NewGameScene menu
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];

//Is the retry button touched?
if([node.name isEqualToString:@"twitterbutton"]){
    NSLog(@"self.delegate = %@",self.delegate);
    [self.delegate showShareScreen];
    if (nil == self.delegate) NSLog(@"delegate is nil"); 
}  
}
@end


推荐答案

问题在下面的评论中描述。

The problem is described in the comment below.

GameOverScene *gameOver = [[GameOverScene alloc] initWithSize:self.size];

ViewController *vc = [[ViewController alloc] init];//This is causing the problem.
//The delegate has to be the viewController which is presenting the scene

gameOver.delegate = vc;
NSLog(@"vc = %@",vc);

[gameOver didMoveToView:self.view]; //Why are you calling this line??

//Present the game over scene with the fade in transition
[self.scene.view presentScene:gameOver transition:transition];

而不是行:

gameOver.delegate = vc;

尝试使用这一行:

gameOver.delegate = self.view.window.rootViewController;

编辑:

使用行

gameOver.delegate = (ViewController*) self.view.window.rootViewController;

应该删除警告

这篇关于警告:尝试使用SLComposeViewController显示视图不在窗口层次结构中的* on *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆