Segue from popup ViewController Swift - ios

I have app with 7 screens(VC) which are embedded in navigation controller.
And I have one separate VC that is not embedded. This VC is acting like custom popup class PopupView: UIViewController {} and get's called by pressing button from every screen that I have in my app using custom segue (setup as custom segue in storyboard):
open class MIBlurPopupSegue: UIStoryboardSegue {
open override func perform() {
MIBlurPopup.show(destination, on: source)
} }
In this popup there's a button that should open another VC (VC always the same) that is embedded in navigation stack.
What I'm trying to achieve is to actually open that VC that is inside navigation stack by pressing button in Popup VC and then get back to the screen where Popup was called.
So, user journey will look like - Opened VC1(2,3,5,6,7) -> Called popup VC -> Pressed button -> opened VC4 -> pressed navigation back button -> returned to VC1.
I what I have right now:
Connected in storyboard all 6 screens to VC4, with segues ids
Tried performSegue(withIdentifier: "toVC4"), present(vc, animated: true, completion: nil)
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "VC4")
self.present(controller, animated: true, completion: nil)
Using protocols to call func in VC1 but failed.
I'm definitely missing something and would be very thankful if someone could provide code sample to solve this issue.

You can try this inside the popup button's action
self.dismiss(animated:true) {
if let nav = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
let vc1 = storyboard!.instantiateViewController(withIdentifier: "MenuId")
let finalVC = storyboard!.instantiateViewController(withIdentifier: "finalId")
nav.setViewControllers([vc1,vc4],animated:true) // set it to false if you want
}
}

Related

Presenting custom popup from popup then dismissing first popup causes flicker

Currently I create a custom popup A then when a button in it is pressed dismiss it and in the completion handler create a new popup B.
Both popups are similar in that they use a black view with alpha set to a value to simulate the grayed out screen affect that a standard alert provides.
However dismissing A before creating B causes a flicker. I would like to create B before dismissing A but haven't found a good way to do this.
Ideas on how to do this and avoid the flicker?
I'm thinking of putting the black view with the alpha set in the view controller screen that shows popup A and turning it on when popup A is shown and off when popup B is dismissed. This doesn't seem like the best solution however. Another way would be to have a single popup and show hide controls but this seems not a good solution either because it makes the storyboard VC messy.
Here's how the code looks now:
From a menu the popup A is created:
let storyBoard = UIStoryboard(name: "MenuStoryboard", bundle: nil)
let aPopup = storyBoard.instantiateViewController(withIdentifier: "Popup_A")
present(aPopup, animated: false, completion: nil)
Then from button action of Popup A:
self.dismiss(animated: false) {
let storyBoard = UIStoryboard(name: "MenuStoryboard", bundle: nil)
let bPopup = storyBoard.instantiateViewController(withIdentifier: "PopUp_B")
if let topViewController = UIApplication.shared.topMostViewController {
topViewController.present(bPopup, animated: false, completion: nil)
}
}
And the extensions:
extension UIViewController {
var topMostViewController : UIViewController {
if let presented = self.presentedViewController {
return presented.topMostViewController
}
if let navigation = self as? UINavigationController {
return navigation.visibleViewController?.topMostViewController ?? navigation
}
if let tab = self as? UITabBarController {
return tab.selectedViewController?.topMostViewController ?? tab
}
return self
}
}
extension UIApplication {
var topMostViewController : UIViewController? {
return self.keyWindow?.rootViewController?.topMostViewController
}
}
The way you do is not recommended, a View Controller should not be concerned of "dismissing itself" and then "asking" another view controller to present a new view controller.
I'd use delegation from PopupA to communicate back to whatever object presented it. That object SHOULD dismiss the PopupA view controller.
Once completed, you could try to display PopupB.

Navigation pushViewController is not working

i have login view controller. when usersingin i' showing popup for that i
refereed
https://www.youtube.com/watch?v=S5i8n_bqblE i can achieved that.
popup had button when i click the button navigation to next view controller
but its now working when i am clicking that action is performing but its not navigating to next view controller wher i did mistake
Singin
class DigitalGateViewController: NANavigationViewController
{
#IBAction func singin(_ sender: UIButton)
{
let lv = NAViewPresenter().activityVC()
self.present(lv, animated: true)
}
}
this is popupviewcontroller
class ActivityViewController: NANavigationViewController {
#IBAction func okbuttonclick() {
let dv = NAViewPresenter().myGuestListVC()
// self.navigationController?.pushViewController(dv, animated: true)
}
}
its not push to textview controller in swift
When you present a view controller, its presented modally and is not pushed onto the previous navigation controller's stack. Hence, you tried to call self.navigationController?.pushViewController(), it doesn't work, because self i.e. NAViewPresenter().myGuestListVC() isn't embedded in a navigation Controller.
If you want to push the new VC onto the previous stack, you will have to dismiss the presented pop up and then push. The easiest way to do this is to use a delegate method.
Edit:
if you want to create a new navigationController, you can do something like this :
let navController = UINavigationController(rootViewController: NAViewPresenter().myGuestListVC())
present(navController, animated: true)
After presenting the navController, you can use self.navigationController.push method henceforth.
The reason why its not pushing because you are presenting it modally not pushing on the navigation stack and so it wont have any navigationController. If you want to push from your modal popup, you can access the property presentingViewController on your modal object and try to push it on navigationController from there.
self.presentingViewController?.navigationController?.pushViewController(myVC, animated: true)
dismiss(animated: true, completion: nil)

How to go through Views without reload them again if I return back Swift

I have ViewController1 and ViewController2 . When i click a button on the ViewController1 should go to ViewController2 and when press a custom back button should return to ViewController1 without reloading its content for ex:- if i type something on ViewController1's text filed it should remain as it is.
Does anyone knows any solution for this?
viewcontoller1 button click code...
#IBAction func Parsent(sender: AnyObject) {
let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "Identifier" as! ViewController2
let navigationVC = UINavigationController(rootViewController: secondVC)
self.present(navigationVC, animated: true, completion: nil)
}
ViewController2 button code here..
#IBAction func Dismiss(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
1- Load View Controller 1
2- Your custom button should present ViewController 2 on top of view controller 1 (the native self.present does this, note that it present it on top of VC1 so VC1 is still there)
3- your back button on View Controller 2 dismisses View Controller 2 and you return to View Controller 1 the same as you left it
Hope this helps!

Why doesn't my UIButton segue behave like a UITableViewCell segue?

I am trying to create segues between my UIViewControllers but am having some difficulties with creating a segue from a UITableViewCell and a UIButton.
When I create a show detail segue through storyboard from a UITableViewCell to a UIViewController it works perfectly and I get the back button showing up. But when I try to create a show detail segue from a UIButton to a UIViewController it doesn't register the navigation stack and presents the screen modally without the back button.
How can I make a successful show detail segue from a UIButton to a viewcontroller? I am new to iOS and am having trouble determining why the UIButton segue doesn't behave the same as the UITableViewCell segue.
Thanks in advance for any help!
Dont connect a segue manually do it through button action.
This is assuming the viewController that has this button is the root view controller of the navigation controller
#IBAction func myButtonTapped(_ sender: Any) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "YOUR STORYBOARD IDENTIFIER GOE HERE")
self.show(vc, sender: self)
}
If you want to go to a tab of a tab bar controller you have to specify its index. I think you can set it in storyboard but i just go with 0 is on the left then they go up sequentially. so in the example below i want to go to the second tab of the tab bar controller with a modal transition. I assume you can use show here like example above I've just never done it.
let tbc = self.storyboard!.instantiateViewController(withIdentifier: "MyTabController") as! UITabBarController
tbc.selectedIndex = 1 // this is 2nd tab index.. start at 0
tbc.modalPresentationStyle = .overCurrentContext
tbc.modalTransitionStyle = .coverVertical
self.present(tbc, animated: true, completion: { finished in
// you can also do some completion stuff in here if you require it.
// self.view.removeFromSuperview()
// self.navigationController?.navigationBar.removeFromSuperview()
})
let vc = self.storyboard!.instantiateViewController(withIdentifier: "StoryBoardID")
self.navigationController?.pushViewController(vc, animated: true)

How to push viewcontroller correctly

This is my storyboard:
Short Description:
My App starts with the LaunchController
a modal segue shows the Reveal View Controller
this bring the Menu Controller and my Main Navigation Controller (ID
"NavController"; green Navbar) together. this will create a slide
menu. (Basic Code:
appcoda.com/ios-programming-sidebar-navigation-menu/)
my Main Navigation Controller shows a TableViewController.
this one have a menu button (3 Lines) which make the slide menu
visible
the plus icon willo push the last view Controller (ID: "VC1").
My Problem:
I would like to set quick actions for my app.
This code help works for that:
#available(iOS 9.0, *)
func handleShortcut( shortcutItem:UIApplicationShortcutItem ) -> Bool {
var succeeded = false
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("NavController")
self.window?.rootViewController = initialViewController
let navVC = self.window?.rootViewController as! UINavigationController
switch shortcutItem.type {
default:
navVC.pushViewController(storyboard.instantiateViewControllerWithIdentifier("VC1"), animated: false)
succeeded = true
break
}
return succeeded
}
This Code set the NavController as initial Controller und push the to VC1.
This works fine.
With the X icon in VC 1, i use the unwind function back to TableView.
The Problem: if i use the undwind function and fall back to tableview i can't open the slide menu. A Touch on the menu icon give no reaction.
The Problem can be, that i start the app with the quick action behind the Reveal View Controller.
How can I solve this problem?
Did you try to make the reveal view controller the entry point of your app and deleting the launch controller, because if you just want to go back to the main view you could just do this :
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateInitialViewController() as UIViewController!
self.presentViewController(controller, animated: false, completion: nil)
If you cannot could you explain the purpose of the launch controller?

Resources