Swift Learning Notes
      • 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
    • 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 No publishing access yet

      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.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      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
    • 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
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
  • 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    # Interview Questions - Life Cycle ### 1. 試說明 App Life Cycle - App回應系統通知、使用者互動、處理其他系統相關事件的不同狀態,像是unattached, foreground, background, suspended。app的狀態會決定可以做什麼跟不能做什麼 - iOS13之後,改成 scene-based app,用 `UISceneDelegate`來處理生命週期相關的事 - ![](https://i.imgur.com/7vaq1fG.png) - When the user or system requests a new scene for your app, UIKit creates it and puts it in the unattached state. - User-requested scenes move quickly to the foreground, where they appear onscreen. A system-requested scene typically moves to the background so that it can process an event. For example, the system might launch the scene in the background to process a location event. - When the user dismisses your app's UI, UIKit moves the associated scene to the background state and eventually to the suspended state. UIKit can disconnect a background or suspended scene at any time to reclaim its resources, returning that scene to the unattached state. - iOS12之前是用 ` UIApplicationDelegate`來處理生命週期相關的事 - (iOS12之前)AppDelegate 是一個 App 中的核心,它可處理初始化、各種生命週期狀態間的轉換、甚至是一些較為複雜的事件 - ![](https://i.imgur.com/rVp6pUJ.png) - After launch, the system puts the app in the inactive or background state, depending on whether the UI is about to appear onscreen. - When launching to the foreground, the system transitions the app to the active state automatically. - After that, the state fluctuates between active and background until the app terminates. [混淆系列:App 的生命週期「App Life Cycle」](https://medium.com/%E5%9C%A8%E7%A8%8B%E5%BC%8F%E8%88%87%E6%97%85%E8%A1%8C%E7%9A%84%E8%B7%AF%E4%B8%8A/%E6%B7%B7%E6%B7%86%E7%B3%BB%E5%88%97-app-%E7%9A%84%E7%94%9F%E5%91%BD%E9%80%B1%E6%9C%9F-app-life-cycle-6ef9c88e9737) ### 2. 試說明 View Controller Life Cycle。 - VC 是app的地基,負責user interfaces和 underlying data的溝通,View Controller Life Cycle就是指VC的生命週期,從產生到消失 - ![](https://i.imgur.com/VnShP0K.png) - 還沒輪到該VC時,都是處於 Not Loaded狀態,而一旦被呼叫時就會開始進入生命週期 - loadView() - 而當view被需要(被呼叫),但是他還是nil時,viewController就會呼叫loadView(),生成UIView然後assign到 - viewDidLoad() - Called after the controller’s view is loaded into memory. — by APPLE 在畫面已經載入到記憶體的時候就會呼叫這個方法,並呈現給使用者看 - viewWillAppear() - Called after the title bar accessory view controller’s view has been loaded into memory is about to be added to the view hierarchy in the window. — by APPLE 畫面載入記憶體且即將要把畫面加入到呼叫階層(hierarchy)中呈現給使用者時 - 當我們的畫面會一直更新、或是即將呈現給使用者時,就可以使用該方法 - 使用情境: - 網路連線如果需要一直更動的話,讓它在呈現給使用者前就會先載入好 - 一些動畫等。 - 在畫面即將被呈現在使用者前,可將一些元件隱藏、動作取消等。 - 其他的客製化任務 - viewDidAppear() - Called when the title bar accessory view controller’s view is fully transitioned onto the screen. — by APPLE - 當畫面剛呈現在螢幕上時,viewDidAppear 會被呼叫。這種方法很適合用在當畫面呈現後需要立刻做的事情 - 播放動畫、音樂、影片等。 - 獲取資料、儲存資料等 - viewWillDisappear() - Called when the view controller’s view is about to be removed from the view hierarchy in the window. — by APPLE當畫面即將在螢幕上被移除,但還保留在整個 View Controller 的呼叫階層(hierarchy)中,會呼叫該方法 - 可以做的事情: - 處理、控制 Timer。 - 隱藏、收回鍵盤。 - 終止網路的請求(request)。 - 有更改到的 UI 畫面回復至最初的基本 UI 畫面。如恢復顯示tab bar/naviBar - 也可以用於儲存 App狀態(state) - viewDidDisappear() - Called after the title bar accessory view controller’s view is removed from the window’s view hierarchy. — by APPLE當畫面從 view 的呼叫階層中移除後會呼叫該方法 - 可以做的事: - 完全停止接收通知(notification)。 - 裝置的感應偵測器(sensors)。 - 釋放資源。 - 其他需要被終止的活動。 - 其他較不常用的方法: - viewWillLayoutSubviews() - Called to notify the view controller that its view is about to layout its subviews. — by APPLE - 當我們的 bonds,畫面的邊界尺寸需要做更改時,在畫面還沒呈現出來前,可以呼叫此方法 - 可以用在: - 新增一個 subView。 - bond or frame 的變化。 - 屏幕水平放置 / 垂直時的更動。 - viewDidLayoutSubviews() - Called to notify the view controller that its view has just laid out its subviews. — by APPLE - 跟上面的方法相呼應。當畫面尺寸已經完成調整 / 變更會呼叫此方法。可以使用在: - 儘管畫面已經做好調整,但是你還是想要更動一些畫面尺寸時。(如旋轉屏幕) - 注意的點:它可以被多次呼叫,會在 Auto layout 完成後呼叫該方法。 - didReceiveMemoryWarning() - Sent to the view controller when the app receives a memory warning. — by APPLE - 我們的 App 不會主動呼叫這個方法,它是由系統發送給 View Controller 的。當你寫的 App 佔據大量的記憶體,造成效能不足時,系統會呼叫該方法。 - 假設當你的 App 滿載且負荷不了時,系統會自動關閉你的 App。這對於使用者來說,猶如閃退。但我們還是可以 Override 這個方法,讓 View Controller 去主動釋放一些不必要的記憶體,避免到時候系統直接關閉我們的 App。 [混淆系列:View Controller 的生命週期「View Controller Life Cycle」](https://medium.com/%E5%9C%A8%E7%A8%8B%E5%BC%8F%E8%88%87%E6%97%85%E8%A1%8C%E7%9A%84%E8%B7%AF%E4%B8%8A/%E6%B7%B7%E6%B7%86%E7%B3%BB%E5%88%97-view-controller-%E7%9A%84%E7%94%9F%E5%91%BD%E9%80%B1%E6%9C%9F-view-controller-life-cycle-179462e95894) ### 3. 試說明 viewDidLoad 與 viewDidAppear 有什麼不同? - viewDidLoad() - Called after the controller’s view is loaded into memory. — by APPLE 在畫面已經載入到記憶體的時候就會呼叫這個方法,並呈現給使用者看 - 什麼時候用到: - 一次性的連線網路資料。 - 準備畫面,如背景色、元件樣子、其他使用者介面。 - 其他資料只需要被載入一次 - 要注意的: - 系統只會在啟動時呼叫 ==一次== viewDidLoad - viewDidLoad 會比「bond(邊界)」以及「rotation(旋轉)」先被呼叫,如果有需要調整畫面尺寸等方法的時候,需要留意是否要放在 viewDidLoad 裡,以免造成錯誤、閃退等 bug - viewDidAppear() - Called when the title bar accessory view controller’s view is fully transitioned onto the screen. — by APPLE - 當畫面剛呈現在螢幕上時,viewDidAppear 會被呼叫。這種方法很適合用在當畫面呈現後需要立刻做的事情 ### 4. 如何決定程式碼要寫在哪個 View Life Cycle 裡? Call API 的 code 要寫在哪? 隱藏UINavigationBar 的 code 要寫在哪? - 如何決定程式碼要寫在哪個 View Life Cycle 裡? - 執行的次數,只需要一次的話 -> viewDidLoad() - 執行的時間點,例如出現時、消失時,出現在畫面呈現之前、之後 - Call API 的 code 要寫在哪? - 一次的話是viewDidLoad() - 會需要一直更新的話,可以寫在viewWillAppear() - 隱藏UINavigationBar 的 code 要寫在哪? - viewWillAppear(),在呈現畫面之前 - viewWillDispear(),退出畫面之前 ### 5. 什麼時候可以確定 Auto Layout System 已經計算完畢,並已經把結果套到 View 上? * view設定 autolayout會經過 3個階段update, layout and render :::spoiler * These steps do not occur in a one-way direction. It’s possible for a step to trigger any previous one or even force the whole cycle to repeat. - update - 這個階段會根據各個view的constraints計算位置跟大小(This step is all about calculating view frame based on its constrains. ) - 系統會以bottom-up的方式開始遍歷 view hierarchy,例如從subview 到 superview,以及為每個view呼叫 `updateConstraints()` - 這個過程是自動發生的,但是我們也可以透過其他方式觸發 - `setNeedsUpdateConstraints()` 會讓目前的constraints失效,然後再下一個cycle更新constraints - `updateConstraintsIfNeeded()` 在constraints已失效的狀態下,會立刻觸發`updateConstraints()` 更新constraints - layout - 這階段會根據在Update階段計算的結果更新畫面layout(During this step, frames of each view are updated with the rectangles calculated in the Update phase ) - 系統會以top-down的方式開始遍歷 view hierarchy,例如從superview 到 subview,以及為每個view呼叫 `layoutSubviews()` - `setNeedsLayout()`會讓目前的layout失效,然後再下一個cycle更新layout - `layoutIfNeeded()`會在layout失效的狀態下,立即觸發`layoutSubviews()` - (基本上這三個方法跟update的三個方法意思很像,彼此間的關係一樣) - render - This step is responsible for bringing pixels onto the screen. - This step is independent of whether Auto Layout is enabled for a view or not. Same as with the layout step, rendering happens top-down - key method `drawRect` - About UIVC - Methods from steps 1 and 2 have their counterparts for the view controllers: - Update phase: updateViewConstraints. - Layout phase: viewWillLayoutSubviews / viewDidLayoutSubviews. ::: 在`viewDidLayoutSubviews` 的時候,會通知VC說 view已經結束 layout階段,就是已經根據在Update階段計算的結果,並更新結果到view上了,那後續view就可以再準備呈現到螢幕畫面上了 (Method viewDidLayoutSubviews is the most important among all. It is called to notify view controller that its view has finished the Layout step, i.e. it’s bounds have changed. This is an opportunity to make changes to a view after it has laid out its subviews, but before it becomes visible on the screen.) ### 6. 試說明 loadView 這個 function 有什麼用途,可以直接使用這個 method 嗎? - What Do in View Load : **loadView( ) basically takes a view (that you create) and sets it to the viewController’s view (superview).** 當(VC的)view被需要(被呼叫),但是他還是nil時,viewController就會呼叫loadView(),生成UIView然後assign到 - 不管是通过xib文件还是通过重写loadView方法创建UIViewController的view,在view创建完成之后都会调用viewDidLoad方法,也可以理解为当UIViewController的view被加载到記憶體中时,viewDidLoad方法会被调用 --> 可以用來控制進入 viewDidLoad()的時機 - You should never call this method directly. The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property. - 蘋果文件:You should never call this method directly. The view controller calls this method when its [view](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621460-view) property is requested but is currently `nil`. This method loads or creates a view and assigns it to the [view](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621460-view) property. [iOS - loadView和viewDidLoad](https://blog.csdn.net/longshihua/article/details/79420686) ### 7. 當從 A Controller push B Controller,會經過哪些 view controller life cycle method? :crying_cat_face: 看A水的 ### 8. 當在不同的 tab 之間切換,會經過哪些 view controller life cycle method? :crying_cat_face: 看A水的 ### 9. 現在 A Controller 是畫面上的 view controller,其中有一個 Button,點擊之後會推出 B Controller。但是現在有個問題,點擊之後,經過了非常久的時間 B Controller 才出現在畫面上。如果要解決這個問題,你會怎麼開始? :crying_cat_face: [庭安筆記](https://crystal-zephyr-cb4.notion.site/Interview-Answers-Life-Cycle-e63629104e39495fafe8f4218f0f4464)

    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
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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