将具有屏幕视图的另一个 class 的代表设置为自己
Setting delegate of another class with screen view to self
我在 iOS 编程方面相当陌生。我有这个设置:
ViewController
在 IB 上的视图,class ViewController
SecondController
在 IB 上的视图,class secondController
我有协议:
protocol SecondControllerDelegate {
func getSomething() -> String
}
并且我在 SecondController 上有委托变量:
class secondController: UIViewController {
var delegate: SecondControllerDelegate?
@IBOutlet weak var labelStatus: UILabel!
override func ViewDidLoad() {
super.viewDidLoad()
}
@IBAction func buttonTouch(sender: AnyObject) {
labelStatus.text = delegate?.getSomething()
}
func try () {
labelStatus.text = "testing"
}
}
现在,根据到处的提示,为了让我可以在SecondController.buttonTouch()
调用delegate?.getSomething()
,我需要在viewController上这样设置:
class ViewController: UIViewController, SecondControllerDelegate {
override func viewDidLoad () {
super.viewDidLoad()
SecondController.delegate = self
}
func doSomething () -> String {
return "testing"
}
}
但这会产生错误 'SecondController.type' does not have a member named 'delegate'
。
其他一些网站说:
class ViewController: UIViewController, SecondControllerDelegate {
var secondController = SecondController()
override func viewDidLoad () {
super.viewDidLoad()
secondController.delegate = self
}
func doSomething () -> String {
return "testing"
}
}
有了这个,就没有错误了。但是如果我在第二个屏幕上做一些应该调用委托的事情,它不会调用委托,就像 SecondController 是两个不同的对象(一个是由 StoryBoard 创建的,一个是在 ViewController 中手动创建的),即本应更改为 "testing" 的 labelStatus
根本没有更改。但如果函数 try()
被调用,它就会改变。我该怎么做?
编辑:我忘了提到我使用了 NavigationController,并从第一个屏幕过渡到第二个屏幕。
因为您尝试学习如何在 Swift 中构建委托,所以我在下面为您编写了一个简单的委托示例
protocol SecondViewControllerDelegate {
func didReceiveInformationFromSecondViewcontroller (information: String)
}
class ViewController: UIViewController, SecondViewControllerDelegate {
func openSecondViewController () {
if let secondViewControllerInstance: SecondViewController = storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as? SecondViewController {
secondViewControllerInstance.delegate = self
navigationController?.pushViewController(secondViewControllerInstance, animated: true)
}
}
func didReceiveInformationFromSecondViewcontroller(information: String) {
////Here you get the information, after sendInfoToViewController() has been executed
}
}
class SecondViewController: UIViewController {
var delegate: SecondViewControllerDelegate?
func sendInfoToViewController () {
delegate?.didReceiveInformationFromSecondViewcontroller("This ist the information")
}
}
更新
在使用 Storyboard Segues 时遵循同样的做法
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let secondViewControllerInstance: SecondViewController = segue.destinationViewController as? SecondViewController {
secondViewControllerInstance.delegate = self
}
}
我在 iOS 编程方面相当陌生。我有这个设置:
ViewController
在 IB 上的视图,class ViewController
SecondController
在 IB 上的视图,class secondController
我有协议:
protocol SecondControllerDelegate {
func getSomething() -> String
}
并且我在 SecondController 上有委托变量:
class secondController: UIViewController {
var delegate: SecondControllerDelegate?
@IBOutlet weak var labelStatus: UILabel!
override func ViewDidLoad() {
super.viewDidLoad()
}
@IBAction func buttonTouch(sender: AnyObject) {
labelStatus.text = delegate?.getSomething()
}
func try () {
labelStatus.text = "testing"
}
}
现在,根据到处的提示,为了让我可以在SecondController.buttonTouch()
调用delegate?.getSomething()
,我需要在viewController上这样设置:
class ViewController: UIViewController, SecondControllerDelegate {
override func viewDidLoad () {
super.viewDidLoad()
SecondController.delegate = self
}
func doSomething () -> String {
return "testing"
}
}
但这会产生错误 'SecondController.type' does not have a member named 'delegate'
。
其他一些网站说:
class ViewController: UIViewController, SecondControllerDelegate {
var secondController = SecondController()
override func viewDidLoad () {
super.viewDidLoad()
secondController.delegate = self
}
func doSomething () -> String {
return "testing"
}
}
有了这个,就没有错误了。但是如果我在第二个屏幕上做一些应该调用委托的事情,它不会调用委托,就像 SecondController 是两个不同的对象(一个是由 StoryBoard 创建的,一个是在 ViewController 中手动创建的),即本应更改为 "testing" 的 labelStatus
根本没有更改。但如果函数 try()
被调用,它就会改变。我该怎么做?
编辑:我忘了提到我使用了 NavigationController,并从第一个屏幕过渡到第二个屏幕。
因为您尝试学习如何在 Swift 中构建委托,所以我在下面为您编写了一个简单的委托示例
protocol SecondViewControllerDelegate {
func didReceiveInformationFromSecondViewcontroller (information: String)
}
class ViewController: UIViewController, SecondViewControllerDelegate {
func openSecondViewController () {
if let secondViewControllerInstance: SecondViewController = storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as? SecondViewController {
secondViewControllerInstance.delegate = self
navigationController?.pushViewController(secondViewControllerInstance, animated: true)
}
}
func didReceiveInformationFromSecondViewcontroller(information: String) {
////Here you get the information, after sendInfoToViewController() has been executed
}
}
class SecondViewController: UIViewController {
var delegate: SecondViewControllerDelegate?
func sendInfoToViewController () {
delegate?.didReceiveInformationFromSecondViewcontroller("This ist the information")
}
}
更新
在使用 Storyboard Segues 时遵循同样的做法
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let secondViewControllerInstance: SecondViewController = segue.destinationViewController as? SecondViewController {
secondViewControllerInstance.delegate = self
}
}