Follow by Email

Search This Blog

Monday, 9 November 2015

How to use Protocol Delegate in iOS using Swift language

Define Protocol

There are two controllers:
FirstViewController
SecondViewController

I need to notify FirstViewController that SecondViewController ViewDidAppearCalled



import UIKit

//Define Your protocole here after Import

protocol SecondViewControllerDelegate{
      
    //Add a new function
    func SecondViewDidLoadFinished(controller:SecondViewController)

}



class SecondViewController: UIViewController {
   
    
    var delegate:SecondViewControllerDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

    }

override func viewDidAppear(animated: Bool) {
        
        if let delegate = self.delegate{
            delegate.SecondViewDidLoadFinished(self)
        }
    }




First view

import UIKit



class FirstViewController: UIViewController,SecondViewControllerDelegate {

    
    override func viewDidLoad() {
        super.viewDidLoad()

    }


@IBAction func buttonTapped(sender:UIButton){
        
        let mapObject:SecondViewControllerSecondViewController()
        mapObject.delegate = self
        self.navigationController?.pushViewController(mapObject, animated: true)   
    }

func SecondViewDidLoadFinished(controller: SecondViewController) {

//Here you will be notified that second view viewDid appear loaded
        
    }


3 comments:

  1. Silicon IT Hub stays innovator in offshore iPhone application development India with skilled and experienced iOS development team that takes care of your varied iPhone and iPad application development projects.

    ReplyDelete
  2. you are always welcome ! Thanks for your comment

    ReplyDelete