janice 程式學習區
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: ios 第三週 0619 tags: ios --- > [👩‍💻 回主頁](https://hackmd.io/@janice-code/after_school_ios) > [內文參考來源](https://chunyeung.medium.com/swift-%E8%AB%87%E8%AB%87%E5%B9%BE%E5%80%8B%E9%87%8D%E8%A6%81%E7%9A%84-ios-app-life-cycle-%E7%94%9F%E5%91%BD%E9%80%B1%E6%9C%9F-93380fe93d95) ## 生命週期 ![](https://i.imgur.com/YXdd3K4.png) :::info 使用 app transitions 執行以下任務: * 在啟動時,初始化 app 的資料結構及 UI。 * App 活化時,完成配置 UI 並準備與用戶互動。 * App 停用後,保存數據並靜止 app 的行為。 * 進入後台狀態後,完成關鍵任務、盡可能釋放內存,並準備 app 快照。 * 終止時,立即停止所有的工作,並釋放所有共享的資源。 ::: - Not Running: 在一開始 App 還沒點開 / App 已被關閉後,App 的狀態就會在這邊,此時 App 不會給在記憶體當中。 - Inactive: 當我們點開 App 後,App 的狀態就會轉為 Inactive,此時 App 開始做一點 SetUp,但是我們還不能使用它。很多 App 在此時都會使用一些準備畫面來渡過這個階段。 - Active: 此時 App 已經進入啟動的狀態,App 可以開始接收事件與回傳事件。UI 也可以開始運作,也就是我們平常在用的 App。 - Background: 當我們把 App 關掉或換另一個 App,如按 Home 鍵後。App 就會進入 Inactive 狀態與 Background。此時,App 都還是在運作的狀態,但是 Interface 就不會進行更新或顯示。在幾秒之後,App 就會入進 Suspended 狀態。 - Suspended: 當 App 進入 Suspended 狀態,App 就不能再執行任何的程度碼,但它仍然在記憶體中存放著。當下一次我們重新打開 App時,App 就會重新回到 Background,再回到 Inactive,最後達到 Action ,進入可以使用的狀態。 --- ![](https://i.imgur.com/1ZdIH3Y.png) - viewDidLoad: 當 view 被載入到記憶體後,但畫面未被顯示之前,就會呼叫viewDidLoad。我們通常會重寫 (overrider) 這方法來增加對 view 的初始化。 - viewWillAppear: 這方法是當在 view 將呈現在畫面,且還沒有任何的動畫開始出現在畫面上時被呼叫的。我們可以重寫這個方法來執行與 view 關聯的自定義任務。例如,您可以使用此方法更改 status bar 的方向或樣式,以改變 status bar 即將在 view 呈現的樣子。 - viewDidAppear: 在 viewWillAppear 結束後,即 view 被載入後到 view controller 後,就會載入這個方法。同樣地,我們可以透過重寫來增加任務到已經呈現的 view。 - viewWillDisappear: 這個方法是當 view 將被移除,且動畫開始執行前呼叫的。我們可以重寫這個方法來改變 view 的各種狀態。如我們可以將被改變的 status bar 還原回來。 - viewDidDisappear: 當 view 被移除後,我們可以使用這個方法來後這時刻的自定義任務。 --- ## MVC 架構 MVC分別是**Model(模型)、View(介面)與Controller(控制)**。從字面上來看就能知道,MVC把程式碼控制分為了三類: * View:管理作為位圖展示到屏幕上的圖形和文字輸出 * Controller:翻譯用戶的輸入並依照用戶的輸入操作模型和視圖 * Model:管理應用的行為和數據,響應數據請求(經常來自視圖)和更新狀態的指令(經常來自控製器 ![](https://i.imgur.com/On02L9W.png) ![](https://i.imgur.com/bkVY7S0.png) 動態網站是透過 Controller 請求 Model 的資料並傳給 View 來顯示。當使用者點擊連結或其它與網頁互動的操作時,都是透過 Controller 來負責給予相對的對應。當使用者有搜尋需求,輸入了某個關鍵字並按下搜尋按鈕後,負責接受這個動作的 Controller 就會讓 View 顯示搜尋的頁面,並向 Model 發出請求將使用者搜尋的相關資料給 View。修改個人資訊也是相同方式。 使用者與網頁的操作互動,全是依賴 Controller 來對 View 與 Model 發送或請求資訊。所以基本上 Controller 就是 View 與 Model 之間的協調者。也就是說 MVC 三者彼此都是獨立且有各自的職責,當某個部分需要修改時,都保證其它兩個不用去做更動與調整。以生活實際的例子來解讀 MVC 會更好理解。 :::warning 以生活情境來看MVC: 回想一下到 ATM 提款的印象。在 ATM 上所看到的畫面就是 View ,當插入卡片、輸入密碼、選擇金額這些操作都屬於 Controller ,它會負責將獲得的資訊去比對 Model 是否有資料,並請求 Model 進行數據變更(金額變更)。最後是否要顯示剩餘金額也是由 Controller 來負責去請求 Model,再給提供給 View 來顯示。 ::: ## 實作 ### Alert ```m UIAlertController *alertController1 = [UIAlertController alertControllerWithTitle:@"大標題" message:@"小標題" preferredStyle:UIAlertControllerStyleAlert]; ``` ``` [self presentViewController:alertController animated:YES completion:nil]; ``` ### addAction ``` [alertController addAction: ShowButton]; ``` ![](https://i.imgur.com/jSNRTOj.jpg)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully