Swift UI Navigation
using navigation controller
using normal segues direclty link to viewcontrollers
using segues programatically (use segue id to navigate after check conditions)
https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementNavigation.html
A navigation controller manages transitions backward and forward through a series of view controllers. The set of view controllers managed by a particular navigation controller is called its navigation stack. The first item added to the stack becomes the root view controller and is never popped off (removed from) the navigation stack.
add navigation controller to scene
select scene doc
Choose Editor > Embed In > Navigation Controller
the story board entry point will be automatically added to navigation controller
this will add a bar in all scenes called navigation bar. this can handle all backword and forward navigation
to use auto back item
add Navigation item to view. this will automatically add back arrow and click will move to back.
the segue shoud show. not work for present as popover
to use button add
add bar buttons for add back and forward.
To go back
dismiss(animated: true, completion: nil)
To go forward
click -> control drag to relavant scene
create super view controller and make other viewCOntrollers as children. make commont nav bar button items click event if you want and link click event with super controller function.
using normal segues direclty link to viewcontrollers
in this normal button use. no navigation controller. button segue link with view controller. to go back click use "Unwind Segue" way. it means
if you want to come back second to first
in first viewcontroller add method like
@IBAction func funcName(unwindSegue: UIStoryboardSegue) {}
in secind button click+control+ drag to exit icon in viewController header. this will suggest that function, it will auto do back process.
using segues programatically
here we add buttons to normal way to viewcontroller. but create segeue is doing by click on firstviewController header bar yellow icon(first icon)+ control+ drag to second view controller. another segue like same wat to another viewController. remember to add segue identifier.
then create action event to each button by normal control+click+ drag to viewcontroller. add this line to connect with each segue
performSegue(withIdentifier: "segue_id", sender: nil)
this way can use to check condition and open specific view controller
to perform back event in second to first use above any way.
pass values from viewControllers
https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?rq=1
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secViewCtrl = segue.destination as! SecondVcViewController
secViewCtrl.strFromFirst = "From first".
}
in secondvc create var
var strFromFirst
tabviewcontroller
drag view controller to story board and editor-> embed in -> tab bar controller
connect other view controllers as normal way by click + control+drag
UIViewController life cycle
https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html
viewDidLoad. - like oncreate
viewWIllAppear. - like onresume
ViewDidAppear
viewWIllDisappear. - like onpause
viewDidDisappear - can handle ondestroy part.
when app open
TabFirstViewController - viewDidLoad
TabFirstViewController - viewWillAppear
TabFirstViewController - viewDidAppear
swcond VC load
TabSecondViewController - viewDidLoad
TabFirstViewController - viewWillDisappear
TabSecondViewController - viewWillAppear
TabSecondViewController - viewDidAppear
TabFirstViewController- viewDidDisappear
back to first VC
TabSecondViewController - viewWillDisappear
TabFirstViewController - viewWillAppear
TabFirstViewController - viewDidAppear
TabSecondViewController - viewDidDisappear
kill app in first VC
TabFirstViewController - viewWillDisappear
TabFirstViewController- viewDidDisappear
what to handle viewWillDisappear vs viewDidDisappear
when viewWillDisappear called next viewDidDisappear called. viewWillDisappear can use to hide keyboard, save edits etc. viewDidDisappear can use to stop running services etc
No comments:
Post a Comment