在我的WatchKit应用程序中,当用户首次启动它时,我想向他们显示一个有用的消息警报,告诉他们应用程序如何工作,例如按钮的功能等。
在WatchKit应用程序中是否有类似于UIAlertView / UIAlertController的东西可以调用?我在这个主题上找不到答案,这可能意味着这是不可能的。
(在 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];
SWIFT
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])
}
我会添加Swift4的结果,这对我在使用WKAlertAction时有效。
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])
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 编写代码
是的,在升级到watchOS 2之后,您可以使用WKInterfaceController的presentAlertController来呈现警报视图。
请参见此处的官方文档。
没有专门用于警报的类。但是,您可以使用“WKInterfaceLabel”和一个“WKInterfaceButton”中的信息以模态方式呈现“WKInterfaceController”。
很遗憾,你不能这样做。但是,如果应用程序是第一次启动,你当然可以使用基于模态页面的层次结构,并附上应用程序运行的截图。我在我的应用程序中就是这么做的! :)
presentAlertControllerWithTitle
的操作数组。 - oelna