如何从SKScene展示UIAlertController

时间:2015-07-01 04:36:14

标签: ios objective-c sprite-kit uialertcontroller uialertaction

我正在使用Spritekit,我正在尝试从我的SKScene中提供一个UIAlertController,但是我很难做到这一点。我已经看过几个教程,但UIAlertController教程都没有特定于Spritekit。我一直看到下面这段代码,但由于SKScene不是UIViewController,因此效果不佳。

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

我有以下相关代码的其余部分。任何人都可以帮我在我的SKScene上展示我的UIAlerController。

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"You Lose!" message:@"Do You Want To Beat This Level?" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *CancelButton = [UIAlertAction actionWithTitle:@"GiveUp" style:UIAlertControllerStyleAlert handler:<#^(UIAlertAction *action)handler#>]

3 个答案:

答案 0 :(得分:3)

SKScene不应该是呈现UIAlertController的人,而是一个UIViewController,例如你最初的GameViewController。从UIViewController调用时,上面的代码工作正常。

您可以使用NSNotificationCenter来帮助您调用视图控制器。

将此添加到视图控制器的viewDidLoad方法

select 30076 PersonKey, 'DC' ServiceAreaState, '*' ServiceAreaCounties
into #a
union
select 30076 PersonKey, 'VA' ServiceAreaState, 'ARLINGTON' ServiceAreaCounties
union 
select 30076 PersonKey, 'VA' ServiceAreaState, 'BEDFORD' ServiceAreaCounties
union
select 30076 PersonKey, 'VA' ServiceAreaState, 'ETC' ServiceAreaCounties
union
select 12345 PersonKey, 'PA' ServiceAreaState, 'BUCKS' ServiceAreaCounties
union
select 12345 PersonKey, 'PA' ServiceAreaState, 'CHESTER' ServiceAreaCounties
union 
select 12345 PersonKey, 'PA' ServiceAreaState, 'MONTGOMERY' ServiceAreaCounties


;with cte as
(
select PersonKey, ServiceAreaState, cast(ServiceAreaCounties as varchar(100)) ServiceAreaCounties, rown from (select PersonKey, ServiceAreaState, ServiceAreaCounties, row_number() over (partition by cast(PersonKey as varchar(10))+ ServiceAreaState  order by (select null))rown from #a)A where rown = 1
union all
select A.PersonKey, A.ServiceAreaState, cast((A.ServiceAreaCounties + ' '+ cte.ServiceAreaCounties) as varchar(100)) ServiceAreaCounties, A.rown from (select PersonKey, ServiceAreaState, ServiceAreaCounties, row_number() over (partition by cast(PersonKey as varchar(10)) + ServiceAreaState  order by (select null))rown from #a)A JOIN CTE on cte.rown + 1 = A.rown and cte.PersonKey = A.PersonKey and cte.ServiceAreaState = A.ServiceAreaState
)

select PersonKey, ServiceAreaState, ServiceAreaCounties ServiceAreaCountiesConcatenated from (
select PersonKey, ServiceAreaState, ServiceAreaCounties, rown, row_number() over (partition by cast(PersonKey as varchar(10)) + ServiceAreaState order by rown desc)newrown from cte)A where newrown = 1

你也需要定义这个方法。

[[NSNotificationCenter defaultCenter] addObserver:self                                          
                                         selector:@selector(playerLost:) 
                                             name:@"PlayerLostNotification" 
                                           object:nil];   

当玩家输掉时,在你的SKScene中,

- (void)playerLost:(NSNotification*) notification {
   UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"You Lose!" 
                                         message:@"Do You Want To Beat This Level?" 
                                  preferredStyle:UIAlertControllerStyleAlert];

   UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"GiveUp"
                         style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * action) {
                          [alert dismissViewControllerAnimated:YES completion:nil];
                       }];
   [alert addAction:cancel];
   [self presentViewController:alert animated:YES completion:nil];
}                             

答案 1 :(得分:0)

SKScene实例无法调用presentViewController(_:animated:completion),因为它不是UIViewController的子类。但是,如果您这样重写,您的警报将会启动:

self.view?.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)

ps:虽然Attempt to present <UIAlertController: 0x7fc31eb32e50> on <Sample_Game.GameViewController: 0x7fc31bd9b4f0> which is already presenting会有警告。如果有人知道如何根除这个警告,这将是伟大的。

[2016年8月11日更新]

要根除上述警告,请检查rootViewController是否已提供视图控制器:

 let vc = self.view?.window?.rootViewController
 if vc.presentedViewController == nil {
    vc.presentViewController(alert, animated: true, completion: nil)
 }

答案 2 :(得分:0)

创建场景时,只需设置指向viewController的指针即可。然后你可以像这样调用它:     [self.viewController presentViewController:alert animated:YES completion:nil];

在ViewController中:

// Create and configure the scene.
GameScene *scene = [GameScene sceneWithSize:viewSize];
SKView * skView = (SKView *)self.view;
scene.viewController = self;

// Present the scene.
[skView presentScene:scene];
相关问题