ios개발/개념 정리

<스위프트> 뷰 컨트롤러: modal, present, push back

studying develop 2020. 3. 3. 21:53

(sans : 없이라는 뜻)

 

뷰컨트롤러와 화면 전환에 대한 개념들이다. 

 

공부하다 보니 뷰 컨틀롤러 전환에 대해서 좀 공식 문서를 좀 더 자세히 읽게 되어서 새로운 글에서 정리하겠다.

 

1. push vs present

 

show 는 modal을 띄우는 것이라 하고, push는 네비게이션 뷰 컨틀롤러에서 올리는 것이라 한다.?? 뭔가 더 있을거 같은데

 

 

<https://medium.com/@twih1203/swift-ios-%ED%99%94%EB%A9%B4%EC%A0%84%ED%99%98%ED%95%98%EA%B8%B0-5e5998679d3a>

 

 

  1. ViewController의 view를 바꿔치기
  2. ViewController가 다른 ViewController 호출(present)
  3. NavigationController 사용하여 화면전환(push)
  4. 화면전환용 객체 Segue 사용

1번은 메모리 오버플로우 위험이 있다는데 왜그러지? 

 

2번은 모달로 뷰컨을 띄우는 방법이라는데 모달이 뭔지는 알겠는데 그냥 올라오는 방식이 다른거 말고 구성 요소적으로 다른건 뭘까?

 

<https://makeapppie.com/2016/06/30/adding-modal-views-and-popovers-in-swift-3-0/> 일단 여기서는 모달이 우리의 관심을 끌기위해 있고, 아이패드에서 시작했따는 역사랑, 어떻게 구현하는지 나온다.

 

<https://stackoverflow.com/questions/9392744/what-is-the-difference-between-modal-and-push-segue-in-storyboards> 여기서는 push랑 present modally의 차이를 알려준다. 사용 예시로는 modal은 로그인, push는 부모와 관련있는 VC를 통해 세부적으로 설명하고자 할때. 그리고 음 잘 이해 못하겠는 문장들이 있따.

 

A push Segue is adding another VC to the navigation stack. This assumes that VC that originates the push is part of the same navigation controller that the VC that is being added to the stack belongs to. Memory management is not an issue with navigation controllers and a deep stack. As long as you are taking care of objects you might be passing from one VC to another, the runtime will take care of the navigation stack. See the image for a visual indication:

 

메모리 관리가 왜 이슈가 아니지? 런타임이 네비게이션 스택을 관리한다고? (오 이게 스택의 장점일 수 있겠다. 메모리 관리가 매우 쉽다!!!?)

 

A modal Segue is just one VC presenting another VC modally. The VCs don't have to be part of a navigation controller and the VC being presented modally is generally considered to be a "child" of the presenting (parent) VC. The modally presented VC is usually sans any navigation bars or tab bars. The presenting VC is also responsible for dismissing the modal VC it created and presented.

 

아마 마지막 문장이 nav vc랑 좀 다른거 같다. 모달로 띄우면 부모 vc가 dismiss해야할 책임이 생긴다.!! 근데 nav vc랑 모달이랑 dismiss 방식이 크게 다른건가 이게? 물론 nav는 스택에서 제거하는 식일테고 ,modal은 dismiss하면 음 힙에서 제거하나? ( swift에 메모리 관리에 대한 글을 찾았따.  새로운 글로 올리겠다.)

 

Generally, when you want to show a detail view of a summary view, use a navigation controller and Push Segues. If the "parent" view doesn't really relate as far as data is concerned to the "child" view, then use a modal. A good example for a modal view would be a Login view. The Login view doesn't really have any relationship as far as data is concerned to the "parent" view. 

 

 

3번은 2번에서의 의문이랑 같게 뭐가 다르지

 

4번은 2,3번이랑 뭐가 다를까?

 

 

출처 : <https://medium.com/@twih1203/swift-ios-%ED%99%94%EB%A9%B4%EC%A0%84%ED%99%98%ED%95%98%EA%B8%B0-5e5998679d3a>

 

 

 

2. optional ? vs !

 

Use attachment.image!.size if you're guaranteed that image? isn't nil. If you're wrong (and it is nil) your app will crash. This is called forced unwrapping.

If you're not sure it won't be nil, use image?.

 

출처 : <https://stackoverflow.com/questions/25895098/what-is-difference-between-and-in-swift>