如何在 Apple Watch 上显示警报

How to show Alert on Apple Watch

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

watchOS2

对于 watchOS2,您可以使用 WKAlertAction 方法:

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

watchOS1

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

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

2 - 将标识符设置为 "ErrorInterfaceController"

3 - 显示该错误:

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

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

请注意,您的 ErrorInterfaceController 可以有一个空的标题,并且 ok 按钮可以关闭它,或者您可以保留它的默认设置 "Done"。

这是最简单的消息显示解决方案。

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

另一种选择是将您的警报 UI 放在一个组中,并在必要时 show/hide 进行。根据您应用程序的设计,这可以很好地工作。我做了类似的事情来显示加载 UI.

对于 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 ]];

在 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
                                                             }
                                           ]];

Swift

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

你应该像蒂亚戈说的那样制作第二个界面控制器,然后从第一个呈现第二个:

Objective-C

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

Swift

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

Swift3.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])

希望对您有所帮助!!!