Creating Reusable Classes

Updated on August 11, 2017 in Swift 3
1 on August 10, 2017

Hello,

I am wondering if anyone has got a good tutorial or suggestions on how to create reusable Classes. TheSwiftGuy does create really nice videos but in every video he’s creating everything in the “ViewController.swift”-File.

For example: Today i created a UIAlertController in my App and I might reuse this in other Apps aswell. Therefore I should store this UIAlertController-Class in a separate File.

let alertController = UIAlertController(title: "Add New User", message: "", preferredStyle: .alert)
 
        let saveAction = UIAlertAction(title: "Save", style: .default, handler: {
            alert -> Void in
 
            let firstTextField = alertController.textFields![0] as UITextField
            let secondTextField = alertController.textFields![1] as UITextField
            let thirdTextField = alertController.textFields![2] as UITextField
 
            let firstName: String = firstTextField.text!
            let lastName: String = secondTextField.text!
            let emailAdress: String = thirdTextField.text!
        })
 
        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: {
            (action : UIAlertAction!) -> Void in
 
        })
 
        alertController.addTextField { (textField : UITextField!) -> Void in
            textField.placeholder = "Enter Firstname..."
        }
        alertController.addTextField { (textField : UITextField!) -> Void in
            textField.placeholder = "Enter Lastname..."
        }
        alertController.addTextField { (textField : UITextField!) -> Void in
            textField.placeholder = "Enter Email..."
        }
 
        alertController.addAction(saveAction)
        alertController.addAction(cancelAction)
 
        self.present(alertController, animated: true, completion: nil)

How would I call this Class from the separate File in my “ViewController.swift”-File? I really need to know how to use many different swift-files and different Classes in order to get to the next “Knowledge-Level” 🙂

 

Thanks in advance!

 

 
  • Liked by
Reply
0 on August 11, 2017

I have finally made it! 😀 really happy! 😀

If you’re interested in my class, here’s the Code:

import UIKit
class Alert: UIViewController {
    
    enum ButtonType {
        case SaveCancel
        case OKCancel
        case OK
    }
    
    func createAlert(title: String, message: String, numberOfTextfields: Int, buttonType: ButtonType) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
            
        var x = 0
        while x < numberOfTextfields {
            alertController.addTextField { (textField : UITextField!) -> Void in }
            x += 1
        }
            
        switch buttonType {
        case .SaveCancel:
            alertController.addAction(saveButton())
            alertController.addAction(cancelButton())
        case .OKCancel:
            alertController.addAction(okButton())
            alertController.addAction(cancelButton())
        case .OK:
            alertController.addAction(okButton())
        }
        
        //DISPLAY THE ALERTVIEW
        //Thanks to darkngs for this super easy fix of my problem!
        var rootViewController = UIApplication.shared.keyWindow?.rootViewController
        //IN CASE ROOTVIEWCONTROLLER = NAVIGATION CONTROLLER
        if let navigationController = rootViewController as? UINavigationController {
            rootViewController = navigationController.viewControllers.first
        }
        //IN CASE ROOTVIEWCONTROLLER = TAB BAR CONTROLLER
        if let tabBarController = rootViewController as? UITabBarController {
            rootViewController = tabBarController.selectedViewController
        }
        rootViewController?.present(alertController, animated: true, completion: nil)
    }
    
    func saveButton() -> UIAlertAction{
        let saveAction = UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in
            //TODO: SAVE TEXT SOMWHERE
        })
        return saveAction
    }
        
    func cancelButton() -> UIAlertAction {
        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: {(action : UIAlertAction!) -> Void in
        })
        return cancelAction
    }
        
    func okButton() -> UIAlertAction {
        let okAction = UIAlertAction(title: "OK", style: .default, handler: {(action : UIAlertAction!) -> Void in
        })
        return okAction
    }
}

Thats the Alert-Class I have created (it is not entirely done yet)!

In my ViewController I can now simply call this AlertController class:

import UIKit
class ViewController: UIViewController {
    let a = Alert()
    
    @IBAction func buttonPress(_ sender: UIButton) {
        a.createAlert(title: "Title", message: "Message", numberOfTextfields: 0, buttonType: .OK)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

It was easier than I expected – I can now finally start creating “clean” coded Apps 🙂

  • Liked by
Reply
Cancel
Loading more replies