FirstViewController에 들어갈 코드
@IBAction func transferData( _ sender: Any ) {
//(identifier: "Next viewcontroller의 storyboard ID")
guard let receiveController = self . storyboard? . instantiateViewController ( identifier: " secondViewController " ) as? SecondViewController else { return }
receiveController. name = nameTextfield. text
receiveController. email = emailTextfield. text
receiveController. isOnoff = sampleSwitch. isOn
receiveController. frequency = sampleSlider. value
//코드로 뷰간의 present연결 (이때,스토리보드에선 뷰간 연결해주면 안됨)
self . present ( receiveController, animated: true , completion: nil )
}
SecondViewController에 들어갈 코드
//이전 viewcontroller에서 넘겨준 데이터를 어떤 형식으로 받을지 선언
//내부에선 아울렛 변수를 참조할 수 없어 만들어줌
var name : String ?
var email : String ?
var isOnoff : Bool ?
var frequency : Float ?
//뒤로가기 Action(이때도 스토리보드에선 뷰간 연결해주면 안됨)
@IBAction func backScreen( _ sender: Any ) {
self . dismiss ( animated: true , completion: nil )
}
* 과제1: Navigation 활용 화면이동
Push & Present 후 처음 뷰로 돌아갈 때
//navigation stack에서 RootViewController(처음 뷰)를 제외한 쌓여있는 모든 뷰를 제거하여 최상위 뷰로 되게 해줌.
self . present ( lvc, animated: true , completion: {
self . navigationController? . popToRootViewController ( animated: true )
} )
import UIKit
class CustomButton : UIButton {
required init ? ( coder aDecoder: NSCoder ) {
super. init ( coder: aDecoder) !
//값이 커질수록 둥글
self . layer. cornerRadius = 0.02 * self . bounds. size. width
//버튼 배경색
self . backgroundColor = UIColor ( red: 8 / 255 , green: 37 / 255 , blue: 108 / 255 , alpha: 1 )
//버튼의 textcolor
self . tintColor = UIColor . white
//버튼 text 굵기 및 크기
self . titleLabel? . font = UIFont . boldSystemFont ( ofSize: 20 )
//버튼의 외곽선컬러
//self.layer.borderColor =
//버튼의 외곽선두께: 값이 커질수록 두꺼움
//self.layer.borderWidth =
}
}
동일한 Action을 하는 button이 여러 개일 때, 한 button의 Action을 우선 선언한 후 그 Action func의 블랙포인트('Line 26')에서 나머지 button으로 드래그해주면 된다.
Navigation bar 배경색 설정 및 경계선 없애는 설정
self . navigationController? . navigationBar. barTintColor = . white
self . navigationController? . navigationBar. shadowImage = UIImage ( )
self . navigationController? . navigationBar. isTranslucent = false
Navigation bar backbutton custom
self . navigationController? . navigationBar. tintColor = UIColor ( red: 7 / 255 , green: 59 / 255 , blue: 163 / 255 , alpha: 1.0 )
self . navigationController? . navigationBar. topItem? . title = " "
* UIPickerView : 뮤직리스트 앱 만들기
* UICollectionView : 뮤직리스트 앱 만들기
* 과제1: tableview로 카카오톡 채팅 구현하기
원하는 cell에만 구분선을 만들고 싶을 때 : UIview이 Height = 0.5인 선을 원하는 cell에 만들어 준 후, tableview의 구분선을 없애준다.
//tableview cell 간 구분선 없애기
Tableview . separatorStyle = UITableViewCell . SeparatorStyle. none
Cell header customize 하기
func tableView( _ tableView: UITableView , viewForHeaderInSection section: Int ) -> UIView ? {
let headerView = UIView ( )
//몇번째 section의 header를 custom 할 것인지 정하기
if section == 2 {
let friendCountLabel = UILabel ( )
friendCountLabel. text = " 친구 \( self . friendInformations. count) "
friendCountLabel. textColor = UIColor ( red: 129 / 255 , green: 129 / 255 , blue: 129 / 255 , alpha: 1.0 )
friendCountLabel. frame = CGRect . init ( x: 16 , y: 8 , width: 35 , height: 17 )
friendCountLabel. font = UIFont . systemFont ( ofSize: 11 )
headerView. addSubview ( friendCountLabel)
}
return headerView
}
//custom한 cell의 header를 제외한 다른 cell header (header height = 0으로 하여) 없애주기
func tableView( _ tableView: UITableView , heightForHeaderInSection section: Int ) -> CGFloat {
if section == 0 {
return 0
}
else if section == 1 {
return 0
}
else if section == 2 {
return 35
}
return tableView. sectionHeaderHeight
}
func tableView( _ tableView: UITableView , trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath ) -> UISwipeActionsConfiguration ? {
if indexPath. section == 2 {
let deleteAction = UIContextualAction ( style: . destructive, title: " 삭제 " , handler: { ( ac: UIContextualAction , view: UIView , success: ( Bool ) -> Void ) in
success ( true )
self . friendInformations. remove ( at: indexPath. row)
self . friendTableview. reloadData ( )
} )
return UISwipeActionsConfiguration ( actions: [ deleteAction] )
}
else {
return UISwipeActionsConfiguration ( )
}
}
설정버튼 눌렀을 때 Actionsheet 나오게 하기
@IBAction func settingBtnAction( _ sender: Any ) {
let optionMenu = UIAlertController ( title: nil , message: nil , preferredStyle: . actionSheet)
let manageAction = UIAlertAction ( title: " 친구 관리 " , style: . default)
let settingAction = UIAlertAction ( title: " 전체 설정 " , style: . default)
let cancelAction = UIAlertAction ( title: " 취소 " , style: . cancel)
optionMenu. addAction ( manageAction)
optionMenu. addAction ( settingAction)
optionMenu. addAction ( cancelAction)
self . present ( optionMenu, animated: true , completion: nil )
}