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

[已解决]swift获得ViewController被showViewController的调用者sender

Swift crifan 3082浏览 0评论

之前用代码:

                let toAddContactItemList = getToAddPersonItemList()
                let selectPersonVC = SelectPersonViewController(curContactItem: self.curContactItem, inputPersonItemList: toAddContactItemList)
                self.showViewController(selectPersonVC, sender: selectPersonVC)

时,现在在被调用的VC:SelectPersonViewController中,想要获得sender的:

当前的VC:ConversationManageViewController

搜:

swift get showViewController sender

swift get view controller sender

swift get viewcontroller sender

swift get viewcontroller caller

Understanding the ‘sender’ in segues and use it to pass on data to another view controller | Xebia Blog

How to access a variable from another view controller in Swift – Quora

uiwindow – ios swift method to navigate through viewcontroller programatically – Stack Overflow

swift  viewcontroller sender

swift  showViewController sender

swift view controller sender

最后搞定了,用的是如何代码:

func getCallerViewController(curVC:UIViewController) -> UIViewController? {
    //        let parentViewController = self.parentViewController
    //        print("parentViewController=\(parentViewController)")
    //        let presentationController = self.presentationController
    //        print("presentationController=\(presentationController)")
    //        let presentedViewController = self.presentedViewController
    //        print("presentedViewController=\(presentedViewController)")
    //        let presentingViewController = self.presentingViewController
    //        print("presentingViewController=\(presentingViewController)")
   
    //        if let naviViewControllers = self.navigationController?.viewControllers{
    //            for (idx,eachVC) in naviViewControllers.enumerate() {
    //                print("[\(idx)] \(eachVC)")
    //            }
    //        }
   
    /*
    parentViewController=Optional(<UINavigationController: 0x7fce92844e00>)
    presentationController=Optional(<_UIFullscreenPresentationController: 0x7fce914bd830>)
    presentedViewController=nil
    presentingViewController=Optional(<JianDao.LoginViewController: 0x7fce914baac0>)
   
    [0] <JianDao.MainViewController: 0x7f80db037600>
    [1] <JianDao.MessageTableViewController: 0x7f80dc23b000>
    [2] <JianDao.ConversationManageViewController: 0x7f80dc230080>
    [3] <JianDao.SelectPersonViewController: 0x7f80dc0bc0c0>
    */
   
    print("curVC=\(curVC)")
    //<JianDao.SelectPersonViewController: 0x7facfd9906c0>
    var calerVc:UIViewController? = nil
   
    if let naviViewControllers = curVC.navigationController?.viewControllers{
        let vcCount = naviViewControllers.count //3
        let maxVcIdx = vcCount – 1 //3
        let callerVcIdx = maxVcIdx – 1 //2
        if callerVcIdx >= 0 {
            calerVc = naviViewControllers[callerVcIdx]
            //Optional(<JianDao.ConversationManageViewController: 0x7facfd93a870>)
        }
    }
   
    return calerVc
}

其中:

此处假定的前提是:

所有的ViewController都是通过NavigationController调用去压栈和出栈的

显示vc是通过self.showViewController的方式去调用新的vc去显示的。

-》然后navi controller的stack中,才能找到调用者vc。

转载请注明:在路上 » [已解决]swift获得ViewController被showViewController的调用者sender

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.267 seconds, using 22.15MB memory