How to use viewDidLoad method of ViewController class

Best Swift-snapshot-testing code snippet using ViewController.viewDidLoad

AlertControllerCreator.swift

Source:AlertControllerCreator.swift Github

copy

Full Screen

...42class ExitAlertController: UIAlertController {43    var onButtonTapped: (() -> ())?44    45    // ViewController lifecycle methods46    override func viewDidLoad() {47        super.viewDidLoad()48        setupButton()49    }50    51    // UI configure methods52    private func setupButton() {53        let agreeButton = UIAlertAction(title: "Нет", style: .default, handler: nil)54        let disagreeButton = UIAlertAction(title: "Да", style: .destructive) {[unowned self] _ in55            disagreeButtonTapped()56        }57        addAction(disagreeButton)58        addAction(agreeButton)59    }60    61    private func disagreeButtonTapped() {62        onButtonTapped?()63    }64}65class ErrorAlertController: UIAlertController {66    // ViewController lifecycle methods67    override func viewDidLoad() {68        super.viewDidLoad()69        setupButton()70    }71    72    // UI configure methods73    private func setupButton() {74        let reportButton = UIAlertAction(title: "Сообщить об ошибке", style: .default)75        addAction(reportButton)76    }77}78class AddSectionAlertController: UIAlertController {79    weak var delegate: AddSectionAlertDelegate?80    81    // ViewController lifecycle methods82    override func viewDidLoad() {83        super.viewDidLoad()84        setupButton()85        setupTextField()86    }87    88    // UI configure methods89    private func setupButton() {90        let addButton = UIAlertAction(title: "Добавить", style: .cancel) { [unowned self] _ in91            guard let text = self.textFields?[0].text else { return }92            self.delegate?.addNewSection(with: text)93        }94        addAction(addButton)95        96        let closeButton = UIAlertAction(title: "Отменить", style: .default)97        98        addAction(closeButton)99    }100    101    private func setupTextField() {102        addTextField { textField in103            textField.placeholder = "Работа"104        }105    }106}107class ChangeSectionAlertController: UIAlertController {108    weak var delegate: ChangeSectionAlertDelegate?109    var section: Int?110    111    // ViewController lifecycle methods112    override func viewDidLoad() {113        super.viewDidLoad()114        setupButton()115        setupTextField()116    }117    118    // UI configure methods119    private func setupButton() {120        let addButton = UIAlertAction(title: "Cохранить", style: .cancel) { [unowned self] _ in121            guard let text = self.textFields?[0].text,122                  let section = section123            else { return }124            125            self.delegate?.changeSection(with: section, text: text)126        }127        addAction(addButton)128        129        let closeButton = UIAlertAction(title: "Отменить", style: .default)130        131        addAction(closeButton)132    }133    134    private func setupTextField() {135        addTextField { textField in136            textField.placeholder = "Работа"137        }138    }139}140class DeleteSectionAlertController: UIAlertController {141    weak var delegate: DeleteAlertDelegate?142    var section: Int?143    144    // ViewController lifecycle methods145    override func viewDidLoad() {146        super.viewDidLoad()147        setupButton()148    }149    150    // UI configure methods151    private func setupButton() {152        let cancelButton = UIAlertAction(title: "Отменить", style: .cancel)153        let deleteButton = UIAlertAction(title: "Удалить", style: .destructive) { [unowned self] _ in154            guard let section = self.section else { return }155            self.delegate?.deleteSection(section)156        }157        addAction(cancelButton)158        addAction(deleteButton)159    }160    161}162class DeleteTaskAlertController: UIAlertController {163    var onButtonTapped: (() -> ())?164    165    // ViewController lifecycle methods166    override func viewDidLoad() {167        super.viewDidLoad()168        setupButton()169    }170    171    // UI configure methods172    private func setupButton() {173        let agreeButton = UIAlertAction(title: "Отменить", style: .default, handler: nil)174        let disagreeButton = UIAlertAction(title: "Удалить", style: .destructive) {[unowned self] _ in175            disagreeButtonTapped()176        }177        addAction(disagreeButton)178        addAction(agreeButton)179    }180    181    private func disagreeButtonTapped() {182        onButtonTapped?()183    }184}185class PleaseSignInAlertController: UIAlertController {186    // ViewController lifecycle methods187    override func viewDidLoad() {188        super.viewDidLoad()189        setupButton()190    }191    192    // UI configure methods193    private func setupButton() {194        let reportButton = UIAlertAction(title: "Хорошо", style: .default)195        addAction(reportButton)196    }197}...

Full Screen

Full Screen

UIViewController+AddBehaviors.swift

Source:UIViewController+AddBehaviors.swift Github

copy

Full Screen

...9// Where ViewController on didLoad adds behaviour which observes keyboard frame10// and scrollView content inset changes based on keyboard frame.11import UIKit12protocol ViewControllerLifecycleBehavior {13    func viewDidLoad(viewController: UIViewController)14    func viewWillAppear(viewController: UIViewController)15    func viewDidAppear(viewController: UIViewController)16    func viewWillDisappear(viewController: UIViewController)17    func viewDidDisappear(viewController: UIViewController)18    func viewWillLayoutSubviews(viewController: UIViewController)19    func viewDidLayoutSubviews(viewController: UIViewController)20}21// Default implementations22extension ViewControllerLifecycleBehavior {23    func viewDidLoad(viewController: UIViewController) {}24    func viewWillAppear(viewController: UIViewController) {}25    func viewDidAppear(viewController: UIViewController) {}26    func viewWillDisappear(viewController: UIViewController) {}27    func viewDidDisappear(viewController: UIViewController) {}28    func viewWillLayoutSubviews(viewController: UIViewController) {}29    func viewDidLayoutSubviews(viewController: UIViewController) {}30}31extension UIViewController {32    /*33     Add behaviors to be hooked into this view controller’s lifecycle.34     This method requires the view controller’s view to be loaded, so it’s best to call35     in `viewDidLoad` to avoid it being loaded prematurely.36     - parameter behaviors: Behaviors to be added.37     */38    func addBehaviors(_ behaviors: [ViewControllerLifecycleBehavior]) {39        let behaviorViewController = LifecycleBehaviorViewController(behaviors: behaviors)40        addChild(behaviorViewController)41        view.addSubview(behaviorViewController.view)42        behaviorViewController.didMove(toParent: self)43    }44    private final class LifecycleBehaviorViewController: UIViewController, UIGestureRecognizerDelegate {45        private let behaviors: [ViewControllerLifecycleBehavior]46        // MARK: - Lifecycle47        init(behaviors: [ViewControllerLifecycleBehavior]) {48            self.behaviors = behaviors49            super.init(nibName: nil, bundle: nil)50        }51        required init?(coder aDecoder: NSCoder) {52            fatalError("init(coder:) has not been implemented")53        }54        override func viewDidLoad() {55            super.viewDidLoad()56            view.isHidden = true57            applyBehaviors { behavior, viewController in58                behavior.viewDidLoad(viewController: viewController)59            }60        }61        override func viewWillAppear(_ animated: Bool) {62            super.viewWillAppear(animated)63            applyBehaviors { behavior, viewController in64                behavior.viewWillAppear(viewController: viewController)65            }66        }67        override func viewDidAppear(_ animated: Bool) {68            super.viewDidAppear(animated)69            applyBehaviors { behavior, viewController in70                behavior.viewDidAppear(viewController: viewController)71            }72        }...

Full Screen

Full Screen

AchievementsViewControllerTests.swift

Source:AchievementsViewControllerTests.swift Github

copy

Full Screen

...25        viewController = nil26    }27    28    func testViewDidLoad() {29        viewController.viewDidLoad()30        XCTAssertNotNil(viewController.view)31    }32    33    func testViewWillAppear() {34        viewController.viewDidLoad()35        viewController.viewWillAppear(true)36        XCTAssertTrue(presenterMock.viewWillAppearCalled)37    }38    39    func testUpdateView() {40        viewController.viewDidLoad()41        viewController.viewWillAppear(true)42        viewController.updateView()43        XCTAssertEqual(viewController.title, "Smart Investing")44    }45    46    func testTableViewHasDelegate() {47        viewController.viewDidLoad()48        XCTAssertNotNil(viewController.achievementsTableView.delegate)49    }50    51    func testTableViewHasDataSource() {52        viewController.viewDidLoad()53        XCTAssertNotNil(viewController.achievementsTableView.dataSource)54    }55    56    func testTableViewConformsToTableViewDataSourceProtocol() {57        viewController.viewDidLoad()58        XCTAssertTrue(viewController.conforms(to: UITableViewDataSource.self))59        XCTAssertTrue(viewController.responds(to: #selector(viewController.tableView(_:numberOfRowsInSection:))))60        XCTAssertTrue(viewController.responds(to: #selector(viewController.tableView(_:cellForRowAt:))))61    }62    63    func testTableViewConfromsToTableViewDelegateProtocol() {64        viewController.viewDidLoad()65        XCTAssertTrue(viewController.conforms(to: UITableViewDelegate.self))66        XCTAssertTrue(viewController.responds(to: #selector(viewController.tableView(_:willDisplay:forRowAt:))))67    }68    69    func testTableViewCellHasReuseIdentifier() {70        viewController.viewDidLoad()71        let cell = viewController.tableView(viewController.achievementsTableView, cellForRowAt: IndexPath(row: 0, section: 0)) as? AchievementTableViewCell72        let actualReuseIdentifer = cell?.reuseIdentifier73        let expectedReuseIdentifier = "AchievementCellIdentifier"74        XCTAssertEqual(actualReuseIdentifer, expectedReuseIdentifier)75    }76    77    func testNoOfRowsInTableView() {78        viewController.viewDidLoad()79        let rows = viewController.tableView(viewController.achievementsTableView, numberOfRowsInSection: 0)80        XCTAssertGreaterThan(rows, 0)81    }82    83    func testTableViewCellForIndexPath() {84        // Given85        let expectedModel = presenterMock.achievementUIModel(for: 0)86        // When87        viewController.viewDidLoad()88        let cell = viewController.tableView(viewController.achievementsTableView, cellForRowAt: IndexPath(row: 1, section: 0)) as? AchievementTableViewCell89        // Then90        XCTAssertEqual(cell?.levelLabel.text, expectedModel.level)91        XCTAssertEqual(cell?.completedPointsLabel.text, "10pts")92        XCTAssertEqual(cell?.alpha, 1.0)93    }94}...

Full Screen

Full Screen

viewDidLoad

Using AI Code Generation

copy

Full Screen

1class ViewController: UIViewController {2    override func viewDidLoad() {3        super.viewDidLoad()4    }5}6class ViewController: UIViewController {7    override func viewDidLoad() {8        super.viewDidLoad()9    }10}11class ViewController: UIViewController {12    override func viewDidLoad() {13        super.viewDidLoad()14    }15}16class ViewController: UIViewController {17    override func viewDidLoad() {18        super.viewDidLoad()19    }20}21class ViewController: UIViewController {22    override func viewDidLoad() {23        super.viewDidLoad()24    }25}26class ViewController: UIViewController {27    override func viewDidLoad() {28        super.viewDidLoad()29    }30}31class ViewController: UIViewController {32    override func viewDidLoad() {33        super.viewDidLoad()34    }35}36class ViewController: UIViewController {37    override func viewDidLoad() {38        super.viewDidLoad()39    }40}41class ViewController: UIViewController {42    override func viewDidLoad() {43        super.viewDidLoad()44    }45}46class ViewController: UIViewController {47    override func viewDidLoad() {48        super.viewDidLoad()49    }50}51class ViewController: UIViewController {52    override func viewDidLoad() {53        super.viewDidLoad()54    }55}

Full Screen

Full Screen

viewDidLoad

Using AI Code Generation

copy

Full Screen

1class ViewController: UIViewController {2    override func viewDidLoad() {3        super.viewDidLoad()4    }5}6class ViewController: UIViewController {7    override func viewDidLoad() {8        super.viewDidLoad()9    }10}11class ViewController: UIViewController {12    override func viewDidLoad() {13        super.viewDidLoad()14    }15}16extension ViewController {17}

Full Screen

Full Screen

viewDidLoad

Using AI Code Generation

copy

Full Screen

1import UIKit2class ViewController: UIViewController {3  override func viewDidLoad() {4    super.viewDidLoad()5  }6}7import UIKit8class ViewController: UIViewController {9  override func viewDidLayoutSubviews() {10    super.viewDidLayoutSubviews()11  }12}13import UIKit14class ViewController: UIViewController {15  override func viewDidAppear(_ animated: Bool) {16    super.viewDidAppear(animated)17  }18}19import UIKit20class ViewController: UIViewController {21  override func viewWillAppear(_ animated: Bool) {22    super.viewWillAppear(animated)23  }24}25import UIKit26class ViewController: UIViewController {27  override func viewWillDisappear(_ animated: Bool) {28    super.viewWillDisappear(animated)29  }30}31import UIKit32class ViewController: UIViewController {33  override func viewDidDisappear(_ animated: Bool) {34    super.viewDidDisappear(animated)35  }36}37import UIKit38class ViewController: UIViewController {39  override func didReceiveMemoryWarning() {40    super.didReceiveMemoryWarning()41  }42}43import UIKit44class ViewController: UIViewController {45  override func viewWillLayoutSubviews() {46    super.viewWillLayoutSubviews()47  }48}49import UIKit50class ViewController: UIViewController {51  override func viewWillTransition(to size: CGSize

Full Screen

Full Screen

viewDidLoad

Using AI Code Generation

copy

Full Screen

1import UIKit2class ViewController: UIViewController {3    override func viewDidLoad() {4        super.viewDidLoad()5    }6}7import UIKit8class ViewController: UIViewController {9    override func viewDidLoad() {10        super.viewDidLoad()11    }12}13import UIKit14class ViewController: UIViewController {15    override func viewDidLoad() {16        super.viewDidLoad()17    }18}19import UIKit20class ViewController: UIViewController {21    override func viewDidLoad() {22        super.viewDidLoad()23    }24}25import UIKit26class ViewController: UIViewController {27    override func viewDidLoad() {28        super.viewDidLoad()29    }30}31import UIKit32class ViewController: UIViewController {33    override func viewDidLoad() {34        super.viewDidLoad()35    }36}37import UIKit38class ViewController: UIViewController {39    override func viewDidLoad() {40        super.viewDidLoad()41    }42}43import UIKit44class ViewController: UIViewController {45    override func viewDidLoad() {46        super.viewDidLoad()47    }48}49import UIKit50class ViewController: UIViewController {51    override func viewDidLoad() {52        super.viewDidLoad()53    }54}55import UIKit56class ViewController: UIViewController {57    override func viewDidLoad() {58        super.viewDidLoad()59    }60}61import UIKit62class ViewController: UIViewController {

Full Screen

Full Screen

viewDidLoad

Using AI Code Generation

copy

Full Screen

1import UIKit2class ViewController: UIViewController {3    override func viewDidLoad() {4        super.viewDidLoad()5        print("Hello World!")6    }7    override func didReceiveMemoryWarning() {8        super.didReceiveMemoryWarning()9    }10}11import UIKit12class ViewController: UIViewController {13    override func viewDidLoad() {14        super.viewDidLoad()15        print("Hello World!")16    }17    override func didReceiveMemoryWarning() {18        super.didReceiveMemoryWarning()19    }20}21import UIKit22class ViewController: UIViewController {23    override func viewDidLoad() {24        super.viewDidLoad()25        print("Hello World!")26    }27    override func didReceiveMemoryWarning() {28        super.didReceiveMemoryWarning()29    }30}31import UIKit32class ViewController: UIViewController {33    override func viewDidLoad() {34        super.viewDidLoad()35        print("Hello World!")36    }37    override func didReceiveMemoryWarning() {38        super.didReceiveMemoryWarning()39    }40}41import UIKit42class ViewController: UIViewController {43    override func viewDidLoad() {44        super.viewDidLoad()45        print("Hello World!")46    }47    override func didReceiveMemoryWarning() {48        super.didReceiveMemoryWarning()49    }50}51import UIKit52class ViewController: UIViewController {53    override func viewDidLoad() {54        super.viewDidLoad()55        print("Hello World!")56    }57    override func didReceiveMemoryWarning() {58        super.didReceiveMemoryWarning()59    }60}

Full Screen

Full Screen

viewDidLoad

Using AI Code Generation

copy

Full Screen

1override func viewDidLoad() {2    super.viewDidLoad()3    let backgroundImage = UIImageView(frame: UIScreen.main.bounds)4    backgroundImage.image = UIImage(named: "background.png")5    self.view.insertSubview(backgroundImage, at: 0)6}7override func viewDidLoad() {8    super.viewDidLoad()9    let backgroundImage = UIImageView(frame: UIScreen.main.bounds)10    backgroundImage.image = UIImage(named: "background.png")11    self.view.insertSubview(backgroundImage, at: 0)12}13override func viewDidLoad() {14    super.viewDidLoad()15    let backgroundImage = UIImageView(frame: UIScreen.main.bounds)16    backgroundImage.image = UIImage(named: "background.png")17    self.view.insertSubview(backgroundImage, at: 0)18}19override func viewDidLoad() {20    super.viewDidLoad()21    let backgroundImage = UIImageView(frame: UIScreen.main.bounds)22    backgroundImage.image = UIImage(named: "background.png")23    self.view.insertSubview(backgroundImage, at: 0)24}25override func viewDidLoad() {26    super.viewDidLoad()27    let backgroundImage = UIImageView(frame: UIScreen.main.bounds)28    backgroundImage.image = UIImage(named: "background.png")29    self.view.insertSubview(backgroundImage, at: 0)30}31override func viewDidLoad() {32    super.viewDidLoad()33    let backgroundImage = UIImageView(frame: UIScreen.main.bounds)34    backgroundImage.image = UIImage(named: "background.png")35    self.view.insertSubview(backgroundImage, at: 0)36}37override func viewDidLoad() {38    super.viewDidLoad()

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Swift-snapshot-testing automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful