【问题标题】:Warning: Attempt to present * on * whose view is not in the window hierarchy with SLComposeViewController警告:尝试使用 SLComposeViewController 在其视图不在窗口层次结构中的 * 上呈现 *
【发布时间】:2014-03-21 19:56:12
【问题描述】:

当我在游戏中推送推特图片时,我想显示分享图片(SLComposeViewController 的模态显示)。
但是,当我按下 twitter 按钮时,显示屏不显示,并且日志显示为标题。
然后,我搜索了类似的问题,但我不知道该怎么办...... [ViewDidLoad] 和 [ViewDidAppear] 很重要,我知道。 下面是我的代码。

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

#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

#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

【问题讨论】:

  • 错误是当前视图控制器 (ViewController) 不可见,因此无法呈现另一个视图控制器。模态需要从可见的视图控制器中呈现。
  • 我添加了 tweetSheet.modalPresentationStyle = UIModalPresentationFormSheet; tweetSheet.modalTransitionStyle = UIModalTransitionStyleCoverVertical;但这并没有解决...

标签: ios objective-c delegates sprite-kit hierarchy


【解决方案1】:

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

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;

应该删除警告

【讨论】:

  • 非常感谢!!我终于明白了。我感激你。但是,该行中有一个“从不兼容的类型'UIViewController'分配给'id'”的提示。请问你知道原因吗?还有,请问如何增加你的声望?
  • 如果这个答案适合您,只需单击旁边的勾号接受它。此外,投票也会有所帮助。
  • 我会检查并研究它,非常感谢你教我一切!
猜你喜欢
  • 1970-01-01
  • 2020-08-07
  • 2014-11-19
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
  • 2013-02-23
  • 2017-07-18
  • 1970-01-01
相关资源
最近更新 更多