ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP4126View Controllers Singlโ€ฆ
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP4126

    Mobile Application Development 2

    Progress0 / 38 done
    Topics38
    1Understanding Xcode2Using the Xcode interface builder and objects library3Understanding view hierarchy and creating a custom app icon
    4Understanding outlets and actions5Using text fields, buttons, labels, web views, and page controllers6Using views with subviews and creating views using code
    7Working with the single view template8Exploring the app delegate and adding new view controllers9Transitioning between multiple view controllers using animations
    10Tabbar and master detail templates
    11Customizing for different inputs12Adjusting text field behaviors and dismissing the keyboard13Detecting keyboard activities with notification center14Using scroll view and responding to keyboard activities programmatically
    15Detecting device hardware16Dynamically adjusting graphical layouts and creating universal apps
    17Understanding UITableView and UITableViewCell18Working with UITableView data source and delegate19Master detail template, drill-down menus, and navigation20Using property lists for data persistence and creating multi-section tables
    21Portrait and landscape modes22Handling device rotation and forcing specific orientation23Dynamically adjusting layouts based on rotation
    24Importing sqlite3 and creating a database25Writing tables, inserting records, and bundling a database with your app26Checking for database existence and reading/displaying data
    27NSTimer class and object transformations28Rotation, scaling, translation, animating image arrays, and playing video
    29Email, Safari, and SMS30Working with camera and photo library
    31Consuming and parsing XML and JSON32Integrating Twitter and Facebook with iOS apps
    33MapKit and MKMapView34Getting and displaying user location and directional information35Displaying map annotations, disclosure buttons, and reverse geocoding36Working with iCloud
    37Gyroscope and accelerometer38Outputting sensor data and using the Shake API
    3 min read
    Topic 7 of 38
    Topic Study Note Illustration
    07

    Using View Controllers: Working with the single view template

    A View Controller is a core component in iOS that manages a screen (view) and handles its behavior, logic, and user interactions.

    ๐Ÿ“ฑ Using View Controllers: Working with the Single View Template (iOS โ€“ Xcode)


    โœ… 1. Definition

    A View Controller is a core component in iOS that manages a screen (view) and handles its behavior, logic, and user interactions.

    The Single View Template in Xcode is the simplest app template, providing one main screen controlled by a single view controller.


    ๐Ÿง  2. Key Concepts

    ๐Ÿ”น View Controller

    • A class (usually UIViewController) that:

      • Controls UI elements
      • Responds to user actions
      • Manages data

    ๐Ÿ”น Single View App

    • Default template when creating a new project

    • Contains:

      • One screen
      • One view controller (ViewController.swift)

    ๐Ÿ”น Lifecycle of a View Controller

    Method Purpose
    viewDidLoad() Called when view loads into memory
    viewWillAppear() Before view appears
    viewDidAppear() After view appears
    viewWillDisappear() Before view disappears

    ๐Ÿ—๏ธ 3. Structure of Single View Template

    When you create a project, you get:

    • AppDelegate.swift โ†’ App lifecycle
    • SceneDelegate.swift โ†’ Scene management
    • ViewController.swift โ†’ Main screen logic
    • Main.storyboard โ†’ UI design

    ๐Ÿ“Š Diagram Description (for Exams)

    Draw:

    App โ†’ ViewController โ†’ View โ†’ UI Elements (Label, Button)
    

    โš™๏ธ 4. Steps to Work with Single View Template


    ๐Ÿ”น Step 1: Create Project

    • Open Xcode
    • Select App (iOS)
    • Choose Single View App

    ๐Ÿ”น Step 2: Understand Default Files

    ViewController.swift

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Code here
        }
    }
    

    ๐Ÿ‘‰ viewDidLoad() is where initial setup is done


    ๐Ÿ”น Step 3: Design UI

    • Open Main.storyboard

    • Drag:

      • Label
      • Button

    ๐Ÿ”น Step 4: Connect UI to Code

    @IBOutlet weak var label: UILabel!
    
    @IBAction func buttonTapped(_ sender: UIButton) {
        label.text = "Hello iOS!"
    }
    

    ๐Ÿ”น Step 5: Run the App

    • Select Simulator
    • Click Run โ–ถ๏ธ

    ๐Ÿงฉ 5. Working with View Controller Code


    ๐Ÿ”น Example: Changing Background Color

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .lightGray
    }
    

    ๐Ÿ”น Example: Show Message on Load

    override func viewDidLoad() {
        super.viewDidLoad()
        print("View Loaded")
    }
    

    ๐Ÿ“Œ 6. Important Rules / Tips

    • Always write setup code inside viewDidLoad()
    • Keep UI logic inside View Controller
    • Avoid putting too much code in one controller
    • Use meaningful names for outlets/actions
    • Test frequently

    ๐Ÿ’ก 7. Example Application

    ๐ŸŽฏ Simple Greeting App

    UI:

    • TextField โ†’ Enter name
    • Button โ†’ Submit
    • Label โ†’ Display message

    Code:

    @IBOutlet weak var nameField: UITextField!
    @IBOutlet weak var messageLabel: UILabel!
    
    @IBAction func showGreeting(_ sender: UIButton) {
        messageLabel.text = "Hello \(nameField.text ?? "")"
    }
    

    โš ๏ธ 8. Common Mistakes

    • โŒ Forgetting to connect outlets
    • โŒ Writing code outside lifecycle methods
    • โŒ Not updating UI on main thread
    • โŒ Overloading ViewController with too much logic

    ๐Ÿ“ 9. Likely Exam Questions

    1. Define View Controller in iOS.
    2. What is the Single View Template?
    3. Explain the lifecycle of a view controller.
    4. What is the role of viewDidLoad()?
    5. Describe the structure of a Single View App.
    6. How do you connect UI elements to ViewController?
    7. Write code to change label text on button click.
    8. Draw a diagram showing ViewController relationship.

    ๐Ÿ“š 10. Quick Revision Summary

    • View Controller manages one screen

    • Single View Template = one screen app

    • Main file: ViewController.swift

    • UI designed in Storyboard

    • Use:

      • IBOutlet โ†’ connect UI
      • IBAction โ†’ handle actions
    • Important method:

      • viewDidLoad() โ†’ initial setup

    โ† Previous topic 6
    Using views with subviews and creating views using code
    Next topic 8 โ†’
    Exploring the app delegate and adding new view controllers

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    3mremaining
    536words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Mobile Application Development 2