之前关于UIAlertController,可以在viewWillAppear中去presentViewController,并在最后去消失了:
[已解决]swift中给已有的ViewController中添加UIAlertAction并直接显示和正常消失返回被调用者视图
但是,后来单独抽出来对应的vc:
ImagePickerViewController
后,还是类似的代码:
class ImagePickerViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) let selectPictureAlertController = UIAlertController(title: “提示”, message: “”, preferredStyle: UIAlertControllerStyle.ActionSheet) let cancelAlertAction = UIAlertAction(title: “取消”, style: .Cancel, handler: self.cancelImagePick(_:)) selectPictureAlertController.addAction(cancelAlertAction) let localAlbumAlertAction = UIAlertAction(title: “打开本地相册”, style: UIAlertActionStyle.Default, handler:self.openLocalAlbum(_:)) selectPictureAlertController.addAction(localAlbumAlertAction) let openCameraAlertAction = UIAlertAction(title: “打开相机”, style: UIAlertActionStyle.Default, handler: self.openCamera(_:)) selectPictureAlertController.addAction(openCameraAlertAction) selectPictureAlertController.modalPresentationStyle = .Custom self.presentViewController(selectPictureAlertController, animated: true, completion: nil) } |
然后最后去让此UIAlertController去消失:
结果试了各种代码:
func dissmissCurrentVC() { gLog.verbose(“”) gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x7be90330>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7becccf0>), self.presentedViewController=Optional(<UIImagePickerController: 0x7c168c00>), self.presentingViewController=Optional(<UINavigationController: 0x7a9a1800>) self.dismissViewControllerAnimated(false, completion: { gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x7be90330>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7becccf0>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7a9a1800>) // if self.pickImageCompletionHandler != nil { // self.pickImageCompletionHandler!(pickedImage: self.pickedImage) // } if self.presentedViewController != nil { self.dismissViewControllerAnimated(false, completion: nil) } //self.selectPictureAlertController.dismissViewControllerAnimated(false, completion: nil) // if self.parentVC != nil { // gLog.verbose(“self.parentVC.presentedViewController=\(self.parentVC!.presentedViewController)”) // //self.parentVC.presentedViewController=Optional(<Sales_App.ImagePickerViewController: 0x12faa2370>) // // if self.parentVC!.presentedViewController != nil { // self.parentVC!.dismissViewControllerAnimated(false, completion: nil) // } // } }) } |
都没用。
去viewWillAppear:
gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x7df92970>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7deb2e20>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7c27c400>) self.presentViewController(selectPictureAlertController, animated: true, completion: nil) gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x7df92970>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7deb2e20>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7c27c400>) |
和之后的代码调试发现:
func openLocalAlbum(alerAction:UIAlertAction){ gLog.verbose(“alerAction=\(alerAction)”) gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x7df92970>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7deb2e20>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7c27c400>) let imagePicker:UIImagePickerController = UIImagePickerController() gLog.verbose(“imagePicker=\(imagePicker)”) imagePicker.delegate = self self.presentViewController(imagePicker, animated: true, completion: nil) } |
之前的UIAlertController,按照到底应该是当前的:
self.presentedViewController
但是实际上此处却是:
self.presentedViewController=nil
inside viewcontroller show UIAlertController and dismiss
ios – show UIAlertController outside of ViewController – Stack Overflow
ios – How to dismiss an UIAlertController and the keyboard simultaneously? – Stack Overflow
改为:
dispatchMain_async({ self.presentViewController(selectPictureAlertController, animated: true, completion: nil) gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x7df92970>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7deb2e20>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7c27c400>) }) |
试试,结果问题依旧。
viewcontroller show UIAlertController
ios – What is the best way to check if a UIAlertController is already presenting? – Stack Overflow
去移动到viewDidAppear试试
(注:之前就没去打算放在viewDidAppear,因为:想要这个UIAlertController尽快的显示,所以放在viewWillAppear比在viewDidAppear更快能显示)
override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let selectPictureAlertController = UIAlertController(title: “提示”, message: “”, preferredStyle: UIAlertControllerStyle.ActionSheet) let cancelAlertAction = UIAlertAction(title: “取消”, style: .Cancel, handler: self.cancelImagePick(_:)) selectPictureAlertController.addAction(cancelAlertAction) let localAlbumAlertAction = UIAlertAction(title: “打开本地相册”, style: UIAlertActionStyle.Default, handler:self.openLocalAlbum(_:)) selectPictureAlertController.addAction(localAlbumAlertAction) let openCameraAlertAction = UIAlertAction(title: “打开相机”, style: UIAlertActionStyle.Default, handler: self.openCamera(_:)) selectPictureAlertController.addAction(openCameraAlertAction) selectPictureAlertController.modalPresentationStyle = .Custom gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x797567f0>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x79759d10>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7b31d000>) self.presentViewController(selectPictureAlertController, animated: true, completion: nil) gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x797567f0>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x79759d10>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7b31d000>) } |
问题依旧:
self.presentedViewController还是nil
但是,最后去用两次的dismissViewControllerAnimated去消失掉UIAlertController,则是可以的:
func dissmissCurrentVC() { gLog.verbose(“”) gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x7be90330>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7becccf0>), self.presentedViewController=Optional(<UIImagePickerController: 0x7c168c00>), self.presentingViewController=Optional(<UINavigationController: 0x7a9a1800>) //self=<Sales_App.ImagePickerViewController: 0x797567f0>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x79759d10>), self.presentedViewController=Optional(<UIImagePickerController: 0x7a454e00>), self.presentingViewController=Optional(<UINavigationController: 0x7b31d000>) self.dismissViewControllerAnimated(false, completion: { gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x7be90330>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7becccf0>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7a9a1800>) //self=<Sales_App.ImagePickerViewController: 0x797567f0>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x79759d10>), self.presentedViewController=Optional(<UIAlertController: 0x7ab89c00>), self.presentingViewController=Optional(<UINavigationController: 0x7b31d000>) // if self.pickImageCompletionHandler != nil { // self.pickImageCompletionHandler!(pickedImage: self.pickedImage) // } if self.presentedViewController != nil { self.dismissViewControllerAnimated(false, completion: nil) } //self.selectPictureAlertController.dismissViewControllerAnimated(false, completion: nil) // if self.parentVC != nil { // gLog.verbose(“self.parentVC.presentedViewController=\(self.parentVC!.presentedViewController)”) // //self.parentVC.presentedViewController=Optional(<Sales_App.ImagePickerViewController: 0x12faa2370>) // // if self.parentVC!.presentedViewController != nil { // self.parentVC!.dismissViewControllerAnimated(false, completion: nil) // } // } }) } |
期间的顺序是:
1.先是:self.dismissViewControllerAnimated
2.然后是:
(此时:self.presentedViewController=nil)
viewDidAppear中的,self.presentViewController
(此时:self.presentedViewController=Optional(<UIAlertController: 0x7ab89c00>))
-》此处会导致界面上,出现了选择相册还是照相机的界面,一闪而过
-》会让用户觉得很不爽
3.最后是:
if self.presentedViewController != nil { self.dismissViewControllerAnimated(false, completion: nil) } |
-》总的说就是:
虽然可以实现:
通过两次的self.dismissViewControllerAnimated
最后能让UIAlertController消失掉
但是还是会有一闪而过的问题
-》
可以去viewDidAppear中加判断,如果当前的selectPictureAlertController有值了,就不再显示
也是可以解决问题的,但是总觉得不够正规
UIViewController presentViewController UIAlertController
UIViewController dismiss UIAlertController
最后无奈,还是用笨办法,就是用刚才非正规的办法,去实现:
当已经添加了UIAlertController的action后,就可以了。
最终代码是:
class ImagePickerViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { var selectPictureAlertController:UIAlertController override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.clearColor() } override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) // guard self.selectPictureAlertController.actions.isEmpty else { gLog.verbose(“already added alert actions”) return } self.selectPictureAlertController = UIAlertController(title: “提示”, message: “”, preferredStyle: UIAlertControllerStyle.ActionSheet) let cancelAlertAction = UIAlertAction(title: “取消”, style: .Cancel, handler: self.cancelImagePick(_:)) selectPictureAlertController.addAction(cancelAlertAction) let localAlbumAlertAction = UIAlertAction(title: “打开本地相册”, style: UIAlertActionStyle.Default, handler:self.openLocalAlbum(_:)) selectPictureAlertController.addAction(localAlbumAlertAction) let openCameraAlertAction = UIAlertAction(title: “打开相机”, style: UIAlertActionStyle.Default, handler: self.openCamera(_:)) selectPictureAlertController.addAction(openCameraAlertAction) selectPictureAlertController.modalPresentationStyle = .Custom gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x797567f0>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x79759d10>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7b31d000>) //self=<Sales_App.ImagePickerViewController: 0x797567f0>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x79759d10>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7b31d000>) self.presentViewController(selectPictureAlertController, animated: true, completion: nil) gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x797567f0>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x79759d10>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7b31d000>) //self=<Sales_App.ImagePickerViewController: 0x797567f0>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x79759d10>), self.presentedViewController=Optional(<UIAlertController: 0x7ab89c00>), self.presentingViewController=Optional(<UINavigationController: 0x7b31d000>) } func dissmissCurrentVC() { gLog.verbose(“”) gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x7be90330>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7becccf0>), self.presentedViewController=Optional(<UIImagePickerController: 0x7c168c00>), self.presentingViewController=Optional(<UINavigationController: 0x7a9a1800>) //self=<Sales_App.ImagePickerViewController: 0x797567f0>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x79759d10>), self.presentedViewController=Optional(<UIImagePickerController: 0x7a454e00>), self.presentingViewController=Optional(<UINavigationController: 0x7b31d000>) self.dismissViewControllerAnimated(false, completion: { gLog.verbose(“self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)”) //self=<Sales_App.ImagePickerViewController: 0x7be90330>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7becccf0>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7a9a1800>) //self=<Sales_App.ImagePickerViewController: 0x797567f0>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x79759d10>), self.presentedViewController=Optional(<UIAlertController: 0x7ab89c00>), self.presentingViewController=Optional(<UINavigationController: 0x7b31d000>) // if self.presentedViewController != nil { self.dismissViewControllerAnimated(false, completion: nil) // } if self.pickImageCompletionHandler != nil { self.pickImageCompletionHandler!(pickedImage: self.pickedImage) } //self.selectPictureAlertController.dismissViewControllerAnimated(false, completion: nil) // if self.parentVC != nil { // gLog.verbose(“self.parentVC.presentedViewController=\(self.parentVC!.presentedViewController)”) // //self.parentVC.presentedViewController=Optional(<Sales_App.ImagePickerViewController: 0x12faa2370>) // // if self.parentVC!.presentedViewController != nil { // self.parentVC!.dismissViewControllerAnimated(false, completion: nil) // } // } }) } } |
转载请注明:在路上 » [基本解决]在单独的vc中去presentViewController显示UIAlertController后无法让其消失