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

[숲] 6주차 실습 #20

Open
wants to merge 1 commit 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -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
}
111 changes: 111 additions & 0 deletions MemoClone/AddMemoViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//
// AddButtonViewController.swift
// MemoClone
//
// Created by 박현수 on 2023/11/20.
//

import UIKit
import SnapKit

protocol MemoAddDelegate: AnyObject {
func didAddMemo(title: String, content: String)
}

class AddMemoViewController: UIViewController, UITextViewDelegate {

weak var delegate: MemoAddDelegate?

@objc func registerMemo() {
if (titleText.textColor == .lightGray || contentText.textColor == .gray || titleText.text.isEmpty || contentText.text.isEmpty) {
print("잘못된 제목 또는 내용입니다.")
return
}
Comment on lines +20 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

내용이 입력되지 않았을 때의 분기처리 하신 거 너무 좋습니다!!
그런데 로그만 띄우면 사용자는 알 수 없으니까

  1. 내용이 입력되지 않았을 때 '작성하기' 버튼 비활성화 하고 입력됐을 때 활성화 시키기
  2. 내용이 입력되지 않았을 때 '작성하기' 버튼 클릭하면 '제목 or 내용을 입력하세요' alert 띄우기
    와 같은 코드를 구현하면 더 좋을 것 같아요!

delegate?.didAddMemo(title: titleText.text, content: contentText.text)
_ = self.navigationController?.popViewController(animated: true)
}

var titleText: UITextView = {
let text = UITextView()
text.layer.cornerRadius = 5
text.layer.borderWidth = 0.5
text.text = "제목 입력"
text.textColor = UIColor.lightGray
text.tag = 1
return text
}()

var contentText: UITextView = {
let text = UITextView()
text.layer.cornerRadius = 5
text.layer.borderWidth = 0.5
text.text = "내용 입력"
text.textColor = UIColor.gray
text.tag = 2
return text
}()

func textViewDidBeginEditing(_ textView: UITextView) {
if textView.tag == 1 {
if textView.textColor == UIColor.lightGray {
textView.text = nil
textView.textColor = UIColor.black
}
}
else if textView.tag == 2 {
if textView.textColor == UIColor.gray {
textView.text = nil
textView.textColor = UIColor.black
}
}
}

func configureSubviews() {
view.addSubview(titleText)
view.addSubview(contentText)
}

func makeConstraints() {
titleText.snp.makeConstraints { make in
make.top.equalTo(view.safeAreaLayoutGuide).offset(50)
make.leading.equalToSuperview().inset(50)
make.trailing.equalToSuperview().inset(50)
Comment on lines +71 to +72
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make.leading.trailing.equalToSuperview().inset(50)
한 줄로 작성할 수 있습니다!

make.height.equalTo(50)
}
contentText.snp.makeConstraints { make in
make.top.equalTo(view.safeAreaLayoutGuide).offset(150)
make.leading.equalToSuperview().inset(50)
make.trailing.equalToSuperview().inset(50)
make.height.equalTo(50)
}
}

override func viewDidLoad() {
super.viewDidLoad()

let rightButton = UIBarButtonItem(title: "작성하기", style: .plain, target: self, action: #selector(registerMemo))

titleText.delegate = self
contentText.delegate = self
configureSubviews()
makeConstraints()

view.backgroundColor = .white
navigationItem.title = "메모 작성"
navigationItem.rightBarButtonItem = rightButton

// Do any additional setup after loading the view.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

}
4 changes: 2 additions & 2 deletions iOS_Study_A/AppDelegate.swift → MemoClone/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//
// AppDelegate.swift
// iOS_Study_A
// MemoClone
//
// Created by 박지윤 on 2023/09/28.
// Created by 박현수 on 2023/11/20.
//

import UIKit
Expand Down
96 changes: 96 additions & 0 deletions MemoClone/DetailViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// DetailViewController.swift
// MemoClone
//
// Created by 박현수 on 2023/11/20.
//

import UIKit
import SnapKit

protocol ModifyMainViewDelegate: AnyObject {
func modifyArray(title: String, content: String, cellIndex: Int)
}

class DetailViewController: UIViewController, ModifyMemoDelegate {

var modifyMainViewDelegate: ModifyMainViewDelegate?

func memoModified(index: Int, cellTitle: String, cellContent: String) {
modifyMainViewDelegate?.modifyArray(title: cellTitle, content: cellContent, cellIndex: index)
isModified = true
}

var cellIndex: Int = 0
var isModified: Bool = false

var titleLabel: UILabel = {
let title = UILabel()
title.font = .boldSystemFont(ofSize: 20)
return title
}()

var contentLabel: UILabel = {
let content = UILabel()
content.font = .systemFont(ofSize: 15)
return content
}()

@objc func modifyMemo() {
let modifyView = ModifyMemoViewController()
modifyView.modifyMemoDelegate = self
modifyView.titleText.text = titleLabel.text
modifyView.contentText.text = contentLabel.text
modifyView.cellIndex = cellIndex
_ = self.navigationController?.pushViewController(modifyView, animated: true)
}

func configureSubViews() {
view.addSubview(titleLabel)
view.addSubview(contentLabel)
}

func makeConstraints() {
titleLabel.snp.makeConstraints { make in
make.leading.equalTo(view.safeAreaLayoutGuide).inset(20)
make.trailing.equalTo(view.safeAreaLayoutGuide).inset(20)
make.top.equalTo(view.safeAreaLayoutGuide).offset(10)
}
contentLabel.snp.makeConstraints { make in
make.leading.equalTo(view.safeAreaLayoutGuide).inset(20)
make.trailing.equalTo(view.safeAreaLayoutGuide).inset(20)
make.top.equalTo(titleLabel.snp.bottom).offset(10)
}
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white

let rightButton = UIBarButtonItem(title: "수정하기", style: .plain, target: self, action: #selector(modifyMemo))
navigationItem.title = "메모 상세"
navigationItem.rightBarButtonItem = rightButton

configureSubViews()
makeConstraints()
// Do any additional setup after loading the view.
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if isModified == true {
_ = self.navigationController?.popViewController(animated: false)
Comment on lines +80 to +81
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정이 됐을 때 바로 홈으로 이동하는 것보다는
이전 뷰인 메모 상세 뷰로 이동하는 게 더 자연스러울 것 같아요!

}
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

}
2 changes: 0 additions & 2 deletions iOS_Study_A/Info.plist → MemoClone/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
<key>UISceneStoryboardFile</key>
<string>Main</string>
</dict>
</array>
</dict>
Expand Down
55 changes: 55 additions & 0 deletions MemoClone/MemoTableViewCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// MemoTableViewCell.swift
// MemoClone
//
// Created by 박현수 on 2023/11/20.
//

import UIKit
import SnapKit

class MemoTableViewCell: UITableViewCell {

static let identifier = "MemoTableViewCell"

var titleLabel: UILabel = {
let label = UILabel()
label.font = .boldSystemFont(ofSize: 15)
return label
}()

var contentLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 10)
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.leading.trailing.equalTo(contentView).inset(15)
}
contentLabel.snp.makeConstraints { make in
make.top.equalTo(titleLabel.snp.bottom).offset(5)
make.leading.trailing.bottom.equalTo(contentView).inset(15)
}
}
}
80 changes: 80 additions & 0 deletions MemoClone/ModifyMemoViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// ModifyMemoViewController.swift
// MemoClone
//
// Created by 박현수 on 2023/11/20.
//

import UIKit
import SnapKit

protocol ModifyMemoDelegate{
func memoModified(index: Int, cellTitle: String, cellContent: String)
}

class ModifyMemoViewController: UIViewController {

var modifyMemoDelegate: ModifyMemoDelegate?

var cellIndex: Int = 0

var titleText: UITextView = {
let text = UITextView()
text.layer.cornerRadius = 5
text.layer.borderWidth = 0.5
return text
}()

var contentText: UITextView = {
let text = UITextView()
text.layer.cornerRadius = 5
text.layer.borderWidth = 0.5
text.textColor = UIColor.gray
return text
}()

@objc func modifyMemo() {
modifyMemoDelegate?.memoModified(index: cellIndex, cellTitle: titleText.text, cellContent: contentText.text)
navigationController?.popViewController(animated: true)
}

func configureSubviews() {
view.addSubview(titleText)
view.addSubview(contentText)
}

func makeConstraints() {
titleText.snp.makeConstraints { make in
make.top.equalTo(view.safeAreaLayoutGuide).offset(50)
make.leading.equalToSuperview().inset(50)
make.trailing.equalToSuperview().inset(50)
make.height.equalTo(50)
}
contentText.snp.makeConstraints { make in
make.top.equalTo(view.safeAreaLayoutGuide).offset(150)
make.leading.equalToSuperview().inset(50)
make.trailing.equalToSuperview().inset(50)
make.height.equalTo(50)
}
}

override func viewDidLoad() {
super.viewDidLoad()

/*let rightButton = UIBarButtonItem(title: "수정하기", style: .plain, target: self, action: #selector(modifyMemo))

titleText.delegate = self
contentText.delegate = self*/
configureSubviews()
makeConstraints()

let rightButton = UIBarButtonItem(title: "수정하기", style: .plain, target: self, action: #selector(modifyMemo))

view.backgroundColor = .white
navigationItem.title = "메모 수정"
navigationItem.rightBarButtonItem = rightButton
//navigationItem.rightBarButtonItem = rightButton
// Do any additional setup after loading the view.
}
}

Loading