diff --git "a/Icon\r" "b/Icon\r" new file mode 100644 index 0000000..e69de29 diff --git a/iOS_Study_B.xcodeproj/project.pbxproj b/iOS_Study_B.xcodeproj/project.pbxproj index 9763af9..f57310a 100644 --- a/iOS_Study_B.xcodeproj/project.pbxproj +++ b/iOS_Study_B.xcodeproj/project.pbxproj @@ -10,9 +10,13 @@ 954B6B812AC57DAA00FDCFAB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954B6B802AC57DAA00FDCFAB /* AppDelegate.swift */; }; 954B6B832AC57DAA00FDCFAB /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954B6B822AC57DAA00FDCFAB /* SceneDelegate.swift */; }; 954B6B852AC57DAA00FDCFAB /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954B6B842AC57DAA00FDCFAB /* ViewController.swift */; }; - 954B6B882AC57DAA00FDCFAB /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 954B6B862AC57DAA00FDCFAB /* Main.storyboard */; }; 954B6B8A2AC57DAB00FDCFAB /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 954B6B892AC57DAB00FDCFAB /* Assets.xcassets */; }; 954B6B8D2AC57DAB00FDCFAB /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 954B6B8B2AC57DAB00FDCFAB /* LaunchScreen.storyboard */; }; + CC3EE7BB2B073A56009C5287 /* MemoDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3EE7BA2B073A56009C5287 /* MemoDetailViewController.swift */; }; + CC3EE7BD2B073DF9009C5287 /* MemoEditViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3EE7BC2B073DF9009C5287 /* MemoEditViewController.swift */; }; + CC72E1FD2B16185400353D28 /* SnapKit in Frameworks */ = {isa = PBXBuildFile; productRef = CC72E1FC2B16185400353D28 /* SnapKit */; }; + CC7D36DE2B04E1380069F109 /* MemoTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC7D36DD2B04E1380069F109 /* MemoTableViewCell.swift */; }; + CC7D36E42B05A0D60069F109 /* MemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC7D36E32B05A0D60069F109 /* MemoViewController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -20,10 +24,13 @@ 954B6B802AC57DAA00FDCFAB /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 954B6B822AC57DAA00FDCFAB /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; 954B6B842AC57DAA00FDCFAB /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; - 954B6B872AC57DAA00FDCFAB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 954B6B892AC57DAB00FDCFAB /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 954B6B8C2AC57DAB00FDCFAB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 954B6B8E2AC57DAB00FDCFAB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + CC3EE7BA2B073A56009C5287 /* MemoDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoDetailViewController.swift; sourceTree = ""; }; + CC3EE7BC2B073DF9009C5287 /* MemoEditViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoEditViewController.swift; sourceTree = ""; }; + CC7D36DD2B04E1380069F109 /* MemoTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoTableViewCell.swift; sourceTree = ""; }; + CC7D36E32B05A0D60069F109 /* MemoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -31,6 +38,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + CC72E1FD2B16185400353D28 /* SnapKit in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -56,13 +64,16 @@ 954B6B7F2AC57DAA00FDCFAB /* iOS_Study_B */ = { isa = PBXGroup; children = ( + CC7D36DD2B04E1380069F109 /* MemoTableViewCell.swift */, 954B6B802AC57DAA00FDCFAB /* AppDelegate.swift */, 954B6B822AC57DAA00FDCFAB /* SceneDelegate.swift */, 954B6B842AC57DAA00FDCFAB /* ViewController.swift */, - 954B6B862AC57DAA00FDCFAB /* Main.storyboard */, + CC7D36E32B05A0D60069F109 /* MemoViewController.swift */, 954B6B892AC57DAB00FDCFAB /* Assets.xcassets */, 954B6B8B2AC57DAB00FDCFAB /* LaunchScreen.storyboard */, 954B6B8E2AC57DAB00FDCFAB /* Info.plist */, + CC3EE7BA2B073A56009C5287 /* MemoDetailViewController.swift */, + CC3EE7BC2B073DF9009C5287 /* MemoEditViewController.swift */, ); path = iOS_Study_B; sourceTree = ""; @@ -83,6 +94,9 @@ dependencies = ( ); name = iOS_Study_B; + packageProductDependencies = ( + CC72E1FC2B16185400353D28 /* SnapKit */, + ); productName = iOS_Study_B; productReference = 954B6B7D2AC57DAA00FDCFAB /* iOS_Study_B.app */; productType = "com.apple.product-type.application"; @@ -111,6 +125,9 @@ Base, ); mainGroup = 954B6B742AC57DAA00FDCFAB; + packageReferences = ( + CC72E1FB2B16185400353D28 /* XCRemoteSwiftPackageReference "SnapKit" */, + ); productRefGroup = 954B6B7E2AC57DAA00FDCFAB /* Products */; projectDirPath = ""; projectRoot = ""; @@ -127,7 +144,6 @@ files = ( 954B6B8D2AC57DAB00FDCFAB /* LaunchScreen.storyboard in Resources */, 954B6B8A2AC57DAB00FDCFAB /* Assets.xcassets in Resources */, - 954B6B882AC57DAA00FDCFAB /* Main.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -139,22 +155,18 @@ buildActionMask = 2147483647; files = ( 954B6B852AC57DAA00FDCFAB /* ViewController.swift in Sources */, + CC3EE7BB2B073A56009C5287 /* MemoDetailViewController.swift in Sources */, 954B6B812AC57DAA00FDCFAB /* AppDelegate.swift in Sources */, + CC7D36E42B05A0D60069F109 /* MemoViewController.swift in Sources */, 954B6B832AC57DAA00FDCFAB /* SceneDelegate.swift in Sources */, + CC7D36DE2B04E1380069F109 /* MemoTableViewCell.swift in Sources */, + CC3EE7BD2B073DF9009C5287 /* MemoEditViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXSourcesBuildPhase section */ /* Begin PBXVariantGroup section */ - 954B6B862AC57DAA00FDCFAB /* Main.storyboard */ = { - isa = PBXVariantGroup; - children = ( - 954B6B872AC57DAA00FDCFAB /* Base */, - ); - name = Main.storyboard; - sourceTree = ""; - }; 954B6B8B2AC57DAB00FDCFAB /* LaunchScreen.storyboard */ = { isa = PBXVariantGroup; children = ( @@ -291,7 +303,6 @@ INFOPLIST_FILE = iOS_Study_B/Info.plist; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; - INFOPLIST_KEY_UIMainStoryboardFile = Main; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; LD_RUNPATH_SEARCH_PATHS = ( @@ -318,7 +329,6 @@ INFOPLIST_FILE = iOS_Study_B/Info.plist; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; - INFOPLIST_KEY_UIMainStoryboardFile = Main; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; LD_RUNPATH_SEARCH_PATHS = ( @@ -356,6 +366,25 @@ defaultConfigurationName = Release; }; /* End XCConfigurationList section */ + +/* Begin XCRemoteSwiftPackageReference section */ + CC72E1FB2B16185400353D28 /* XCRemoteSwiftPackageReference "SnapKit" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/SnapKit/SnapKit.git"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 5.6.0; + }; + }; +/* End XCRemoteSwiftPackageReference section */ + +/* Begin XCSwiftPackageProductDependency section */ + CC72E1FC2B16185400353D28 /* SnapKit */ = { + isa = XCSwiftPackageProductDependency; + package = CC72E1FB2B16185400353D28 /* XCRemoteSwiftPackageReference "SnapKit" */; + productName = SnapKit; + }; +/* End XCSwiftPackageProductDependency section */ }; rootObject = 954B6B752AC57DAA00FDCFAB /* Project object */; } diff --git a/iOS_Study_B.xcodeproj/xcuserdata/jiyoon.xcuserdatad/xcschemes/xcschememanagement.plist b/iOS_Study_B.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings similarity index 50% rename from iOS_Study_B.xcodeproj/xcuserdata/jiyoon.xcuserdatad/xcschemes/xcschememanagement.plist rename to iOS_Study_B.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings index 3e5066d..0c67376 100644 --- a/iOS_Study_B.xcodeproj/xcuserdata/jiyoon.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/iOS_Study_B.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -1,14 +1,5 @@ - - SchemeUserState - - iOS_Study_B.xcscheme_^#shared#^_ - - orderHint - 0 - - - + diff --git a/iOS_Study_B.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/iOS_Study_B.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved new file mode 100644 index 0000000..009c162 --- /dev/null +++ b/iOS_Study_B.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -0,0 +1,14 @@ +{ + "pins" : [ + { + "identity" : "snapkit", + "kind" : "remoteSourceControl", + "location" : "https://github.com/SnapKit/SnapKit.git", + "state" : { + "revision" : "f222cbdf325885926566172f6f5f06af95473158", + "version" : "5.6.0" + } + } + ], + "version" : 2 +} diff --git a/iOS_Study_B/Base.lproj/Main.storyboard b/iOS_Study_B/Base.lproj/Main.storyboard deleted file mode 100644 index 25a7638..0000000 --- a/iOS_Study_B/Base.lproj/Main.storyboard +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/iOS_Study_B/Info.plist b/iOS_Study_B/Info.plist index dd3c9af..0eb786d 100644 --- a/iOS_Study_B/Info.plist +++ b/iOS_Study_B/Info.plist @@ -15,8 +15,6 @@ Default Configuration UISceneDelegateClassName $(PRODUCT_MODULE_NAME).SceneDelegate - UISceneStoryboardFile - Main diff --git a/iOS_Study_B/MemoDetailViewController.swift b/iOS_Study_B/MemoDetailViewController.swift new file mode 100644 index 0000000..34ae0ca --- /dev/null +++ b/iOS_Study_B/MemoDetailViewController.swift @@ -0,0 +1,119 @@ +// +// MemoDetailViewController.swift +// iOS_Study_B +// +// Created by 임아영 on 11/17/23. +// + + +import UIKit +import SnapKit + +class MemoDetailViewController: UIViewController { + + var memo: Memo? + var index: Int? + weak var delegate: MemoEditDelegate? + + let Label1: UILabel = { + let label = UILabel() + label.font = UIFont.boldSystemFont(ofSize: 18) + return label + }() + + let Label2: UILabel = { + let label = UILabel() + label.font = UIFont.systemFont(ofSize: 16) + label.numberOfLines = 0 + return label + }() + + override func viewDidLoad() { + super.viewDidLoad() + view.backgroundColor = .white + + navigationItem.title = "메모 상세" + + configureSubviews() + makeConstraints() + setNavigationBar() + + if let memo = memo { + displayMemo(memo) + } + } + + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + + if isMovingFromParent { + backButtonTapped() + } + } + + func configureSubviews() { + view.addSubview(Label1) + view.addSubview(Label2) + + } + + func makeConstraints() { + Label1.snp.makeConstraints { make in + make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(20) + make.leading.equalTo(view.safeAreaLayoutGuide.snp.leading).offset(20) + make.trailing.equalTo(view.safeAreaLayoutGuide.snp.trailing).offset(-20) + } + Label2.snp.makeConstraints { make in + make.top.equalTo(Label1.snp.bottom).offset(20) + make.leading.equalTo(view.safeAreaLayoutGuide.snp.leading).offset(20) + make.trailing.equalTo(view.safeAreaLayoutGuide.snp.trailing).offset(-20) + } + } + + func setNavigationBar() { + let editButton = UIBarButtonItem(title: "수정하기", style: .plain, target: self, action: #selector(editButtonTapped)) + navigationItem.rightBarButtonItem = editButton + } + + @objc func editButtonTapped() { + let MemoEditViewController = MemoEditViewController() + + MemoEditViewController.memo = memo + MemoEditViewController.index = index + MemoEditViewController.delegate = self + self.navigationController?.pushViewController(MemoEditViewController, animated: true) + } + + @objc func backButtonTapped() { + guard let updatedMemo = createUpdatedMemo() else { + return + } + + delegate?.didUpdateMemo(updatedMemo, atIndex: index!) + self.navigationController?.popViewController(animated: true) + } + + func createUpdatedMemo() -> Memo? { + guard let updatedLabel1 = Label1.text, + let updatedLabel2 = Label2.text else { + return nil + } + + return Memo(id: memo?.id, title: updatedLabel1, content: updatedLabel2) + } + + func displayMemo(_ memo: Memo) { + Label1.text = memo.title + Label2.text = memo.content + } + } + +protocol MemoEditDelegate: AnyObject { + func didUpdateMemo(_ memo: Memo, atIndex: Int) +} + +extension MemoDetailViewController: MemoEditDelegate { + func didUpdateMemo(_ memo: Memo, atIndex: Int) { + displayMemo(memo) + } +} diff --git a/iOS_Study_B/MemoEditViewController.swift b/iOS_Study_B/MemoEditViewController.swift new file mode 100644 index 0000000..f0bcfa4 --- /dev/null +++ b/iOS_Study_B/MemoEditViewController.swift @@ -0,0 +1,100 @@ +// +// MemoEditViewController.swift +// iOS_Study_B +// +// Created by 임아영 on 11/17/23. +// + + +import UIKit +import SnapKit + +class MemoEditViewController: UIViewController { + + var memo: Memo? + var index: Int? + weak var delegate: MemoEditDelegate? + + let textfield: UITextField = { + let textfield = UITextField() + textfield.font = UIFont.systemFont(ofSize: 20) + textfield.keyboardType = .default + textfield.layer.cornerRadius = 5.0 + textfield.layer.borderColor = UIColor.black.cgColor + textfield.layer.borderWidth = 1.0 + + return textfield + }() + + let textfield2: UITextField = { + let textfield2 = UITextField() + textfield2.font = UIFont.systemFont(ofSize: 20) + textfield2.keyboardType = .default + textfield2.layer.cornerRadius = 5.0 + textfield2.layer.borderColor = UIColor.black.cgColor + textfield2.layer.borderWidth = 1.0 + + return textfield2 + }() + + override func viewDidLoad() { + super.viewDidLoad() + view.backgroundColor = .white + navigationItem.title = "메모 수정" + + configureSubviews() + makeConstraints() + setNavigationBar() + + if let memo = memo { + displayMemo(memo) + } + } + + func configureSubviews() { + view.addSubview(textfield) + view.addSubview(textfield2) + } + + func makeConstraints() { + textfield.snp.makeConstraints { make in + make.height.equalTo(40.0) + make.top.equalTo(view.safeAreaLayoutGuide).inset(10) + make.leading.trailing.equalTo(view.safeAreaLayoutGuide).inset(18) + } + textfield2.snp.makeConstraints { make in + make.height.equalTo(40.0) + make.top.equalTo(view.safeAreaLayoutGuide).inset(60) + make.leading.trailing.equalTo(view.safeAreaLayoutGuide).inset(18) + } + } + + func setNavigationBar() { + let setButton = UIBarButtonItem(title: "완료", style: .plain, target: self, action: #selector(setButtonTapped)) + navigationItem.rightBarButtonItem = setButton + } + + @objc func setButtonTapped() { + guard let updatedMemo = createUpdatedMemo() else { + return + } + delegate?.didUpdateMemo(updatedMemo, atIndex: index!) + self.navigationController?.popViewController(animated: true) + } + + func displayMemo(_ memo: Memo) { + textfield.text = memo.title + textfield2.text = memo.content + } + + func createUpdatedMemo() -> Memo? { + guard let updatedLabel1 = textfield.text, + let updatedLabel2 = textfield2.text else { + return nil + } + + return Memo(id: memo?.id, title: updatedLabel1, content: updatedLabel2) + } + +} + diff --git a/iOS_Study_B/MemoTableViewCell.swift b/iOS_Study_B/MemoTableViewCell.swift new file mode 100644 index 0000000..ec2af27 --- /dev/null +++ b/iOS_Study_B/MemoTableViewCell.swift @@ -0,0 +1,64 @@ +// +// MemoTableViewCell.swift +// iOS_Study_B +// +// Created by 임아영 on 11/15/23. +// + +import UIKit +import SnapKit + +class MemoTableViewCell : UITableViewCell { + static let identifier = "MemoTableViewCell" + + let titleLabel: UILabel = { + let label = UILabel() + label.font = UIFont.boldSystemFont(ofSize: 23) + + return label + }() + + let contentLabel: UILabel = { + let label = UILabel() + label.font = UIFont.systemFont(ofSize: 16) + label.numberOfLines = 1 + + return label + + }() + + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { + super.init(style: style, reuseIdentifier: reuseIdentifier) + configureSubviews() + makeConstraints() + } + + override func awakeFromNib() { + super.awakeFromNib() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func configureSubviews() { + contentView.addSubview(titleLabel) + contentView.addSubview(contentLabel) + } + + func makeConstraints() { + titleLabel.snp.makeConstraints { make in + make.top.equalTo(contentView).inset(10) + make.leading.equalTo(contentView).inset(15) + } + contentLabel.snp.makeConstraints { make in + make.top.equalTo(titleLabel.snp.bottom).offset(10) + make.leading.equalTo(contentView).inset(15) + } + } + + func configure(with memo : Memo) { + titleLabel.text = memo.title + contentLabel.text = memo.content + } + } diff --git a/iOS_Study_B/MemoViewController.swift b/iOS_Study_B/MemoViewController.swift new file mode 100644 index 0000000..a4e8366 --- /dev/null +++ b/iOS_Study_B/MemoViewController.swift @@ -0,0 +1,97 @@ +// +// MemoViewController.swift +// iOS_Study_B +// +// Created by 임아영 on 11/16/23. +// + +import UIKit +import SnapKit + +struct Memo : Identifiable { + var id: ObjectIdentifier? + var title: String? + var content: String? +} + + +class MemoViewController: UIViewController { + + let textfield: UITextField = { + let textField = UITextField() + textField.placeholder = "제목 입력" + textField.font = UIFont.systemFont(ofSize: 20) + textField.keyboardType = .default + textField.layer.cornerRadius = 5.0 + textField.layer.borderColor = UIColor.black.cgColor + textField.layer.borderWidth = 1.0 + + return textField + }() + + let textfield2: UITextField = { + let textField = UITextField() + textField.placeholder = "내용 입력" + textField.font = UIFont.systemFont(ofSize: 20) + textField.keyboardType = .default + textField.layer.cornerRadius = 5.0 + textField.layer.borderColor = UIColor.black.cgColor + textField.layer.borderWidth = 1.0 + + return textField + }() + + // 초기 메모 내용을 설정하기 위한 변수 + var initialMemoContent: String? + var memoIndex: Int? + + override func viewDidLoad() { + super.viewDidLoad() + view.backgroundColor = .white + navigationItem.title = "메모 작성" + + configureSubviews() + makeConstraints() + setNavigationBar() + + } + + func configureSubviews() { + view.addSubview(textfield) + view.addSubview(textfield2) + } + + func makeConstraints() { + textfield.snp.makeConstraints { make in + make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(20) + make.height.equalTo(40.0) + make.leading.equalTo(view.safeAreaLayoutGuide.snp.leading).offset(20) + make.trailing.equalTo(view.safeAreaLayoutGuide.snp.trailing).offset(-20) + } + + textfield2.snp.makeConstraints { make in + make.top.equalTo(textfield.snp.bottom).offset(20) + make.height.equalTo(40.0) + make.leading.equalTo(view.safeAreaLayoutGuide.snp.leading).offset(20) + make.trailing.equalTo(view.safeAreaLayoutGuide.snp.trailing).offset(-20) + } + } + + func setNavigationBar() { + let addButton = UIBarButtonItem(title: "작성하기", style: .plain, target: self, action: #selector(addButtonTapped)) + navigationItem.rightBarButtonItem = addButton + } + + @objc func addButtonTapped() { + guard let title = textfield.text, let content = textfield2.text else { + return + } + let newMemo = Memo(id: ObjectIdentifier(self), title: title, content: content) + + if let viewController = navigationController?.viewControllers.first as? ViewController { + viewController.addMemo(newMemo) + } + + self.navigationController?.popViewController(animated: true) + } + } diff --git a/iOS_Study_B/SceneDelegate.swift b/iOS_Study_B/SceneDelegate.swift index 9987c27..6b30d23 100644 --- a/iOS_Study_B/SceneDelegate.swift +++ b/iOS_Study_B/SceneDelegate.swift @@ -16,7 +16,14 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). - guard let _ = (scene as? UIWindowScene) else { return } + guard let windowScene = (scene as? UIWindowScene) else { return } + window = UIWindow(windowScene: windowScene) + + let mainViewController = ViewController() + let navigationController = UINavigationController(rootViewController: mainViewController) + + window?.rootViewController = navigationController + window?.makeKeyAndVisible() } func sceneDidDisconnect(_ scene: UIScene) { diff --git a/iOS_Study_B/ViewController.swift b/iOS_Study_B/ViewController.swift index f3577b5..4a38ed2 100644 --- a/iOS_Study_B/ViewController.swift +++ b/iOS_Study_B/ViewController.swift @@ -4,16 +4,105 @@ // // Created by 박지윤 on 2023/09/28. // - +import SnapKit import UIKit class ViewController: UIViewController { - + + var memos = [Memo]() + + let tableview:UITableView = { + let table = UITableView() + table.register(MemoTableViewCell.self, forCellReuseIdentifier: MemoTableViewCell.identifier) + table.backgroundColor = .white + + return table + }() + override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view. + configureSubviews() + makeConstraints() + setNavigationBar() + + tableview.dataSource = self + tableview.delegate = self + + view.backgroundColor = .white + } + + func setNavigationBar() { + let addButton = UIBarButtonItem(image: UIImage(systemName: "plus"), style: .plain, target: self, action: #selector(addButtonTapped)) + navigationItem.title = "메모장" + navigationItem.rightBarButtonItem = addButton + } + + func configureSubviews() { + view.addSubview(tableview) + } + + func makeConstraints() { + tableview.snp.makeConstraints { make in + make.edges.equalTo(view.safeAreaLayoutGuide) + } } + + @objc func addButtonTapped() { + let addMemoViewController = MemoViewController() + self.navigationController?.pushViewController(addMemoViewController, animated: true) + } + + func addMemo(_ memo: Memo) { + memos.append(memo) + tableview.reloadData() + } + + func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { + let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (_, _, completionHandler) in + + self.memos.remove(at: indexPath.row) + tableView.deleteRows(at: [indexPath], with: .fade) + completionHandler(true) + } + + return UISwipeActionsConfiguration(actions: [deleteAction]) + } + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + let detailMemoViewController = MemoDetailViewController() + detailMemoViewController.memo = memos[indexPath.row] + detailMemoViewController.index = indexPath.row + detailMemoViewController.delegate = self + + self.navigationController?.pushViewController(detailMemoViewController, animated: true) + } + +} + +extension ViewController : UITableViewDataSource, UITableViewDelegate{ + + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return memos.count + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + guard let cell = tableView.dequeueReusableCell(withIdentifier: MemoTableViewCell.identifier, for: indexPath) as? MemoTableViewCell else { return UITableViewCell() } + + let memo = memos[indexPath.row] + cell.configure(with: memo) + + return cell + } + + func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { + return 70 + } } +extension ViewController: MemoEditDelegate { + func didUpdateMemo(_ memo: Memo, atIndex index: Int) { + memos[index] = memo + tableview.reloadData() + } +}