π± Using Text Fields, Buttons, Labels, Web Views, and Page Controllers in iOS (Xcode)
β
1. Overview
These UI components are essential for building interactive iOS apps:
- Text Field β User input
- Button β Trigger actions
- Label β Display text
- Web View β Display web content
- Page Controller β Swipe between screens
π§ 2. Key UI Components
πΉ 1. Text Field (UITextField)
π Definition
A Text Field allows users to enter text input.
π Uses
- Login forms (username, password)
- Search bars
βοΈ Example
@IBOutlet weak var nameField: UITextField!
let userName = nameField.text
πΉ 2. Label (UILabel)
π Definition
A Label displays read-only text.
π Uses
- Titles
- Messages
- Output display
βοΈ Example
@IBOutlet weak var resultLabel: UILabel!
resultLabel.text = "Welcome!"
πΉ 3. Button (UIButton)
π Definition
A Button is used to trigger an action when tapped.
π Uses
- Submit forms
- Navigate screens
βοΈ Example
@IBAction func submitPressed(_ sender: UIButton) {
resultLabel.text = "Submitted!"
}
πΉ 4. Web View (WKWebView)
π Definition
A Web View is used to display web pages inside the app.
π Uses
- Open websites
- Show online content
βοΈ Example
import WebKit
@IBOutlet weak var webView: WKWebView!
if let url = URL(string: "https://www.apple.com") {
webView.load(URLRequest(url: url))
}
πΉ 5. Page Controller (UIPageViewController)
π Definition
A Page Controller allows users to swipe between multiple screens/pages.
π Uses
- App tutorials
- Image sliders
- Onboarding screens
ποΈ 3. How They Work Together
π Example Flow:
- User enters text in Text Field
- Presses Button
- Action triggered
- Result shown in Label
- Optional: Open link in Web View
- Navigate pages using Page Controller
π Diagram Description (for Exams)
Draw:
TextField β Button β IBAction β Label Update
β
WebView (optional)
β
Page Controller (multiple screens)
βοΈ 4. Step-by-Step Implementation
πΉ Step 1: Add UI Elements
From Objects Library:
-
Drag:
- Text Field
- Label
- Button
- Web View
πΉ Step 2: Connect Outlets
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var webView: WKWebView!
πΉ Step 3: Add Button Action
@IBAction func showText(_ sender: UIButton) {
label.text = textField.text
}
πΉ Step 4: Load Web Content
if let url = URL(string: "https://example.com") {
webView.load(URLRequest(url: url))
}
πΉ Step 5: Setup Page Controller
Basic steps:
- Create multiple view controllers
- Embed in UIPageViewController
- Enable swipe navigation
π 5. Important Rules / Tips
- Use secure text entry for passwords
- Always validate user input
- Keep UI responsive with Auto Layout
- Do not overload one screen with too many elements
- Use Page Controller for better UX in tutorials
π‘ 6. Example Application
π― Simple Search App
- Text Field β Enter URL
- Button β Load page
- Web View β Display website
@IBAction func loadWebsite(_ sender: UIButton) {
if let text = textField.text,
let url = URL(string: text) {
webView.load(URLRequest(url: url))
}
}
π 7. Likely Exam Questions
- Define UITextField and its uses.
- What is UILabel? Explain with example.
- How does UIButton work in iOS?
- Explain WKWebView and its purpose.
- What is UIPageViewController?
- Write code to display text from TextField to Label.
- How do you load a webpage inside an app?
- Explain interaction between UI components.
π 8. Quick Revision Summary
- Text Field β Input
- Label β Output
- Button β Action trigger
- Web View β Display web pages
- Page Controller β Swipe between screens
π Flow:
User Input β Button β Action β Output / Navigation