Skip to content

Instantly share code, notes, and snippets.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var pageA: PageA?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
pageA = PageA()
let nav: UINavigationController = UINavigationController(rootViewController: pageA!)
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "PageA"
self.view.backgroundColor = UIColor.white
// 生成切換至PageB的按鈕
let switchBtn: UIButton = UIButton()
switchBtn.frame = CGRect(x: self.view.frame.size.width / 2 - 100, y: self.view.frame.size.height / 2 - 25, width: 200, height: 50)
switchBtn.titleLabel?.font = UIFont.systemFont(ofSize: 30)
switchBtn.setTitle("切換至PageB", for: .normal)
class PageB: UIViewController, UITableViewDelegate, UITableViewDataSource {
var delegate: PageBDelegate! // delegate
var testDataTV: UITableView!
var dataList: [String] = ["Apple", "Banana", "Cherry"] // 畫面上顯示的資料
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "PageB"
// 切換至PageB
func onClickToPageB(_ sender: UIButton) {
let pageB: PageB = PageB()
pageB.delegate = self // 由我(PageA)來代理你(PageB)
self.navigationController?.pushViewController(pageB, animated: true)
}
enum Shape {
case rectangle
case circle
case other
}
enum Shape: Int {
case rectangle = 1
case circle = 2
case other = 3
}
enum Shape {
case rectangle(Int)
case circle(Int, Int)
case other
}
enum Shape: Int {
case rectangle = 1
case circle = 2
case other = 3
}
func chooseShape(shape: Shape) {
switch shape {
case .rectangle:
print("長方形...")
enum Shape {
case rectangle(Int, Int)
case circle(Double)
case other
}
func chooseShape(shape: Shape) {
switch shape {
case .rectangle(let width, let height):
print("長方形的面積為: \(width * height)")
enum Shape: Int {
case rectangle = 1
case circle = 2
case other = 3
}
func chooseShape(shape: Shape) {
switch shape {
case .rectangle:
print("長方形")