【问题标题】:How to show Alert on Apple Watch如何在 Apple Watch 上显示警报
【发布时间】:2015-04-16 10:27:29
【问题描述】:

如何在 Apple Watch 上显示警报。是否有任何替代方法可以在 Apple Watch 中显示警报,因为我检查了 UIAlertView 在 Apple Watch 上不起作用。

【问题讨论】:

  • 更新了我的答案。有了 watchOS2,我相信你有一个更好的解决方案来满足你的需求 :)

标签: objective-c watchkit apple-watch watchos-2


【解决方案1】:

使用 watchOS2

使用 watchOS2 你可以使用WKAlertAction 方法:

+ (instancetype nonnull)actionWithTitle:(NSString * nonnull)title
                                 style:(WKAlertActionStyle)style
                               handler:(WKAlertActionHandler nonnull)handler

使用 watchOS1

如果您不介意失去 UIAlertView 看到背后内容的功能,您可以:

1 - 创建一个 ErrorInterfaceController(带或不带 ok 按钮)

2 - 将标识符设置为“ErrorInterfaceController”

3 - 显示该错误:

[self presentControllerWithName:@"ErrorInterfaceController" 
                        context:@{@"title" : @"yourTitle",
                                  @"text"  : @"yourText"}];

4 - 在您的 ErrorInterfaceController.m 中,您可以根据上下文设置标题和文本。

请注意,您的 ErrorInterfaceController 可以有一个空标题,并且 ok 按钮可以将其关闭,或者您可以使用默认的“完成”保持原样。

这是呈现消息的最简单的解决方案。

如果你想要更复杂的东西,你需要记住 WatchKit 没有 z-index 并且你不能通过代码动态添加元素。因此,您需要有一个解决方案,使用在您的应用程序扩展中呈现的 UIImages 并将它们发送到 WatchKit。

【讨论】:

  • 请在 deep.where 中解释代码在哪里?在控制器中如何传递上下文?
  • 用一些图片完成了答案。想法基本上是定义一个新的接口控制器,负责呈现错误,然后简单地呈现它。
【解决方案2】:

Swift 3.0 更新 - 在 watchOS 3.0 中

let action = WKAlertAction(title: "Decline", style: WKAlertActionStyle.default) {
        print("Ok")
    }
    presentAlert(withTitle: "Message", message: "Please select value. Swipe right to change it.", preferredStyle: WKAlertControllerStyle.alert, actions:[action])

希望对你有帮助!!!

【讨论】:

    【解决方案3】:

    对于 watchOS 2,这里是一个示例:

    WKAlertAction *action =
                [WKAlertAction actionWithTitle:@"OK"
                                         style:WKAlertActionStyleDefault
    
                                       handler:^{
                                           // do something after OK is clicked
                                       }];
    
    NSString *title = @"Oops!";
    NSString *message = @"Here comes the error message";
    
    [self.interfaceController
                presentAlertControllerWithTitle:title
                                        message:message
                                 preferredStyle:WKAlertControllerStyleAlert
                                        actions:@[ action ]];
    

    【讨论】:

      【解决方案4】:

      在 watchOS 2 中

      Objective-C

      NSString *titleOfAlert = @"Something Happened Wrong";
      NSString *messageOfAlert = @"Error Message Here";
      [self.interfaceController presentAlertControllerWithTitle: titleOfAlert
                                                        message: messageOfAlert
                                                 preferredStyle:
                                                  WKAlertControllerStyleAlert
                                                 actions:@[
                                                    [WKAlertAction actionWithTitle: @"OK"
                                                                   style: WKAlertActionStyleDefault
                                                                   handler: ^{
                                                                      //something after clicking OK
                                                                   }
                                                 ]];
      

      斯威夫特

      let titleOfAlert = "Something Happened Wrong"
      let messageOfAlert = "Error Message Here"
      self.interfaceController.presentAlertControllerWithTitle(titleOfAlert, message: messageOfAlert, preferredStyle: .Alert, actions: [WKAlertAction(title: "OK", style: .Default){
          //something after clicking OK
      }])
      

      在 watchOS 1 中

      您应该制作第二个界面控制器,正如 Tiago 所说,然后从第一个中呈现第二个:

      Objective-C

      [self presentControllerWithName:@"ErrorInterfaceController" 
                          context:@{@"title" : @"yourTitle",
                                    @"text"  : @"yourText"}];
      

      斯威夫特

      self.presentController(name: "ErrorInterfaceController", context:["title":"yourTitle" , "text":"yourText"])
      

      【讨论】:

        【解决方案5】:

        另一种选择是将警报 UI 放在一个组中,并根据需要显示/隐藏它。根据您的应用程序的设计,这可以很好地工作。我为显示加载 UI 做了类似的事情。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-08
          相关资源
          最近更新 更多