I am trying to emulate an Objective-C template to access an object delegate using Swift. Normally I would put the protocol in .h, which is shared between two UIViewControllers.
Purpose: Call a delegate (host) to reject (push) the UIViewController.
Problem: Unable to access delegate greeting ().
The following codes compile, but I get an error at runtime: unknown delegation method (see below).
Host / Calling (Delegate) Object:
import UIKit
class MainViewController: UIViewController, ProtocolNameDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
func hello() {
println("Hello");
}
@IBAction func greenAction(sender : AnyObject) {
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("GreenViewController") as GreenViewController
secondViewController.delegate = self;
self.presentViewController(secondViewController, animated: true, completion: nil)
}
@IBAction func exitAction(sender : AnyObject) {
exit(0)
}
An advanced (second or "green") UIViewController that needs to be fired:
import UIKit
@class_protocol protocol ProtocolNameDelegate {
func hello()
}
class GreenViewController: UIViewController {
weak var delegate: ProtocolNameDelegate?
@IBAction func returnAction(sender : UIBarButtonItem) {
println("Inside returnAction")
delegate?.hello()
}
}
Editorial: fixed delegate access to: delegate? .Hello () ... And restart the application. Below is the debugger:
(lldb) po delegate
Some
{
Some = {
payload_data_0 = 0x0d110b90 -> 0x00009a78 (void *)0x00009b70: OBJC_METACLASS_$__TtC9RicSwift218MainViewController
payload_data_1 = 0x0d110b90 -> 0x00009a78 (void *)0x00009b70: OBJC_METACLASS_$__TtC9RicSwift218MainViewController
payload_data_2 = 0x00000000
instance_type = 0x00000000
}
}
(lldb) po delegate.hello()
error: <REPL>:1:1: error: 'ProtocolNameDelegate?' does not have a member named 'hello'
delegate.hello()
(lldb) po delegate?.hello()
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=2, address=0xb3145b0).
The process has been returned to the state before expression evaluation.
: - ?