Fast delegate between two view controllers without segue

My delegate protocol never called

My first controller is ViewController

class ViewController: UIViewController,testProtocol { @IBAction func btInit(sender: AnyObject) { println("Bt Init") let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as targetViewController self.presentViewController(initViewController,animated: false, nil) } var targetController = targetViewController(); override func viewDidLoad() { super.viewDidLoad() self.targetController.delegate = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func testDelegate(){ println(" in my view controller delegate ") } } 

In my second view, the controller is targetViewController

 protocol testProtocol { func testDelegate() // this function the first controllers } class targetViewController: UIViewController { @IBAction func BtTarget(sender: AnyObject) { println("bt target pressed") delegate?.testDelegate() } var delegate : testProtocol? override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func testDelegate(){ println(" in my target view controller delegate ") } } 

Why did testDelegate () never call ViewController? What am I doing wrong? Thanks.

I read a lot of posts about this, but all examples are given with segue transition, and I don't want to use segue.

+5
source share
1 answer

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)).
+11
source

All Articles