FlashChat TIL
-Cocoapod, Firebase practice
1. 로그인/로그아웃 테스트 중 계속해서 아이디 패스워드 치기 귀찮으면 => ?
storyboard 에서 잠깐 아이디 패스워드 쳐놓고 테스트 해도 됨
(코드베이스가 아니라 스토리보드 베이스일땐 이렇게 하면 진짜 편하네..)
2. popToRootViewController
웰컴VC -> 로그인VC -> 채팅VC로 이동한 경우,
채팅창에서 로그아웃한 뒤 WelcomeVC로 한번에 rootVC를 제외한 모든 stack을 pop 해서 넘어가는 코드
3. navigation back button 숨기기 - 한줄로 간단하게 해결
navigationItem.hidesBackButton = true
4. Constants 파일 만들기 by removing strings such as "⚡️FlashChat"
리터럴한 값들의 제거 (조그마한 타이핑 에러에도 에러가 나는걸 방지)
cf) Constants보다 짧게 쓰려면 상수 K
로 쓰는 경향이 있다.
4-1. 타입프로퍼티로 접근 (Constants파일 만든거 사용시)
struct MyStructure {
let instanceProperty = "ABC"
static let typeProperty = "123"
}
let myStructure = MyStructure()
print(myStructure.instanceProperty) //인스턴스에서 접근
print(MyStructure.typeProperty) //type에서 접근(MyStructure)
참고)
타입 메소드도 위와 매우 유사함
struct MyStructure {
func instanceMethod()
static func typeMethod()
}
let myStructure = MyStructure()
myStructure.instanceMethod() //인스턴스에서 접근
MyStructure.typeMethod() //type에서 접근(MyStructure)
5. 채팅 TableView에서 커스텀셀 (TableViewCell) 만들기 (with XIBfile)
XIBfile = 스토리보드 디자인 파일
스위프트 파일과 함께 xib file이 같이 생성됨
awakefromNib() 의 의미?
Nib이란 xib의 오래된 이름
awakefromNib() { } 여기 안에서 일어나는 일은 '초기화'
만들어진 Xibfile을 ChatVC에 등록하는 과정
1. XibFile에서 identifier name 적어두기 (tableview datasource 코드쪽의 dequereusablecell identifier를 여기에 적어야함)
2. ChatVC 에서 tableview (with xib file) register 코드를 viewDidLoad에 적어줌
tableView.register(UINib(nibName: K.cellNibName, bundle: nil), forCellReuseIdentifier: K.cellIdentifier)
3. tableview datasource 쪽에서 MessageCell(Xibfile) 로 타입캐스팅 및 xibfile안에 있는 요소들(label 등)에 대해서 datasource 배치
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: K.cellIdentifier, for: indexPath) as! MessageCell
cell.label.text = messages[indexPath.row].body
return cell
}
<타입캐스팅>
is : Type Checking 에 사용됨.
let cell = UITableViewCell()
if cell is UITableViewCell { //Cell이 UITableViewCell 타입인가?
print("The types match!")
}
[다운캐스팅]
as! : Forced Downcast 강제로 다운 캐스팅 (하위 클래스로) (191강)
let messageCell = cell as! MessageCell
as? : 안전하게 다운 캐스팅(인터넷이나 json 데이터 등등 타입이 확실치 않을 때 주로 사용
let messageCell = cell as? MessageCell
[업캐스팅]
as : 객체를 상위 클래스 유형으로 올리는데 사용
let newCell = messageCell as UITableViewCell
?나 !를 쓰지 않아도 오류가 발생하지 않는 이유? 하위 클래스를 가져와서 상위 클래스로 변환하는건 절대 실패하지 않기 때문
(보통 상속 받은 유형으로 업캐스팅하기 때문)
클로저는 백그라운드에서 실행되므로
UI 변경 메인쓰레드에서.
위에처럼 DispatchQueue.main.async 안에다가 tableView reload하는게 좋은 습관 :)
IQKeyboardManager 관련
https://github.com/hackiftekhar/IQKeyboardManager/wiki/Properties-&-Functions
Properties & Functions
Codeless drop-in universal library allows to prevent issues of keyboard sliding up and cover UITextField/UITextView. Neither need to write any code nor any setup required and much more. - hackiftek...
github.com
IQKeyboardManager.shared.enableAutoToolbar = false
키보드 너무 높이 올라가지 않게끔 autotoolbar를 가려줌
IQKeyboardManager.shared.shouldResignOnTouchOutside = true
textfield 이외의 곳을 클릭하면 키보드가 내려감
위 두 코드는 AppDelegate에서 아래와 같이 사용
navigation bar hidden 시키는 지점?
viewWillAppear
(뷰가 나올랑말랑 나오려고 할 때)
(뷰가 화면에 나오기 바로 직전에)
WelcomeVC 에는 navigation bar hidden 시키고 싶지만, 그 다음 화면부터는 네비게이션 바 띄우고 싶을 때?
viewWillDisappear에서 다시 hidden = false로 바꾸어주어야 함
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.isNavigationBarHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.isNavigationBarHidden = false
}
super클래스의 함수를 override 하는 경우, 반드시 super를 호출하는 습관 꼭 !
<앱 생명주기>
ViewDidLoad
ViewWillAppear - navigation bar hidden
ViewDidAppear
ViewWillDisappear
ViewDidDisappear
'iOS > iOS Swift Udemy - AngelaYu' 카테고리의 다른 글
SwiftUI) 간단한 테이블뷰 만들기 (0) | 2023.10.03 |
---|---|
SwiftUI background Image(배경 이미지)로 꽉 채우기 (0) | 2023.10.03 |
Sec15 179) Cocoapods 설치 (0) | 2023.01.25 |
Sec15 176) for in loops 반복문 (0) | 2023.01.25 |
Sec15. 175) 타이핑 애니메이션 (0) | 2023.01.25 |