【问题标题】:WatchKit and UIAlertView / UIAlertController popupWatchKit 和 UIAlertView / UIAlertController 弹出窗口
【发布时间】:2015-05-20 05:34:06
【问题描述】:

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

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

【问题讨论】:

    标签: ios watchkit


    【解决方案1】:

    (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];
    

    斯威夫特

    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的操作数组@
    【解决方案2】:

    我将在使用时添加对我有用的 swift4 结果

    WKAlertAction

    watchOS 4.0

    斯威夫特 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 中的代码

      【讨论】:

        【解决方案4】:

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

        the official documentation here

        【讨论】:

          【解决方案5】:

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

          【讨论】:

          • 不确定这是可能的,因为你不能重叠元素,所以你必须隐藏主 UI,而且它会非常 hacky(突然/没有动画等)。相反,如果可能的话,显示一个模态控制器可能是一个更好的选择。
          【解决方案6】:

          很遗憾,您不能这样做。但是,如果这是第一次启动应用程序,您当然可以拥有一个基于模式页面的层次结构,其中包含应用程序如何工作的屏幕截图。我正在我的应用程序中这样做! :)

          【讨论】:

          • 你能在故事板中添加代码和屏幕截图吗? watchkit 中只有一个主视图控制器,您如何将用户重定向到其他地方,然后不再显示该页面?我不明白。
          【解决方案7】:

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

          【讨论】:

          • 可悲的是,苹果让我们的生活变得更加复杂。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-10
          • 1970-01-01
          • 2013-05-21
          • 2011-09-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多