#Apple Store 的 AirTag 刻字 (包含 解決 Xcode Source Control — Push local changes stuck on Loading)

功能展示

  • 利用 isModalInPresentation 禁止頁面下拉
  • 利用 switch 搭配 Label.font.withSize() 控制文字大小
  • 利用 UIAlertController 產生警示匡
  • 利用 prepare() 傳遞頁面資料

頁面之間的連線

首先新增一個 TableViewController ,之後產生 NavigationController

接下來新增一個 UIViewController,之後產生 UINavigationController

最後在 加上鐫刻文字編輯鐫刻 進行拉線,拉線時選擇 present modally

編輯頁面禁止下拉

在 編輯頁面的 override func viewDidLoad() 中,新增 self.isModalInPresentation = true

override func viewDidLoad() {
super.viewDidLoad()
//禁止下拉
self.isModalInPresentation = true

改變 AirTag 上的文字大小

利用 switch Label.text?.count ?? 0 來判斷有幾個字母

  • 4個字母 字體大小設定為 30
  • 3個字母 字體大小設定為 40
  • 2個字母 字體大小設定為 50
  • 1個字母 字體大小設定為 60
switch Label.text?.count ?? 0 {
case 0:
Label.font = Label.font.withSize(60)
case 1:
Label.font = Label.font.withSize(60)
case 2:
Label.font = Label.font.withSize(50)
case 3:
Label.font = Label.font.withSize(40)
case 4:
Label.font = Label.font.withSize(30)
default:
break
}

警示匡的產生

當文字輸入超過 4 個時,會跳出警示匡和一個小的警示鈕,且警示匡只會出現一次,按下 警示鈕才會出現第二次警示匡,要消除警示匡,需要手動將文字刪除,或是按下右邊的清除鍵

資料傳回上一頁

回到上一頁,只能透過左上角的 取消 以及右上角的 儲存

首先在 AirTagTableViewController 新增兩個 @IBAction func unwind

@IBAction func unwindToCancel(_ unwindSegue: UIStoryboardSegue) {}    
@IBAction func unwindToData(_ unwindSegue: UIStoryboardSegue) {
}

接下來進行拉線

利用 AirTagViewController 中的 prepare() 把資料傳回上一頁

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
Labelt = Label.text
}

AirTagTableViewController 剛剛設定的 @IBAction func unwindToData 抓取回傳資料

@IBAction func unwindToData(_ unwindSegue: UIStoryboardSegue) {
if let sourceViewController = unwindSegue.source as? AirTagViewController ,
let data = sourceViewController.Labelt {
Label.text = data

資料傳回編輯頁面

原本抓取的資料,要再一次傳回編輯頁面,這時就有特別的寫法,我嘗試過

IBSegueAction 失敗,prepare 一開始也失敗,我稍微爬文了一下,發現

AirTagTableViewAirTagViewController 之間有 NavigationController

,必須先設定 AirTagTableViewControllerNavigationController

identifier ,兩條都需要設定

設定好 identifier 後,在 AirTagTableViewController 中的 prepare 輸入以下

程式,輸入後即可將資料傳回編輯頁面

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueShowNavigation01" {
if let destVC = segue.destination as? UINavigationController,
let targetController = destVC.topViewController as? AirTagViewController {
targetController.Labelt = Label.text ?? ""
}
}
if segue.identifier == "segueShowNavigation02" {
if let destVC = segue.destination as? UINavigationController,
let targetController = destVC.topViewController as? AirTagViewController {
targetController.Labelt = Label.text ?? ""
}
}

還有很多細節都能改進,官方的程式是不能輸入表情符號的,只能輸入 官方另外的表情符號,所以先由原廠內建鍵盤的表情符號代替,比較花時間的是頁面之間的拉線部分,查了不少文章才找到方法,以及 AirTagTableViewAirTagViewController 之間有 NavigationController 的傳遞資料方法等,如果有更好的方法歡迎留言與我討論謝謝。

Xcode Source Control — Push local changes stuck on Loading

不知道是否有人遇過要 push 更新檔到 Github 時,遇到 Loading 卡住問題?我遇過了兩次,到今天才找到解決方法

第一步

到 Repositories 找到 Branch 的 main ,右鍵點選 Delete

第二步

到 Remotes 點選 main ,右鍵選取 Check Out…

第三步

隨意更新程式碼進行 commit 測試,沒意外 Loading 的問題就會解決

網上主要有兩種方法,第一種是重裝 Xcode ,第二種是用 終端機進行 push ,我覺得第一種有點耗時,所以選擇嘗試第二種,但我第二種不太熟悉,所以爬文時,有看到刪除 Branch 這個方法,經過嘗試也順利解決,如果有人有其他解決方法,歡迎留言討論謝謝。

GitHub

--

--