bypolinafox
bypolinafox

Reputation: 23

How to navigate between controllers, swift

I'm creating analog of iphone's notes in order to studying. I want to make navigation between controllers. And there are two ways: navigation controller and present/dismiss methods. Now I use second.

I have three screens: "list of notes", "watch-note" screen and "edit/make-note" screen. When I go to "existingNote -> edit existingNote" (happening "present" from "list of note" and then "present" from "watch-note" screen), edit it and save, executing dismiss(one time) and my screen became "watch-note screen", it's ok. But when I make note (it happening from the "list of notes" screen) and save, executing dismiss and my screen became "list of notes" screen, but I want to see "watch-note" screen, wanna watch just made note.

Can I do it by present/dismiss or I have to use NavigationController?

Upvotes: 0

Views: 118

Answers (2)

Kartikey
Kartikey

Reputation: 143

Using present/dismiss:

You will need to present the "watch-note" screen on "list of notes" silently after make note. This involves two steps:

  1. "list of notes" screen should know that "make note" is being dismissed. You can either pass variables between two controllers or use dismissViewControllerAnimated:completion
  2. When you know the above, present "watch-note" from "list of notes". Remember to use animated: false otherwise user will see screen coming up animation.

Using Navigation Controller: (Highly recommended)

  1. Navigation controller will provide you more flexibility and ability to move between controllers. You can use navigationController.popToViewController to pop to desired controller list/watch/make/add notes.

  2. Your code won't be messy and will be easy to maintain. You need not pass variables or remember which screen you came from. You can use navigationController.viewControllers to know which controllers are in the stack.

Upvotes: 1

Prakash
Prakash

Reputation: 872

There is a method to identify that you have pushed through navigation controller or presented from view controller i.e., isBeingPresented

isBeingPresented is true when the view controller is being presented and false when being pushed.

-(void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];

if ([self isBeingPresented]) {
    // being presented
    DispatchQueue.main.async() {
        self.dismiss(animated: true, completion: nil)
    }
} else if ([self isMovingToParentViewController]) {
    DispatchQueue.main.async() {
        self.navigationController?.popViewController(animated: true)
    }
}

}

Upvotes: 0

Related Questions