这是我在一个按钮内编码的内容UITableViewCell.
var shareAlert = UIAlertController(title: "Post Alert",
message: "Your Post has been Shared with your Friends",
preferredStyle: UIAlertControllerStyle.Alert)
var Ok = UIAlertAction(title: "Ok",
style: UIAlertActionStyle.Default,
handler: nil)
shareAlert.addAction(Ok)
shareAlert.presentViewController(shareAlert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
当用户单击按钮时,我希望它显示此警报,但是一旦我单击按钮,应用程序就会崩溃.我做错了吗?如何从按钮中显示警报视图UITableViewCell?
我有一个UI测试,涉及解雇系统生成UIAlertController.此警报要求用户访问设备日历的权限.测试的目标是点击OK按钮后的行为:
1 let app = XCUIApplication()
...
// this code was basically generated by the recording feature of XCode 7
2 app.alerts.elementBoundByIndex(0).collectionViews.buttons["OK"].tap()
Run Code Online (Sandbox Code Playgroud)
现在,OK第2行不是点击按钮,而是让模拟器点击第一个按钮,该Cancel按钮恰好是按钮......
此外,我发现测试框架无法准确识别出现的警报.因此,如果我检查当前的警报计数,我总是得到0:
// ...tap...
let count = app.alerts.count // == 0
Run Code Online (Sandbox Code Playgroud)
如果我使用NSPredicate条件并等待几秒钟,也会发生这种情况.
UI测试是否可能无法与系统生成的警报一起可靠地工作?我正在使用XCode 7.0.1.
在swift中显示UIAlertView的简单方法是:
let alert = UIAlertView()
alert.title = "Alert!"
alert.message = "A wise message"
alert.addButtonWithTitle("Ok, thank you")
alert.show()
Run Code Online (Sandbox Code Playgroud)
但现在这在iOS 9中已经过折旧,建议使用UIAlertController:
let myAlert: UIAlertController = UIAlertController(title: "Alert!", message: "Oh! Fancy", preferredStyle: .Alert)
myAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(myAlert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
哪个好,但我在使用SpriteKit并在GameScene中,这给出了一个错误Value of type 'GameScene' has no member 'presentViewController'...
我是否需要切换回我的ViewController来呈现这个或者有没有办法从GameScene中调用它.
我找到了这个答案,但它是Objective-C.
之前已经问过这个问题:在Swift的iOS警报中从TextField获取输入值.但是,有人拥有Objective-C语言的代码吗?
提前致谢!
到目前为止我得到了什么:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Adding A Row"
message:@"Enter A Number"
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"";
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//Do some action here
}];
[alert addAction:cancel];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud) 我想为我的项目创建一个可重用的UIAlertController类.我尝试了以下代码.但我的警报没有显示.我的代码或任何代码帮助有什么问题.这是我的警报类
class AlertView: NSObject {
class func showAlert(view: UIViewController , message: String){
let alert = UIAlertController(title: "Warning", message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
view.presentViewController(alert, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
我在另一个视图控制器中调用它
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
AlertView.showAlert(self, message: "Test alert")
// Do any additional setup after loading the view, typically from a nib.
}
}
Run Code Online (Sandbox Code Playgroud) 是否有可能做到这一点 ??我有一个出现在viewcontroller中的UIAlertController.我希望实现的是当警报出现时模糊/覆盖背景,一旦警报消失,背景应该是可见的
import UIKit import LocalAuthentication类TabBarViewController:UITabBarController {
@IBOutlet weak var noteTabBar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
self.authenticateUser()
self.tabBar.hidden = false
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(false, forKey: "sendModeToggle")
userDefaults.setObject("Avenir-Medium", forKey: "font")
userDefaults.setObject(13, forKey:"fontSize")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation …Run Code Online (Sandbox Code Playgroud) 我有一些UITableView来自内部的数据array.我想UIAlertController点击显示,但我遇到了非常奇怪的延迟.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("tapped \(dispatcher.conversations[indexPath.row].name)") //this one works fine, no problems here
let message = dispatcher.conversations[indexPath.row].desc + "\n\nDo you wanna play this?"
let alertname = dispatcher.conversations[indexPath.row].name
let alert = UIAlertController(title: alertname, message: message, preferredStyle: .alert)
let actionOK = UIAlertAction(title: "Play", style: UIAlertActionStyle.default, handler: { (action) in
//play the file
})
let actionCancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { (action) in
//cancel the file
})
alert.addAction(actionOK)
alert.addAction(actionCancel)
self.present(alert, …Run Code Online (Sandbox Code Playgroud) 我想添加图像/图标来UIAlertController喜欢苹果音乐播放器中的对话框
我想要的是彩色/大尺寸图像/图标,如下面的图像,而不是这个问题中的一个。我相信它的ios 11+功能,但是我找不到它的文档。
完全像这样:
我可以这样做UIAlertController还是应该创建自己的自定义对话框uiviewcontroller ?
ios ×7
swift ×7
uitableview ×2
ios9 ×1
iphone ×1
objective-c ×1
sprite-kit ×1
ui-testing ×1
uialertview ×1
xcode ×1
xcode7 ×1