Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

ios - Access the instance of a Viewcontroller from another in swift

I am trying to transfer data from the textfield of one View Controller to the label from another.

How can I call the View Controller instance from the code of the other View Controller? I'm working with storyboards thus I never created an instance of the View Controllers in the code? Are the instances automatically created? And what name do they have?

Thanks for your help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1. If the view controller containing the textfield can call (with a segue) the view controller containing the label...

Add a new Cocoa Touch class file in your project, name it FirstViewController and set the following code in it:

import UIKit

class FirstViewController: UIViewController {
    
    @IBOutlet weak var textField: UITextField! // FIXME: link this to the UITextField in the Storyboard!!!
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let controller = segue.destinationViewController as! SecondViewController
        controller.text = textField.text
    }
    
}

Add a new Cocoa Touch class file in your project, name it SecondViewController and set the following code in it:

import UIKit

class SecondViewController: UIViewController {
    
    var text: String?
    @IBOutlet weak var label: UILabel! // FIXME: link this to the UILabel in the Storyboard!!!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        label.text = text
    }
    
}

In the Storyboard, embed the first view controller in a UINavigationController. Link the first view controller to the second with a UIButton or a UIBarButtonItem. Set the name of the first view controller to FirstViewController and the name of the second view controller to SecondViewController. Create a UITextField in the first view controller. Create a UILabel in the second view controller. Link the textfield and the label to their respective declarations in FirstViewController and SecondViewController.


2. If the view controller containing the label can call (with a segue) the view controller containing the textfield...

Here, this is a perfect protocol/delegate case. You may find a lot of stuff on StackOverflow dealing with this. However, here is a rough example.

Add a new Cocoa Touch class file in your project, name it FirstViewController and set the following code in it:

import UIKit

class FirstViewController: UIViewController, DetailsDelegate {
    
    @IBOutlet weak var label: UILabel! // FIXME: link this to the UILabel in the Storyboard
    
    func updateLabel(withString string: String?) {
        label.text = string
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let controller = segue.destinationViewController as! SecondViewController
        controller.delegate = self
    }
    
}

Add a new Cocoa/Cocoa Touch class file in your project, name it SecondViewController and set the following code in it:

import UIKit

protocol DetailsDelegate: class {
    func updateLabel(withString string: String?)
}

class SecondViewController: UIViewController {
    
    weak var delegate: DetailsDelegate?
    @IBOutlet weak var textField: UITextField! // FIXME: link this to the UITextField in the Storyboard
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        
        delegate?.updateLabel(withString: textField.text)
    }

}

In the Storyboard, embed the first view controller in a UINavigationController. Link the first view controller to the second with a UIButton or a UIBarButtonItem. Set the name of the first view controller to FirstViewController and the name of the second view controller to SecondViewController. Create a UILabel in the first view controller. Create a UITextField in the second view controller. Link the textfield and the label to their respective declarations in FirstViewController and SecondViewController.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...