WatchKit和UIAlertView / UIAlertController弹出窗口

19

在我的WatchKit应用程序中,当用户首次启动它时,我想向他们显示一个有用的消息警报,告诉他们应用程序如何工作,例如按钮的功能等。

在WatchKit应用程序中是否有类似于UIAlertView / UIAlertController的东西可以调用?我在这个主题上找不到答案,这可能意味着这是不可能的。

7个回答

58

(在 watchOS 2.0 中新增)

 WKAlertAction *act = [WKAlertAction actionWithTitle:@"OK" style:WKAlertActionStyleCancel handler:^(void){
        NSLog(@"ALERT YES ");
    }];

 NSArray *testing = @[act];

[self presentAlertControllerWithTitle:@"Voila" message:@"This is Watch OS 2 !" preferredStyle:WKAlertControllerStyleAlert actions:testing];

SWIFT

func showPopup(){

    let h0 = { print("ok")}

    let action1 = WKAlertAction(title: "Approve", style: .default, handler:h0)
    let action2 = WKAlertAction(title: "Decline", style: .destructive) {}
    let action3 = WKAlertAction(title: "Cancel", style: .cancel) {}

    presentAlert(withTitle: "Voila", message: "", preferredStyle: .actionSheet, actions: [action1,action2,action3])

}

对我来说完美运作,即使只有一个按钮。只需记得调整提供给presentAlertControllerWithTitle的操作数组。 - oelna

12

我会添加Swift4的结果,这对我在使用WKAlertAction时有效。

WKAlertAction

watchOS 4.0

Swift 4

        let action1 = WKAlertAction.init(title: "Cancel", style:.cancel) {
            print("cancel action")
        }
        
        let action2 = WKAlertAction.init(title: "default", style:.default) {
            print("default action")
        }
        
        let action3 = WKAlertAction.init(title: "destructive", style:.destructive) {
            print("destructive action")
        }
        
        presentAlert(withTitle: "Alert Title", message: "message is here", preferredStyle:.actionSheet, actions: [action1,action2,action3])

3
   let h0 = { print("h0 action")}
   let h1 = { print("h1 action")}

   let action1 = WKAlertAction(title: "h0 action", style: .default, handler:h0)
   let action2 = WKAlertAction(title: "h1 action", style: .default, handler:h0)

   self.presentAlert(withTitle: "Title", message: "a message", preferredStyle: .actionSheet, actions: [action1, action2])

使用 Swift 3 编写代码


3

是的,在升级到watchOS 2之后,您可以使用WKInterfaceController的presentAlertController来呈现警报视图。

请参见此处的官方文档


2

没有专门用于警报的类。但是,您可以使用“WKInterfaceLabel”和一个“WKInterfaceButton”中的信息以模态方式呈现“WKInterfaceController”。


2
很遗憾,苹果刚刚让我们的生活变得更加复杂了。 - Sam B

2

很遗憾,你不能这样做。但是,如果应用程序是第一次启动,你当然可以使用基于模态页面的层次结构,并附上应用程序运行的截图。我在我的应用程序中就是这么做的! :)


你能否添加代码和屏幕截图,展示如何在Storyboard中以模态方式实现这个功能?WatchKit中只有一个主视图控制器,那么如何将用户重定向到其他地方,然后再也不显示该页面呢?我不理解。 - Sam B

1
如果我可以再提一个建议:在您的初始界面控制器中创建一个单独的组来处理“警报”,并根据需要显示/隐藏它。

不确定是否可能,因为您无法重叠元素,所以您必须隐藏主要的 UI,这将会很突兀 / 没有动画效果等,非常 hacky。相反,如果可能的话,显示一个 modal controller 可能是更好的选择。 - strangetimes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接