【发布时间】:2015-05-20 05:34:06
【问题描述】:
在我的 WatchKit 应用程序中,当用户第一次启动它时,我想向他们展示一个有用的消息警报,告诉他们应用程序是如何工作的,例如按钮的作用等等。
我可以在 WatchKit 应用程序中调用类似于 UIAlertView / UIAlertController 的东西吗?我找不到关于这个主题的答案,这很可能意味着这是不可能的。
【问题讨论】:
在我的 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];
斯威夫特
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])
}
【讨论】:
presentAlertControllerWithTitle的操作数组@
我将在使用时添加对我有用的 swift4 结果
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”。
【讨论】: