Usually you set the delegation property of the new view controller to prepareForSegue: You said you weren't using segue, so you need to instantiate the second controller and present it somehow. You can do this by doing something like:
let storyboard = UIStoryboard(name: "AStoryboardName", bundle: nil) let secondVC = storyboard.instantiateViewControllerWithIdentifier(anIdentifier) as! targetViewController secondVC.delegate = self presentViewController(secondVC, animated: true, completion: nil)
You have the testDelegate() method in both , but you only want it in the first view controller. Then your second view controller can call delegate?.testDelegate() at the appropriate time.
Finally, you usually want the delegate properties to be weak, so I would recommend changing var delegate : testProtocol? on weak var delegate: testProtocol?
I would honor the delegation. Here is a relatively simple five-step delegation process that can help you:
Delegation in 5 steps:
object A is a delegate for object B, and object B sends messages:
- Define the delegate protocol for object B.
- Give object B an additional delegate variable. This variable should be weak.
- Make object B send messages to its delegate when something interesting happens, such as a user, by clicking on the Cancel or Finish buttons, or when he needs information.
- Make Object A conform to the delegate protocol. He must indicate the protocol name in his class line and implement the methods from the protocol.
- Tell object B that object A is now its delegate (in prepareForSegue (sender)).
source share