[UIKit] Enum with Reusable VC

2023. 10. 10. 22:22ยท๐ŸŽ Dev/๊ตฌํ˜„

โš™๏ธ Setting

- iOS 16 ↑
- Swift vesrion 5.9
- Xcode version 15
 

๐Ÿ”– Background Info

- During the development of my first app, Zickwan, I encountered a challenge: the UI design and features of the writing and editing view controllers were identical. Creating two separate view controllers wasn't ideal for efficient memory management.

 

 

โœ๏ธ Steps

- To achieve reusable views, enums proved to be a valuable tool. Here are the steps to make views reusable using enums.

 

 

1. Define TransitionType with an enum: Enum cases represent different roles when switching between views. In my case, I established roles for adding and editing.

 

enum TransitionType: String {
    case add
    case edit
}

 

 

2. Declare the type in the initial ViewController: The initial view controller, by default, will be for adding. This is achieved by declaring the type as .add.

 

var type : TransitionType = .add

 

 

3. Utilize a switch statement to separate functions: The switchingData() function uses the type enum to execute specific actions based on whether it's an add or edit scenario.

 

func switchingData() {
        
        switch type {
        case .add:
            
            print("add")
            
        case .edit:
            print("edit")
        }
        
    }

 

 

 

โœ… Enum

- Enumerations (Enum)?

 

Enumerations (Enums) are a powerful feature in Swift, defining a type for a group of related values. They offer several benefits:

 

1. Compile-time Error Recognition:

Errors are identified at compile time, enhancing the robustness of the code.

 

2. Reduction of Human Errors:

Enums help minimize mistakes by providing a clear set of options.

 

3. Narrowing Down Value Selections:

By defining specific values, enums guide and limit the selection of options.

 

4. Forcing Selections Among Limited Values:

Enums encourage developers to choose from a predefined set of values, contributing to code consistency.

 

 

 

 

 

 


 

์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'๐ŸŽ Dev > ๊ตฌํ˜„' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[SwiftUI] pagerView ๋งŒ๋“ค๊ธฐ (iOS ๋ฒ„์ „๋Œ€์‘)  (2) 2024.08.14
[SwiftUI] CustomPopUpView ์• ๋‹ˆ๋ฉ”์ด์…˜ ํšจ๊ณผ ํ•ด๊ฒฐํ•˜๊ธฐ  (0) 2024.06.27
[SwiftUI] ์Šค์œ ๋กœ ์„น์…˜ ์ ‘์—ˆ๋‹คํˆ๋‹ค ๊ตฌํ˜„ํ•˜๊ธฐ  (0) 2024.06.21
[SwiftUI] pre/next buttons๊ฐ€ ์žˆ๋Š” ์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋” ๊ตฌํ˜„ํ•˜๊ธฐ  (2) 2024.05.16
[SwiftUI] TabView page indicator ์ปค์Šคํ…€ํ•˜๊ธฐ  (1) 2024.04.25
'๐ŸŽ Dev/๊ตฌํ˜„' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [SwiftUI] CustomPopUpView ์• ๋‹ˆ๋ฉ”์ด์…˜ ํšจ๊ณผ ํ•ด๊ฒฐํ•˜๊ธฐ
  • [SwiftUI] ์Šค์œ ๋กœ ์„น์…˜ ์ ‘์—ˆ๋‹คํˆ๋‹ค ๊ตฌํ˜„ํ•˜๊ธฐ
  • [SwiftUI] pre/next buttons๊ฐ€ ์žˆ๋Š” ์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋” ๊ตฌํ˜„ํ•˜๊ธฐ
  • [SwiftUI] TabView page indicator ์ปค์Šคํ…€ํ•˜๊ธฐ
Callie_
Callie_
  • Callie_
    CalliOS
    Callie_
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ
      • ๐ŸŽ APPLE
      • ๐ŸŽ Dev
        • Swift
        • UIKit
        • SwiftUI
        • Issue
        • ๊ตฌํ˜„
      • ๐ŸŽ Design
        • HIG
      • โš™๏ธ CS
      • ๐Ÿ’ก ์•Œ๊ณ ๋ฆฌ์ฆ˜
        • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
        • ๋ฐฑ์ค€
      • ๐ŸŸ๏ธ ์ง๊ด€๋กœ๊ทธ (์ถœ์‹œ์•ฑ)
        • ์—…๋ฐ์ดํŠธ
      • ๐ŸŒฑ SeSAC iOS 3๊ธฐ
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    addTarget
    ios
    clipsToBound
    Enum
    ๋„คํŠธ์›Œํฌํ†ต์‹ 
    Infoํƒญ
    SwiftUI
    DiffableDataSource
    CustomView
    cornerradius
    assets
    diffable
    TableViewCell
    TapGestureRecognizer
    ์ƒ๋ช…์ฃผ๊ธฐ
    SeSAC
    .fullScreen
    Snapshot
    apply
    layer.shadow
    ํ™”๋ฉด์ „ํ™˜
    IBOutlet
    CocoaTouchFramework
    Entry Point
    .OverFullScreen
    modalPresentStyle
    DidEndOnExit
    stroyboard
    IBAction
    Swift
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
Callie_
[UIKit] Enum with Reusable VC
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”