ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Mobile Application Development 2
    COMP4126
    Progress0 / 38 topics
    Topics
    1. Creating an iOS App: Understanding Xcode2. Using the Xcode interface builder and objects library3. Understanding view hierarchy and creating a custom app icon4. Outlets, Actions, and Views: Understanding outlets and actions5. Using text fields, buttons, labels, web views, and page controllers6. Using views with subviews and creating views using code7. Using View Controllers: Working with the single view template8. Exploring the app delegate and adding new view controllers9. Transitioning between multiple view controllers using animations10. Application Templates: Tabbar and master detail templates11. The iOS Keyboard: Customizing for different inputs12. Adjusting text field behaviors and dismissing the keyboard13. Detecting keyboard activities with notification center14. Using scroll view and responding to keyboard activities programmatically15. Working with Different iOS Devices (iPhone & iPad): Detecting device hardware16. Dynamically adjusting graphical layouts and creating universal apps17. Using Table Views: Understanding UITableView and UITableViewCell18. Working with UITableView data source and delegate19. Master detail template, drill-down menus, and navigation20. Using property lists for data persistence and creating multi-section tables21. Supporting Screen Rotations: Portrait and landscape modes22. Handling device rotation and forcing specific orientation23. Dynamically adjusting layouts based on rotation24. Working with Databases: Importing sqlite3 and creating a database25. Writing tables, inserting records, and bundling a database with your app26. Checking for database existence and reading/displaying data27. Using Animations & Video: NSTimer class and object transformations28. Rotation, scaling, translation, animating image arrays, and playing video29. Accessing Integrated iOS Apps: Email, Safari, and SMS30. Working with camera and photo library31. Using Web Services: Consuming and parsing XML and JSON32. Integrating Twitter and Facebook with iOS apps33. Working with iOS Maps and Location Services: MapKit and MKMapView34. Getting and displaying user location and directional information35. Displaying map annotations, disclosure buttons, and reverse geocoding36. Working with iCloud37. Working with the Accelerometer: Gyroscope and accelerometer38. Outputting sensor data and using the Shake API
    COMP4126›Using View Controllers: Working with the single view template
    Mobile Application Development 2Topic 7 of 38

    Using View Controllers: Working with the single view template

    3 minread
    536words
    Beginnerlevel

    📱 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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time3 min
      Word count536
      Code examples0
      DifficultyBeginner