16

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

4

5 回答 5

14

使用 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。

于 2015-04-16T10:48:52.453 回答
11

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

希望能帮助到你 !!!

于 2017-08-20T15:41:44.177 回答
7

对于 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 ]];
于 2016-02-06T23:57:35.207 回答
5

在 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"])
于 2016-03-15T10:31:27.883 回答
4

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

于 2015-04-16T11:55:19.003 回答