如何在Apple Watch上显示警报

Iqb*_*han 14 objective-c apple-watch watchkit watchos-2

如何在Apple手表上显示Alert.是否有任何替代方案可以在Apple Watch中显示警报,因为我检查过并且UIAlertView无法在Apple Watch上运行.

Tia*_*ida 14

使用watchOS2

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

+ (instancetype nonnull)actionWithTitle:(NSString * nonnull)title
                                 style:(WKAlertActionStyle)style
                               handler:(WKAlertActionHandler nonnull)handler
Run Code Online (Sandbox Code Playgroud)

使用watchOS1

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

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

在此输入图像描述

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

在此输入图像描述

3 - 出现以下错误:

[self presentControllerWithName:@"ErrorInterfaceController" 
                        context:@{@"title" : @"yourTitle",
                                  @"text"  : @"yourText"}];
Run Code Online (Sandbox Code Playgroud)

4 - 在ErrorInterfaceController.m中,您可以使用上下文设置标题和文本.

请注意,您的ErrorInterfaceController可以具有空标题,并且ok按钮可以将其解除,或者您可以使用默认的"完成"保留它的方式.

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

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


vom*_*ako 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 ]];
Run Code Online (Sandbox Code Playgroud)


Sey*_*aei 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
                                                             }
                                           ]];
Run Code Online (Sandbox Code Playgroud)

迅速

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
}])
Run Code Online (Sandbox Code Playgroud)

在watchOS 1中

你应该制作第二个接口控制器,正如蒂亚戈所说的那样,然后从第一个出现第二个接口控制器:

Objective-C的

[self presentControllerWithName:@"ErrorInterfaceController" 
                    context:@{@"title" : @"yourTitle",
                              @"text"  : @"yourText"}];
Run Code Online (Sandbox Code Playgroud)

迅速

self.presentController(name: "ErrorInterfaceController", context:["title":"yourTitle" , "text":"yourText"])
Run Code Online (Sandbox Code Playgroud)


Har*_*ngh 5

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])
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你 !!!