📱 Exploring the App Delegate & Adding New View Controllers (iOS – Xcode)
🧩 PART 1: Exploring the App Delegate
✅ 1. Definition
The App Delegate is a central class in an iOS app that handles the app’s lifecycle events such as launching, backgrounding, and termination.
👉 File name: AppDelegate.swift
🧠 2. Key Concepts
🔹 App Lifecycle
The sequence of states an app goes through:
- Launch
- Active
- Background
- Terminated
🔹 Role of App Delegate
- App startup configuration
- Handling system-level events
- Managing app state transitions
⚙️ 3. Important Methods in App Delegate
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
return true
}
📌 Common Methods
| Method |
Purpose |
didFinishLaunchingWithOptions |
Called when app launches |
applicationDidEnterBackground |
App goes to background |
applicationWillEnterForeground |
App returns to foreground |
applicationWillTerminate |
App is about to close |
📊 Diagram Description (for Exams)
Draw lifecycle flow:
Launch → Active → Background → Foreground → Terminated
📌 Important Rules
- App Delegate runs before UI appears
- Used for global app settings
- Avoid placing UI logic here
💡 Example
func applicationDidEnterBackground(_ application: UIApplication) {
print("App in Background")
}
🧭 PART 2: Adding New View Controllers
✅ 1. Definition
A View Controller manages a single screen.
Adding new view controllers allows you to create multiple screens in your app.
🧠 2. Key Concepts
🔹 Multiple Screens
- Each screen = one view controller
- Navigation between screens is required
🔹 Types of Navigation
- Push (Navigation Controller)
- Present (Modal)
⚙️ 3. Steps to Add a New View Controller
🔹 Step 1: Open Storyboard
🔹 Step 2: Add View Controller
- Drag View Controller from Objects Library
🔹 Step 3: Design UI
- Add labels, buttons, etc.
🔹 Step 4: Create Swift File
- File → New → Cocoa Touch Class
- Name it (e.g.,
SecondViewController)
🔹 Step 5: Link Class to View Controller
- Select new screen → Identity Inspector
- Set class name
🔹 Step 6: Add Navigation
🔸 Option 1: Segue (Storyboard)
🔸 Option 2: Programmatically
let vc = storyboard?.instantiateViewController(withIdentifier: "SecondVC") as! SecondViewController
self.present(vc, animated: true)
🔹 Step 7: Set Storyboard ID
- In Identity Inspector → set identifier (e.g., "SecondVC")
🔁 4. Passing Data Between View Controllers
vc.data = "Hello from First Screen"
📊 Diagram Description (for Exams)
Draw:
ViewController1 → (Button Click) → ViewController2
📌 5. Important Rules / Tips
- Always assign Storyboard ID
- Use Navigation Controller for push navigation
- Keep each ViewController focused on one task
- Avoid strong coupling between controllers
💡 6. Example
🎯 Two-Screen App
Screen 1:
Screen 2:
👉 On button click → navigate to screen 2
⚠️ 7. Common Mistakes
- ❌ Not setting Storyboard ID
- ❌ Not linking class properly
- ❌ Wrong segue type
- ❌ Force unwrapping causing crash
📝 8. Likely Exam Questions
- Define App Delegate.
- Explain the role of App Delegate in iOS.
- Describe app lifecycle states.
- What is
didFinishLaunchingWithOptions?
- How do you add a new view controller in Xcode?
- Explain different navigation methods.
- Write code to navigate to another screen.
- How is data passed between view controllers?
📚 9. Quick Revision Summary
🔹 App Delegate
- Manages app lifecycle
- Handles system-level events
- Key method:
didFinishLaunchingWithOptions
🔹 View Controllers
👉 Multi-screen apps require multiple view controllers