how can i access IBOutlet in another class? in swift

I'm sorry i have bad eng skill.

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

firstly this is 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
}

}

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() is work in ViewController's viewDidLoad() but error in SecondViewController with this setance

fatal error : unexpectedly found nil while unwrapping an Optional value

how can i fix this problem?


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.


This is because the ViewController is not yet loaded. You create a variable to store it temporarily. When the ViewController appears, under your viewDidLoad method, assign the variable to the IBOutlet.

Example:

In the FirstViewController you want to pass a string message say "pass message" to SecondViewController and display on a label of IBOutlet. You can't assign the string directly to SecondViewController on FirstViewController . You have to create a string first.

When your SecondViewController loads, in the viewDidLoad method, assign the "pass message" string to the label to display. That is how you display on your IBOutlet.

Just in case:

class FirstViewController: UIViewController{
 // All your above codes.

func PassMessage(){
     let displayViewController = storyboard!.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
     displayViewController.message = "pass message"
    }
}

In SecondViewController

class SecondViewController: UIViewController{
@IBOutlet weak var displayLabel: UILabel!
var message = ""

override func viewDidLoad(){
     super.viewDidLoad()
     self.displayLabel.text = message
    }
}

Note: Assign SecondViewController in the StoryboardID.


我已经使用这个函数来处理以前的类函数操作

let appdel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let nav : UINavigationController! = window.rootViewController as! UINavigationController

for(var i:Int = 0 ; i < nav.viewControllers.count ; i++)
    {
        let viewc :  UIViewController = nav.viewControllers[i]
        if(viewc.respondsToSelector("showListViewController"))
        {
            viewc.performSelector("showListViewController")
        }
    }
链接地址: http://www.djcxy.com/p/87746.html

上一篇: “致命错误:意外地发现零,而解包可选值”是什么意思?

下一篇: 我如何在另一个课程中访问IBOutlet? 迅速