我如何在另一个班级访问IBOutlet?在迅速 [英] how can i access IBOutlet in another class? in swift

查看:86
本文介绍了我如何在另一个班级访问IBOutlet?在迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我有糟糕的技能。

I'm sorry i have bad eng skill.

我希望在另一个班级中访问IBOutlet。
i在stackoverflow中发现了很多问题,但是我的代码中的所有答案都不起作用

i want access IBOutlet in another class. i found many question in stackoverflow, but all answer not working in my code

首先这是我的代码。

ViewController.swift

import UIKit

class ViewController: UIViewController {

@IBOutlet var MainViewController: UIView! = nil
@IBOutlet var LyricsViewController: UIView! = nil
@IBOutlet var ListViewController: UIView! = nil
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

   showMainViewController()    
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func showMainViewController(){
    MainViewController.alpha = 1
    LyricsViewController.alpha = 0
    ListViewController.alpha = 0
}
func showLyricsViewController(){
    MainViewController.alpha = 0
    LyricsViewController.alpha = 1
    ListViewController.alpha = 0
}
func showListViewController(){
    MainViewController.alpha = 0
    LyricsViewController.alpha = 0
    ListViewController.alpha = 1
}

}

我希望在这个类中访问ViewController的函数

and i want access ViewController's function in this class

SecondViewController.swift

import UIKit

class SecondViewController : UIViewController{

@IBOutlet weak var menuButton: UIButton!
var listBoolean = false

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func LinkListView(sender: AnyObject) {

    if(listBoolean == false){
        ViewController().showListViewController()
        listBoolean = true
    }
    else{
        ViewController().showMainViewController()
        listBoolean = false

    }

}

}

func showMainViewController()在ViewController的viewDidLoad()
中工作但在SecondViewController中有此setance的错误

func showMainViewController() is work in ViewController's viewDidLoad() but error in SecondViewController with this setance


致命错误:在展开可选值时意外发现nil

fatal error : unexpectedly found nil while unwrapping an Optional value

我该如何解决这个问题?

how can i fix this problem?

推荐答案

你必须创建一个视图控制器对象然后访问它。
Ex: -

You have to create a object of view controller and then access it. Ex:-

var dash : abcViewController = storyBoard.instantiateViewControllerWithIdentifier("abc") as! abcViewController
var a = dash.getXYZ()

像这样你可以访问变量和其他视图控制器的功能。

Like this way you access the variable and function of other view controller.

这篇关于我如何在另一个班级访问IBOutlet?在迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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