在计算之后,我希望显示一个弹出框或警告框,向用户传递消息。有人知道我在哪里可以找到更多的信息吗?
当前回答
自iOS 8发布以来,UIAlertView现在已弃用;UIAlertController是替换的。
下面是它在Swift 5中的示例:
let alert = UIAlertController(title: "Hello!", message: "Message", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK!", style: .default) { (sender: UIAlertAction) -> Void in
// ... Maybe handle "OK!" being tapped.
}
alert.addAction(alertAction)
// Show.
present(alert, animated: true) { () -> Void in
// ... Maybe do something once showing is complete.
}
如您所见,API允许我们实现对动作和呈现警报时的回调,这非常方便!
对于较旧的Swift版本:
let alert = UIAlertController(title: "Hello!", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
let alertAction = UIAlertAction(title: "OK!", style: UIAlertActionStyle.default)
{
(UIAlertAction) -> Void in
}
alert.addAction(alertAction)
present(alert, animated: true)
{
() -> Void in
}
其他回答
对于Swift 3和Swift 4:
由于UIAlertView已弃用,所以在Swift 3上有一个显示Alert的好方法
let alertController = UIAlertController(title: NSLocalizedString("No network connection",comment:""), message: NSLocalizedString("connected to the internet to use this app.",comment:""), preferredStyle: .alert)
let defaultAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default, handler: { (pAlert) in
//Do whatever you want here
})
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
弃用:
这是受选中响应启发的swift版本:
显示AlertView:
let alert = UIAlertView(title: "No network connection",
message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok")
alert.delegate = self
alert.show()
将委托添加到视图控制器:
class AgendaViewController: UIViewController, UIAlertViewDelegate
当用户点击按钮,这段代码将被执行:
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
}
虽然我已经写了不同类型的弹出窗口的概述,但大多数人只需要一个警报。
如何实现弹出对话框
class ViewController: UIViewController {
@IBAction func showAlertButtonTapped(_ sender: UIButton) {
// create the alert
let alert = UIAlertController(title: "My Title", message: "This is my message.", preferredStyle: UIAlertController.Style.alert)
// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
// show the alert
self.present(alert, animated: true, completion: nil)
}
}
我更完整的答案在这里。
从iOS 8.0开始,你需要像下面这样使用UIAlertController:
-(void)alertMessage:(NSString*)message
{
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:@"Alert"
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction
actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
self在我的例子中是一个UIViewController,它为弹出窗口实现了presentViewController方法。
自iOS 8发布以来,UIAlertView现在已弃用;UIAlertController是替换的。
下面是它在Swift 5中的示例:
let alert = UIAlertController(title: "Hello!", message: "Message", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK!", style: .default) { (sender: UIAlertAction) -> Void in
// ... Maybe handle "OK!" being tapped.
}
alert.addAction(alertAction)
// Show.
present(alert, animated: true) { () -> Void in
// ... Maybe do something once showing is complete.
}
如您所见,API允许我们实现对动作和呈现警报时的回调,这非常方便!
对于较旧的Swift版本:
let alert = UIAlertController(title: "Hello!", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
let alertAction = UIAlertAction(title: "OK!", style: UIAlertActionStyle.default)
{
(UIAlertAction) -> Void in
}
alert.addAction(alertAction)
present(alert, animated: true)
{
() -> Void in
}
下面是Xamarin.iOS中的c#版本
var alert = new UIAlertView("Title - Hey!", "Message - Hello iOS!", null, "Ok");
alert.Show();
推荐文章
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容
- 在代码中为UIButton设置一个图像
- NSRange从Swift Range?
- UICollectionView中的单元格间距
- 我如何在我的iOS应用程序中每n分钟得到一个后台位置更新?
- 如何使用iOS创建GUID/UUID
- 禁用所呈现视图控制器的交互式撤销