如何从 xib 调用 performSegueWithIdentifier? [英] How to call performSegueWithIdentifier from xib?

查看:25
本文介绍了如何从 xib 调用 performSegueWithIdentifier?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有segue的viewController到名为toSecond"的secondViewController.在 viewController 我加载 customView.xib

I have viewController with segue to secondViewController named "toSecond". In viewController i load customView.xib

let myCustomView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil)[0]

在这个自定义视图中,我有按钮操作:

in this customView i have button with action:

viewController().goToSecond()

在viewController中我有这个代码的功能

in viewController i have function with this code

func goToSecond() {
self.performSegueWithIdentifier("toSecond", sender: self)
}

但是当我在 customView 中按下按钮时,我变成了一个错误:

but when i pressed button in customView i become a error:

viewController 没有标识符为 'toSecond' 的 segue

viewController has no segue with identifier 'toSecond'

当我直接从 viewController 调用这个函数时,一切都很好!

when i call this function directly from viewController all work great!

那么,如何从我的 customView 中调用 performSegueWithIdentifier?

So, how can i call performSegueWithIdentifier from my customView?

customView 源代码:

customView source code:

import UIKit

class customView: UIView {

@IBAction func ToSecondButton(sender: AnyObject) {
viewController().goToSecond() }

}

viewController 源代码:

viewController source code:

import UIKit

class viewController: UIViewController {

...
let myCustomView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil)[0]
self.view.addSubview(myCustomView)
func goToSecond() {
    self.performSegueWithIdentifier("toSecond", sender: self)
    }
...

}

推荐答案

问题在于您的 UIView 子类正在调用 viewController().goToSecond().那不是在做你认为的那样.viewController() 没有引用加载自定义视图的视图控制器.它正在实例化该类的第二个孤立实例(未连接到任何故事板),因此无法找到转场.

The problem is that your UIView subclass is calling viewController().goToSecond(). That's not doing what you think it is. The viewController() isn't referencing the view controller that loaded your custom view. It's instantiating a second, orphaned instance of that class (not connected to any storyboard) and therefore cannot find the segue.

如果您真的要让这个自定义 UIView 子类启动转场,您需要将原始视图控制器的引用传递给自定义视图.因此,向自定义视图子类添加一个属性,该属性可以保存对其视图控制器的引用,并且当视图控制器实例化此自定义视图时,它必须设置该属性.

If you're really going to have this custom UIView subclass initiate a segue, you need to pass a reference to your original view controller to the custom view. So add a property to the custom view subclass that can hold the reference to its view controller, and when the view controller instantiates this custom view, it has to set that property.

例如:

import UIKit

protocol CustomViewDelegate: class {         // make this class protocol so you can create `weak` reference
    func goToNextScene()
}

class CustomView: UIView {

    weak var delegate: CustomViewDelegate?   // make this `weak` to avoid strong reference cycle b/w view controller and its views

    @IBAction func toSecondButton(sender: AnyObject) {
        delegate?.goToNextScene() 
    }

}

然后

import UIKit

class ViewController: UIViewController, CustomViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let myCustomView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil)[0] as! CustomView
        myCustomView.delegate = self

        // ... do whatever else you want with this custom view, adding it to your view hierarchy
    }


    func goToNextScene() {
        performSegueWithIdentifier("toSecond", sender: self)
    }

    ...

}

这篇关于如何从 xib 调用 performSegueWithIdentifier?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆