문제 해결 기록
UIAlertController의 present 중복 문제
- 에러 처리시, UIAlertController를 사용하여 사용자에게 알림. 하지만 여러 개의 에러가 연쇄되어 발생시, UIAlertController가 여러 개 present되면서 에러가 발생.
- 다음과 같이 isBeingPresented를 이용하여 UIAlertController의 present상태를 확인 후 띄워줌.
if !self.alert.isBeingPresented {
self.alert.message = self.apiManager.errorHandler(error: error)
self.present(self.alert, animated: true, completion: nil)
}
NSLayoutConstraint 변경
- 텍스트 필드에 텍스트를 입력할때, 텍스트필드의 너비가 줄어들면서 숨어있던 버튼이 나오게 하려고 했는데, 단순히 새로운 값을 activate() 해주면, 같은 뷰에 제약이 두 개가 들어가며 레이아웃이 깨짐.
- 제약의 객체 타입은 NSLayoutConstraint, 해당 객체의 constant 값을 변경
// 변수로 선언 후, NSLayoutConstraint.activate([])로 다른 뷰들의 제약을 설정해줄때, 적절한 곳에 넣음
private lazy var trailingOfSearchTextField: NSLayoutConstraint = searchTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8)
@IBAction func touchUpCancelButton(_ sender: UIButton) {
searchTextField.text = ""
searchTextField.resignFirstResponder()
}
func textFieldDidBeginEditing(_ textField: UITextField) {
UIView.animate(withDuration: 0.2) {
self.trailingOfSearchTextField.constant = -50
self.view.layoutIfNeeded()
}
}
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
UIView.animate(withDuration: 0.2) {
self.trailingOfSearchTextField.constant = -8
self.view.layoutIfNeeded()
}
}
UITableView안에 UICollectionView 넣을 때, 데이터 전달하기
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: WeatherForecastTableViewCell.identifier, for: indexPath) as? WeatherForecastTableViewCell else { return UITableViewCell() }
cell.dayLabel.text = dayList[indexPath.row]
cell.setUpForecast(forecast: forecasts[indexPath.row])
return cell
}
// 전달받을 변수
private var forecast: Forecast?
override func prepareForReuse() {
super.prepareForReuse()
forecast = nil
}
// prepare를 통해서 테이블뷰의 indexPath.row마다 다른 데이터를 적용
func setUpForecast(with forecast: Forecast) {
self.forecast = forecast
}