Clear stack of ViewControllers when Exit and Re-login - swift

i've an app in swift 5, where i want to my user can logout, going to a login screen, so i created a menu with option "Logout". the option is working, but, when i logout and login again, instead of my "segue" leave to the main screen, leaves back to the option menu, where i was before. So, I need to, when I logout, clear my previous stack, to i don't go back to my Menu, previosly openned, when i re-login.
I tried to use dismiss method, uwind segue and popToRootViewController, but, or not worked, or they had a delay, and poped up my login screen, instead of my option menu.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
//self.navigationController?.popToRootViewController(animated: false)
}

Set the rootViewController of the main window that lives inside the app delegate:
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = vc

Related

Dismiss completly a view controller in middle

I've been looking for days. Here's the problem:
I have a Home view controller. When it receives a notification, it presents a NotiViewController, called noti1, by
`home.present(noti1, animated: true, completion: nil)`.
After 15sec, another notification comes, I get the top view controller topVC, present another NotiViewController, called noti2, by
`topVC.present(noti2, animated: true, completion: nil)`.
Each notiVC has an timer which waits for 30sec to dismiss itself. Now I have Home -> noti1 -> noti2.
After 15sec, noti1 runs out of time and has to be dismissed. How can I dismiss it without interupting noti2 which is being presenting?
I tried
`orderVC.beginAppearanceTransition(false, animated: false)
orderVC.willMove(toParent: nil)
orderVC.view.removeFromSuperview()
orderVC.removeFromParent()
orderVC.endAppearanceTransition()`
These code does remove the view from screen, but it leaves a UITransitionView behind, which blocks user actions.
This image is took after noti2 is removed from superview etc, so there are 2 UITransitionViews is presenting, blocking user actions.
Any idea? Thanks a lot.
Found a way. I'm not sure if I'm correct or wrong, but here is my thought: View controller must be dismissed the way it's presented before. For example if you use present(viewController, animated, completion) to present, you should use dismiss(animated, completion) to dismiss. The problem, in my case, is if you dismiss a view controller, all child view controllers will be gone too.
The way I resolved my issue is using another way to "show" the target view controller:
parentVC.view.addSubview(childVC.view)
parentVC.addChild(childVC)
childVC.didMove(toParent: parentVC)
After adding some view controllers, I got a tree parentVC -> child1 -> child2 -> child3... If I want to dismiss child2, the code in my question worked:
child2.willMove(toParent: nil)
child2.view.removeFromSuperview()
child2.removeFromParent()
There is no UITransitionView blocking user actions, but it got no animation. For my this is good enough.

Dismissing SKScene Completely

Okay so I have an app that starts off with a menu view controller that prompts user to press one of 4ish buttons which then loads a viewcontroller which then presents a scene and the user plays the game based on which button was pressed.
I am then having the user being redirected to another viewcontroller which presents another another scene, once a condition is met (they lose the game). Only problem is, the 2nd viewcontroller(and i'm assuming it's scene) is still running. I know this because I have a print statement inside of it's override function update method to see if it's still there.
In addition, I have audio playing in my old gamesene and it's still currently playing. I wouldn't EXACTLY mind that since later on i'm going to just end up passing audio data (mute all) between all 3 viewcontrollers and their presented scenes.
Only problem with what is going on right now is that when I run the app since the old viewiewcontroller an it's scene seem to still be running underneath, it keeps calling the transition which causes a weird look where when the condition meets, the transition loops endlessly to the new viewcontroller then back to the beginning of the transition then to the new viewcontroller again.
I've tried this piece of code:
let theVC = self.viewController?.storyboard?.instantiateViewController(withIdentifier: "TrumpVC") as! TrumpViewController
self.viewController?.navigationController?.pushViewController(theVC, animated: true)
self.viewController?.dismiss(animated: true, completion: {});
But it doesn't seem to help at all :( Essentially I navigate to a new viewcontroller and dismiss the current one (this is all in my scene)
Thanks
Solution:
let theVC = self.viewController?.storyboard?.instantiateViewController(withIdentifier: "TrumpVC") as! TrumpViewController
self.viewController?.navigationController?.pushViewController(theVC, animated: true)
self.viewController?.removeFromParentViewController()
self.viewController?.dismiss(animated: true, completion: nil)
self.view?.presentScene(nil)

Swift 3 - How to add navigation controllers buttons (as Back) after renewing - refresh and reload my viewcontroller

I am trying to refresh my viewcontroller (reload it ) using UIApplication.shared.keyWindow?.rootViewController as shown below in code, it works however when reloaded the novaigation controller is not there, I mean there a back button that can go to previous view controller it does not appear when I did that - any help
#IBAction func Save(_ sender: AnyObject) {
UIApplication.shared.keyWindow?.rootViewController = storyboard!.instantiateViewController(withIdentifier: "UploadPhotoviewcontroller")
}
Default back button, appearance and work in current navigationController navigation stack. If you change rootViewController, this action will destroy you current navigation stack and all previous controllers. And you cant go back.
Change the rootViewController be bad practic. I think you have a problems with the architecture
#jon when I try to comment it did not and I can not reply to your question as comment - I do not know why
Exactly that is the reason
I found the answer:
To deselect the picked image in elcimagepickercontroller use below code:
imagePicker.popToRootViewController(animated: true)
self.dismiss(animated: true, completion: nil)

Come back to the tabBarController, swift

Currently on my viewController : Upload, my button send the data to my database only if all the information are filled out, and I come back to the preview view (table View) with :
self.navigationController?.popViewControllerAnimated(true)
I would like, if it is possible, to come back to my main view on the tabBarController. I tried many things, like directly on the storyboard with Present modally segue to "TabBar controller", but I come back to the TabBar without sending my data to the database and without checking in the information are filled out..
How can I do it?
Thanks!
UITabBarController has a property selectedIndex with which you can switch the selected tab. So on completion after dismissing the UploadViewController you can run:
self.tabBarController?.selectedIndex = 0 // Index to select
It would probably be best to create a delegate for your UploadViewController to fire a function to do all the work in your previewVC on API call completion.
(Super late response...in case someone has similar questions, presumably in later version of Swift, such as mine which is Swift 5, iOS 13.2). Steps:
Be sure to set an id for your UITabBarController storyboard, e.g. "TabBarViewController"
Next, add the following to an action that has already been connected to a button:
let ID_TABBAR = "TabBarViewCOntroller"
#IBAction func returnToTabbar(_ sender: Any) {
let tabBarController = self.storyboard?.instantiateViewController(identifier:ID_TABBAR) as! UITabBarController
self.navigationController?.pushViewController(tabBarController, animated:true)
}
Referenced from one of the responses from this post.
Update: In case your Tab Bar View Controller also happens to be the root view controller, the two lines of the code in the returnToTabbar method above can be:
self.dismiss(animated:true, completion:nil);
self.navigationController?.popViewController(animated:true);
(ref.: See answer here, for Swift4 but works just fine in Swift5)

Segue following a change in criteria from a local notification in Swift

I have created a local notification in Swift which gives the option to end a current game without having to go back in to the app. That's all fine and works as it should. The issue I'm having is that if the user does this, I don't want them to go back to the Game view controller if that happens to be the last view that was open when the app entered the background. I would like them to go back to the app's Home view controller instead.
I expected to be able to add a perform segue to my Game view controller in the following way, should the criteria match. I tried adding it to viewDidAppear(), but it didn't work:
override func viewDidAppear(animated: Bool) {
if isThereACurrentGame() == false {
performSegueWithIdentifier("unwindToHomeScreen", sender: self)
}
}
Is this something to do with viewDidAppear() not being called when the app comes back to the foreground? If so, what might an alternative be?
P.S. My isThereACurrentGame() function works as it should, as does the performSegueWithIdentifier elsewhere in the view controller, so these aren't the cause of the problem.