最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中给已有的ViewController中添加UIAlertAction并直接显示和正常消失返回被调用者视图

iOS crifan 2999浏览 0评论

现在用代码:

        let imagePickVC = ImagePickerViewController()
        imagePickVC.modalPresentationStyle = .Custom
        self.presentViewController(imagePickVC, animated: true, completion: nil)

和:

class ImagePickerViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    var selectPictureAlertController:UIAlertController
    var pickedImage:UIImage
    var attachmentIdList:[String]
    init(){
        self.selectPictureAlertController = UIAlertController()
        self.pickedImage = UIImage()
        self.attachmentIdList = [String]()
        super.init(nibName: nil, bundle: nil)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.clearColor()
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        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)
    }

可以显示了:

但是,最后选择图片:

结束后,去让页面消失,试了各种代码:

                let parentVC = self.parentViewController
                gLog.verbose("self=\(self), parentVC=\(parentVC), self.selectPictureAlertController=\(self.selectPictureAlertController)")
                //self=<Sales_App.ImagePickerViewController: 0x7c6f1640>, parentVC=nil, self.selectPictureAlertController=<UIAlertController: 0x7d429e00>
                self.selectPictureAlertController.dismissViewControllerAnimated(false, completion: nil)
                self.dismissViewControllerAnimated(false, completion: {
                    //self.dismissViewControllerAnimated(true, completion: nil)
//                    SingletonRootNC().popViewControllerAnimated(true)
//                    parentVC?.dismissViewControllerAnimated(true, completion: nil)
                })
//                SingletonRootNC().popViewControllerAnimated(true)

都无法让AlertAction消失了:

swift alert actionsheet dismiss

ios – Dismiss UIAlertView after 5 Seconds Swift – Stack Overflow

ios – How do I pop to RootViewController when an alert is dismissed? – Stack Overflow

swift UIAlertController dismiss

ios – How to dismiss an UIAlertController with no actions in SWIFT? – Stack Overflow

ios – How to programatically dismiss UIAlertController without any buttons? – Stack Overflow

ios – Prevent UIAlertController to dismiss – Stack Overflow

after UIImagePickerController dismiss viewcontroller

After displaying and dismissing the Modal View Controller UIImagePickerController my Cocos2d iPhone app doesn’t see multiple touches anymore – Stack Overflow

iphone – Presenting a modal view controller immediately after dismissing another – Stack Overflow

ios – DIsmissing a UIImagePickerController is also making the parent dissapear – Stack Overflow

ios – UIAlertController Error When called in a modal presented view controller in swift – Stack Overflow

ios – How to present UIAlertController when not in a view controller? – Stack Overflow

swift – Prevent dismissal of UIAlertController – Stack Overflow

ios – Prevent UIAlertController to dismiss – Stack Overflow

折腾了半天:

                gLog.verbose("self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController), self.selectPictureAlertController=\(self.selectPictureAlertController)")
                //self=<Sales_App.ImagePickerViewController: 0x7c6f1640>, parentVC=nil, self.selectPictureAlertController=<UIAlertController: 0x7d429e00>
                //self=<Sales_App.ImagePickerViewController: 0x7c730010>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7bfc3ad0>), self.presentedViewController=Optional(<UIImagePickerController: 0x7d9a7a00>), self.presentingViewController=Optional(<UINavigationController: 0x7d152400>), self.selectPictureAlertController=<UIAlertController: 0x7e17e200>
                gLog.verbose("SingletonRootNC().viewControllers=\(SingletonRootNC().viewControllers)")
                //SingletonRootNC().viewControllers=[<Sales_App.MainViewController: 0x7b2dbe00>, <Sales_App.CustomerDetailViewController: 0x7aa7fa00>]
                //SingletonRootNC().viewControllers=[<Sales_App.MainViewController: 0x7c90d000>, <Sales_App.CustomerDetailViewController: 0x7d14c200>]
//                self.selectPictureAlertController.dismissViewControllerAnimated(false, completion: nil)
//                self.dismissViewControllerAnimated(false, completion: {
//                    dispatchMain_sync(0.1, thingsTodo: {
//                        self.selectPictureAlertController.dismissViewControllerAnimated(false, completion: nil)
//
//                        dispatchMain_sync(0.1, thingsTodo: {
//                            self.dismissViewControllerAnimated(false, completion: nil)
//                        })
//                    })
//                } )
//                dispatchMain_sync(0.05, thingsTodo: {
//                    self.selectPictureAlertController.dismissViewControllerAnimated(false, completion: nil)
//                    
//                    dispatchMain_sync(0.2, thingsTodo: {
//                        self.dismissViewControllerAnimated(false, completion: nil)
//                    })
//                })
//                dispatchMain_sync(0.05, thingsTodo: {
//                    self.selectPictureAlertController.dismissViewControllerAnimated(false, completion: nil)
//                    
//                    dispatchMain_sync(0.2, thingsTodo: {
//                        self.dismissViewControllerAnimated(false, completion: nil)
//                    })
//                })
//                dispatchMain_async({
                    gLog.verbose("self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)")
                    //self=<Sales_App.ImagePickerViewController: 0x7be7ad40>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7bf56830>), self.presentedViewController=Optional(<UIImagePickerController: 0x7c1c3a00>), self.presentingViewController=Optional(<UINavigationController: 0x7d902c00>)
                    //self=<Sales_App.ImagePickerViewController: 0x7a9d5f70>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7a9e1540>), self.presentedViewController=Optional(<UIImagePickerController: 0x7a268c00>), self.presentingViewController=Optional(<UINavigationController: 0x7926ac00>)
                    self.dismissViewControllerAnimated(false, completion: {
//                        dispatchMain_async({
                            gLog.verbose("self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)")
                            //self=<Sales_App.ImagePickerViewController: 0x7be7ad40>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7bf56830>), self.presentedViewController=Optional(<UIAlertController: 0x7d93e800>), self.presentingViewController=Optional(<UINavigationController: 0x7d902c00>)
                            //self=<Sales_App.ImagePickerViewController: 0x7a9d5f70>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7a9e1540>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7926ac00>)
                            self.dismissViewControllerAnimated(false, completion: {
//                                dispatchMain_async({
                                    gLog.verbose("self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)")
                                    //self=<Sales_App.ImagePickerViewController: 0x7be7ad40>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7bf56830>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7d902c00>)
                                    //self=<Sales_App.ImagePickerViewController: 0x7a9d5f70>, self.parentViewController=nil, self.presentationController=nil, self.presentedViewController=Optional(<UIAlertController: 0x79b46a00>), self.presentingViewController=nil
                                    self.dismissViewControllerAnimated(false, completion: nil)
//                                })
                            })
//                        })
                    })
//                })
//                self.dismissViewControllerAnimated(true, completion: {
//                    //self.dismissViewControllerAnimated(true, completion: nil)
////                    SingletonRootNC().popViewControllerAnimated(true)
////                    parentVC?.dismissViewControllerAnimated(true, completion: nil)
//                    
//                    gLog.verbose("SingletonRootNC().viewControllers=\(SingletonRootNC().viewControllers)")
//                    //SingletonRootNC().viewControllers=[<Sales_App.MainViewController: 0x7b2dbe00>, <Sales_App.CustomerDetailViewController: 0x7aa7fa00>]
//                    
//                    dispatchMain_sync(0.1, thingsTodo: {
//                        self.selectPictureAlertController.dismissViewControllerAnimated(true, completion: nil)
//                        
//                        gLog.verbose("SingletonRootNC().viewControllers=\(SingletonRootNC().viewControllers)")
//                        
////                        SingletonRootNC().popViewControllerAnimated(true)
////                        gLog.verbose("SingletonRootNC().viewControllers=\(SingletonRootNC().viewControllers)")
//                    })
//                })
//                self.selectPictureAlertController.dismissViewControllerAnimated(false, completion: {
////                    dispatchMain_sync(0.1, thingsTodo: {
//                        self.dismissViewControllerAnimated(false, completion: nil)
////                    })
//                })

最终用代码:

                gLog.verbose("self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController), self.selectPictureAlertController=\(self.selectPictureAlertController)")
                //self=<Sales_App.ImagePickerViewController: 0x7c6f1640>, parentVC=nil, self.selectPictureAlertController=<UIAlertController: 0x7d429e00>
                //self=<Sales_App.ImagePickerViewController: 0x7c730010>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7bfc3ad0>), self.presentedViewController=Optional(<UIImagePickerController: 0x7d9a7a00>), self.presentingViewController=Optional(<UINavigationController: 0x7d152400>), self.selectPictureAlertController=<UIAlertController: 0x7e17e200>
                gLog.verbose("SingletonRootNC().viewControllers=\(SingletonRootNC().viewControllers)")
                //SingletonRootNC().viewControllers=[<Sales_App.MainViewController: 0x7b2dbe00>, <Sales_App.CustomerDetailViewController: 0x7aa7fa00>]
                //SingletonRootNC().viewControllers=[<Sales_App.MainViewController: 0x7c90d000>, <Sales_App.CustomerDetailViewController: 0x7d14c200>]
                gLog.verbose("self=\(self), self.parentViewController=\(self.parentViewController), self.presentationController=\(self.presentationController), self.presentedViewController=\(self.presentedViewController), self.presentingViewController=\(self.presentingViewController)")
                //self=<Sales_App.ImagePickerViewController: 0x7be7ad40>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7bf56830>), self.presentedViewController=Optional(<UIImagePickerController: 0x7c1c3a00>), self.presentingViewController=Optional(<UINavigationController: 0x7d902c00>)
                //self=<Sales_App.ImagePickerViewController: 0x7a9d5f70>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7a9e1540>), self.presentedViewController=Optional(<UIImagePickerController: 0x7a268c00>), self.presentingViewController=Optional(<UINavigationController: 0x7926ac00>)
                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: 0x7be7ad40>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7bf56830>), self.presentedViewController=Optional(<UIAlertController: 0x7d93e800>), self.presentingViewController=Optional(<UINavigationController: 0x7d902c00>)
                    //self=<Sales_App.ImagePickerViewController: 0x7a9d5f70>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7a9e1540>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7926ac00>)
                    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: 0x7be7ad40>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7bf56830>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7d902c00>)
                        //self=<Sales_App.ImagePickerViewController: 0x7a9d5f70>, self.parentViewController=nil, self.presentationController=nil, self.presentedViewController=Optional(<UIAlertController: 0x79b46a00>), self.presentingViewController=nil
                        self.dismissViewControllerAnimated(false, completion: nil)
                    })
                })

实现了,可以正常显示:

点击➕后:

点击 打开本地相册:

点选图片后,正常消失掉:

-》然后通过三次的输出:

self=<Sales_App.ImagePickerViewController: 0x7be7ad40>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7bf56830>), self.presentedViewController=Optional(<UIImagePickerController: 0x7c1c3a00>), self.presentingViewController=Optional(<UINavigationController: 0x7d902c00>)
self=<Sales_App.ImagePickerViewController: 0x7be7ad40>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7bf56830>), self.presentedViewController=Optional(<UIAlertController: 0x7d93e800>), self.presentingViewController=Optional(<UINavigationController: >)
self=<Sales_App.ImagePickerViewController: 0x7be7ad40>, self.parentViewController=nil, self.presentationController=Optional(<UIPresentationController: 0x7bf56830>), self.presentedViewController=nil, self.presentingViewController=Optional(<UINavigationController: 0x7d902c00>)

可以看出:

在点击了 打开相册,去选择相册之后,而返回之前的页面时:

对于之前通过调用:

self.presentViewController

即:

        imagePickVC.modalPresentationStyle = .Custom
        self.presentViewController(imagePickVC, animated: true, completion: nil)
-》
self.presentViewController(selectPictureAlertController, animated: false, completion: nil)
-》
self.presentViewController(imagePicker, animated: true, completion: nil)

的话:

(1)正在显示的页面是self.presentedViewController

对于始终都是用当前的vc的

self.presentedViewController

来表示的

(2)用于显示字页面的上一级页面是self.presentationController

上述三层调用,都是:

self.presentationController=Optional(<UIPresentationController: 0x7bf56830>)

(3)当前的父页面parent是空的:

self.parentViewController=nil

(4)对应着源码中的解释:

    /*
      If this view controller is a child of a containing view controller (e.g. a navigation controller or tab bar
      controller,) this is the containing view controller.  Note that as of 5.0 this no longer will return the
      presenting view controller.
    */
    weak public var parentViewController: UIViewController? { get }
    // The view controller that was presented by this view controller or its nearest ancestor.
    @available(iOS 5.0, *)
    public var presentedViewController: UIViewController? { get }
    // The view controller that presented this view controller (or its farthest ancestor.)
    @available(iOS 5.0, *)
    public var presentingViewController: UIViewController? { get }

先后有三个(层)页面:

1.选择图片的页面是:<UIImagePickerController: 0x7c1c3a00>

2.alert view的<UIAlertController: 0x7d93e800>

3.当前vc中显示出了的那个:UIAlertController: 0x79b46a00>

通过:

self.dismissViewControllerAnimated()

去让其消失掉

而最终会到,最开始的,调用此ImagePickerViewController的页面中。

[后记]

改为了独立的vc,结果还是有问题:

[基本解决]在单独的vc中去presentViewController显示UIAlertController后无法让其消失

转载请注明:在路上 » [已解决]swift中给已有的ViewController中添加UIAlertAction并直接显示和正常消失返回被调用者视图

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.213 seconds, using 22.13MB memory