委托创建ViewController的新实例

时间:2017-01-14 23:59:56

标签: ios swift delegates

我通过委托方法将数据发送到视图控制器。在print(self)中我viewDidLoad我接收数据的视图控制器中,我得到此值:

<PageViewControllerImage: 0x7fba16d0dc40>

如果我在委托函数中print(self),我得到这个值:

<PageViewControllerImage: 0x7fba16e0eab0>

我相信我是如何定义委托的,它正在创建PageViewControllerImage的新实例,但是我无法弄清楚如何获得它的现有引用?

这就是我用来在发送数据的类中设置委托:

self.indexDelegate = PageViewControllerImage()

------编辑--------

我正在尝试从我的PageViewController类发送数据:

var indexDelegate: GetMyIndexDelegate?



func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if (completed && finished) {
        if let currentVC = pageViewController.viewControllers?.last {
            let index = vcArr.index(of: currentVC)

            if index == 3 {

                // call the delegate function in delegate class
                indexDelegate?.getIndex(index: index!)

            }
        }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    // just been trying to define instance first
    let storyboard: UIStoryboard = UIStoryboard (name: "Main", bundle: nil)
    let PageViewControllerImageInstance: PageViewControllerImage = storyboard.instantiateViewController(withIdentifier: "vc4") as! PageViewControllerImage
    print("Defined Instance \(PageViewControllerImageInstance)")
    self.indexDelegate = PageViewControllerImageInstance

然后在类PageViewControllerImage:

中接收数据
protocol GetMyIndexDelegate {
    func getIndex(index: Int)
}

import UIKit

class PageViewControllerImage: UIViewController, GetMyIndexDelegate {

//delegate functionto get index
func getIndex(index: Int) {
    if index == 3 {
        print("Inside delegate: \(self)")

    }
}

@IBOutlet var signupImage: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    print("viewDidLoad: \(self)")

    // Do any additional setup after loading the view.
}

即使在设置委托之前定义viewController的实例,它仍会返回不同的实例编号:

Defined Instance <ParseStarterProject_Swift.PageViewControllerImage: 0x7fb365f06260>

viewDidLoad: <ParseStarterProject_Swift.PageViewControllerImage: 0x7fb365d0c940>

Inside delegate: <ParseStarterProject_Swift.PageViewControllerImage: 0x7fb365f06260>

2 个答案:

答案 0 :(得分:0)

代表通常是外部设置的,应该是可选的。

我将假设indexDelegateViewController上的属性。您应该在转换到PageViewControllerImage实例时设置委托。

这可以通过几种方式实现,具体取决于您如何转换到PageViewControllerImage控制器。

如果您设置了故事板segue,则可以在prepareForSegue中设置委托。 E.g:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showPageViewControllerImage" {
        guard let pageViewControllerImage = segue.destination as? PageViewControllerImage
        else {
            return
        }
        self.delegate = pageViewControllerImage
    }
}

或者,如果您手动转换到新控制器,则可以将委托设置为该实例。

答案 1 :(得分:0)

正在pagecontroller类中实例化viewControllerAfter

将委托定义到pageViewController的正确实例。

self.indexDelegate = self.childViewControllers [2] as! PageViewControllerImage