Its always good to reuse code and UI in application. In this blog we will learn how to reuse view using “addSubview” Method.

Step 1 – Create Project– Create single view application

Step 2 – Create UI and add new files

1. Create another viewcontroller in storyboard and create new file “SecondViewcontroller”.

2. Add button in “ViewController” class and navigate to “SecondViewcontroller”

3. Drag view in both view controller

4. Embed navigation bar to navigate from one screen to another –>Go Editor menu – embed in – navigation controller

Screen Shot 2017-09-16 at 12.37.23 PM.png

Step 3 – Create common view Xib and file

  1. Create new file CommonView  ->> Click on file – new – Cocoa touch class – next (click) – subclass of UIView
  2. Create new .XIB for common view ->> Click on file – new – view (user interface) –  give name CommonView
  3.  Change Custom class to CommonView

Screen Shot 2017-09-16 at 12.28.31 PM.png

4. Change Simulated matrix – Size – Freeform and resize .Xib

Screen Shot 2017-09-16 at 12.31.38 PM.png

Step 4 – Create Outlet

ViewController class –

for View – @IBOutlet weak var firstdView: UIView!

SecondViewController class –

for View – @IBOutlet weak var secondView: UIView!

CommonView class –

for Label –    @IBOutlet weak var lblCommon: UILabel!

for Button   –    @IBAction func btnCommon(_ sender: Any) {  }

Step 5 – Add common view to both viewcontroller as Subview

Please find code in each file and comments which is easily understandable

CommonView.Swift

import UIKit

protocol CommonViewDeleget {
    func btnCommonClick()
}

class CommonView: UIView {
    
    var deleget : CommonViewDeleget?
    
    @IBOutlet weak var lblCommon: UILabel!
        
    @IBAction func btnCommon(_ sender: Any) {
    
        self.deleget?.btnCommonClick()
    }
}

ViewController Class :

import UIKit

class ViewController: UIViewController, CommonViewDeleget {
   
    @IBOutlet weak var firstView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        //---- initialise CommonView
        let commonView = UINib(nibName: "CommonView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CommonView
        firstView.addSubview(commonView)  //---- to add "commonView" as subview
        
        commonView.deleget = self   //---- delegete
        
        commonView.lblCommon.text = "We can change label in particular Screen"
    }
    
    
    //MARK: commonViewDeleget
    func btnCommonClick() {
        
        print("Button Click")
    }
}

SecondViewController Class :

import UIKit

class SecondViewController: UIViewController {
    
    @IBOutlet weak var secondView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //---- initialise CommonView
        let commonView = UINib(nibName: "CommonView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CommonView
        secondView.addSubview(commonView)  //---- to add "commonView" as subview
        
    }
}

githubDownloadButton.png You can download source code

240_F_77959340_hWLiOY93juohUoXqjKzqPB79552nw8XU

Hope you find this blog useful. Please feel free to contact with me in case you have any query, suggestions.  You can comment, like and follow posts.

You can request any topic related to Swift and iOS development.

Donate any small amount you think for this knowledge to grow this forum.                        

To Donate –  paypal.me/SandeshSardar

download

 

Advertisements