Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIPickerView #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions UIKitBoostCourse.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
1B7E2D3D2AD296B200E954E5 /* UIPickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B7E2D3C2AD296B200E954E5 /* UIPickerView.swift */; };
1C5004332AAAB4B300DBE27E /* UIColorWell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C5004322AAAB4B300DBE27E /* UIColorWell.swift */; };
1C5BB6DF2AB8889D0033B6C8 /* UISegmentedControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C5BB6DE2AB8889D0033B6C8 /* UISegmentedControl.swift */; };
1C5E82632ACD0E94007B3F40 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 1C5E82622ACD0E94007B3F40 /* README.md */; };
Expand All @@ -27,6 +28,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
1B7E2D3C2AD296B200E954E5 /* UIPickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIPickerView.swift; sourceTree = "<group>"; };
1C5004322AAAB4B300DBE27E /* UIColorWell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIColorWell.swift; sourceTree = "<group>"; };
1C5BB6DE2AB8889D0033B6C8 /* UISegmentedControl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UISegmentedControl.swift; sourceTree = "<group>"; };
1C5E82622ACD0E94007B3F40 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
Expand Down Expand Up @@ -138,6 +140,7 @@
1C5004322AAAB4B300DBE27E /* UIColorWell.swift */,
D87BE9432AC8506F003CD151 /* UISearchField.swift */,
1C7E9A032AC06BDA00C808FE /* UIStepper.swift */,
1B7E2D3C2AD296B200E954E5 /* UIPickerView.swift */,
);
path = UIControl;
sourceTree = "<group>";
Expand Down Expand Up @@ -249,6 +252,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1B7E2D3D2AD296B200E954E5 /* UIPickerView.swift in Sources */,
1CCBC1172AC9671D00B261CF /* UIStackView+AddArrangedSubviews.swift in Sources */,
1C98C6412AA48D9D0029AAAA /* AppDelegate.swift in Sources */,
1C5004332AAAB4B300DBE27E /* UIColorWell.swift in Sources */,
Expand Down
106 changes: 106 additions & 0 deletions UIKitBoostCourse/Source/UIControl/UIPickerView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//
// UIPickerView.swift
// UIKitBoostCourse
//
// Created by t2023-m0080 on 2023/10/08.
//

import UIKit
import SwiftUI

class UIPickerViewController : BaseUIViewController {

// 첫 번째 컴포넌트에 표시될 데이터
let data1 = ["1번 항목", "2번 항목", "3번 항목"]

// 두 번째 컴포넌트에 표시될 데이터
let data2 = ["string형식은", "다 가능✅", "합니다", "짠" ]


// UIPickerView 인스턴스 생성 및 설정
lazy var picker: UIPickerView = {
let p = UIPickerView()
p.dataSource = self // 데이터 소스 지정
p.delegate = self // 델리게이트 지정
p.translatesAutoresizingMaskIntoConstraints = false
return p
}()


override func viewDidLoad() {
super.viewDidLoad()

setUI()
}


override func setUI() {
self.view.backgroundColor = .white
self.view.addSubview(picker)

NSLayoutConstraint.activate([
picker.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
picker.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
picker.widthAnchor.constraint(equalToConstant: 300),
picker.heightAnchor.constraint(equalToConstant: 200)
])
}
}


// UIPickerViewDataSource extension
extension UIPickerViewController: UIPickerViewDataSource {
// 컴포넌트(열)의 수 반환
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}

// 각 컴포넌트에서 표시될 행의 수 반환
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch component {
case 0:
return data1.count
case 1:
return data2.count
default:
return 0
}
}
}


// UIPickerViewDelegate extension
extension UIPickerViewController: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch component {
// 각 행에 표시될 문자열 반환
case 0:
return data1[row]
case 1:
return data2[row]
default:
return nil
}
}
}


// 프리뷰
private struct UIViewControllerRepresenter: UIViewControllerRepresentable {

let viewController: UIViewController

func makeUIViewController(context: Context) -> UIViewController {
return viewController
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

struct UIViewControllerPreview4: PreviewProvider {

static var previews: some View {
let viewController = UIPickerViewController()
return UIViewControllerRepresenter(viewController: viewController)
}
}