250x250
반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- swiftlint
- algorithm
- xcode
- designPattern
- Equatable
- reetcode
- protocol
- GIT
- COMMIT
- tip
- ToDoRim
- it
- Extentsion
- keyWindow
- github
- toyproject
- SwiftGen
- Swift
- IOS
- OSLog
- flutter
- Leetcode
- pubspec.yaml
- enumerations
- listview
- dart
- Widget
- pubspec
- basic
- UIAccessibility
Archives
- Today
- Total
수니의 개발새발
[iOS/Swift] Storyboard, ViewController 접근 코드를 Protocol로 정의 (StoryboardInstantiable) 본문
iOS - Swift
[iOS/Swift] Storyboard, ViewController 접근 코드를 Protocol로 정의 (StoryboardInstantiable)
개발자 수니 2022. 2. 9. 15:22728x90
반응형
📌 이번 글은
Storyboard를 접근 코드를 Protocol로 정의하여,
필요한 곳에 사용하는 예제입니다.
코드에서 Storyboard, Xib를 접근하는 방법에 대한 포스팅입니다.
[iOS/Swift] 코드에서 Storyboard, ViewController 접근 (instantiateViewController)
📌 이번 글은 코드에서 Storyboard와 ViewController를 접근하는 방법입니다. 👩🏻💻 예제 storyboardName : Storyboard 파일의 이름 storyboardId : Storayboard > ViewController의 id 코드에서 ViewContr..
sunidev.tistory.com
StoryboardInstantiable Protocol 정의
public protocol StoryboardInstantiable: NSObjectProtocol {
associatedtype T
static var defaultFileName: String { get }
static func instantiateViewController(_ bundle: Bundle?) -> T
}
StoryboardInstantiable 확장
extension StoryboardInstantiable where Self: UIViewController {
static var defaultFileName: String {
return NSStringFromClass(Self.self).components(separatedBy: ".").last!
}
static func instantiateViewController(_ bundle: Bundle? = nil) -> Self {
let fileName = defaultFileName
let storyboard = UIStoryboard(name: fileName, bundle: bundle)
guard let vc = storyboard.instantiateInitialViewController() as? Self else {
fatalError("Cannot instantiate initial view controller \(Self.self) from storyboard with name \(fileName)")
}
return vc
}
}
👩🏻💻 예제
Storyboard ViewController에 Storyboard ID 지정
ViewController Class에 StoryboardInstantiable 채택
import UIKit
class MainViewController: UIViewController, StoryboardInstantiable {
override func viewDidLoad() {
super.viewDidLoad()
}
}
코드에서 StoryboardInstantiable을 채택한 ViewController로 이동
let mainVC: MainViewController = MainViewController.instantiateViewController(Bundle.main)
self.navigationController?.pushViewController(mainVC, animated: true)
🙋🏻♀️ 참고
728x90
반응형
'iOS - Swift' 카테고리의 다른 글
[Xcode/Swift] 전처리기, 전처리문(#if DEBUG) (with. Active Compilation Conditions) (0) | 2022.04.01 |
---|---|
[iOS/Swift] SwiftLint 규칙 정리 (0) | 2022.03.27 |
[iOS/Swift] Color Set으로 Custom Color관리하기 (with. SwiftGen) (0) | 2022.02.08 |
[iOS/Swift] SwiftGen 적용하기 (pod 'SwiftGen') (0) | 2022.02.04 |
[iOS/Swift] typealias (0) | 2022.02.02 |
Comments