Return from Hosting Controller to another controller from storyboard - swift

I have a project with Storyboard but now I'm migrating to SwiftUI.
I have a Hosting Controller that control del SwiftUI segment, but now I need to return from this to another controller from Storyboard.
Basically I can do this: Storyboard (Controller 1) -> HostingController (SwiftUI).
So now y I need to return: HostingController (SwiftUI) -> Storyboard (Controller 2).

I like to use closures for this. I assume you are presenting the hosting controller like this?
let viewController = UIHostingController(rootView: ContentView())
self.present(viewController, animated: true)
You can add a closure dismissSelf, inside your SwiftUI View struct:
struct ContentView: View {
var dismissSelf: (() -> Void)?
var body: some View {
Button(action: {
dismissSelf?()
}) {
Text("Return")
}
}
}
This will call dismissSelf when the button is pressed. Now, you need to assign dismissSelf to a block of code that dismisses the UIHostingController. You can do it like this:
class ViewController: UIViewController {
#objc func buttonPressed() {
var viewController: UIViewController?
let contentView = ContentView {
/// set the closure (it's a trailing closure, so you don't need to put the `dismissSelf`)
viewController?.dismiss(animated: true, completion: nil)
}
viewController = UIHostingController(rootView: contentView)
if let vc = viewController {
self.present(vc, animated: true, completion: nil)
}
}
/// make the button
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: 50, y: 50, width: 80, height: 40))
button.setTitle("Present", for: .normal)
button.setTitleColor(.blue, for: .normal)
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
view.addSubview(button)
}
}
There's one thing that's kind of oof about this. viewController will never be nil, but to avoid a force unwrap I put in an if let.
Result:

Related

How can I make sure that after declaring a delegate of a protocol in Swift 5.2, that delegate is not nil when it is called?

I am trying to clear a textfield in MainViewController from the DetailViewController. I have the following code in a Swift Playground.
import UIKit
import PlaygroundSupport
protocol DetailViewControllerDelegate: class {
func bar()
}
class DetailViewController: UIViewController {
var detailViewControllerDelegate: DetailViewControllerDelegate!
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let button = UIButton()
button.frame = CGRect(x: 100, y: 200, width: 180, height: 20)
button.setTitle("Hello World!", for: .normal)
button.backgroundColor = .blue
button.addTarget(self, action: #selector(handlePress), for: .touchUpInside)
view.addSubview(button)
self.view = view
}
#objc func handlePress() {
print("\(#function)")
if let vrvc = detailViewControllerDelegate {
vrvc.bar()
} else {
print("detailViewControllerDelegate is NIL")
}
}
}
class MainViewController : UIViewController, DetailViewControllerDelegate {
func bar() {
print("Inside Bar")
}
override func loadView() {
let detailViewController = DetailViewController()
detailViewController.detailViewControllerDelegate = self
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = DetailViewController()
When the button is pressed, detailViewController is nil. How can I make sure that detailViewController is NOT nil when the button is pressed?
You have two distinct DetailViewControllers:
override func loadView() {
let detailViewController = DetailViewController()
detailViewController now references a new instance of DetailViewController
detailViewController.detailViewControllerDelegate = self
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = DetailViewController()
liveView now references a new instance of DetailViewController which is different from the one above and in which you have not set detailViewControllerDelegate.
If you need to create a DetailViewController in one place and reference it in another you need to store a reference to it in a property that is accessible in both places.
That said, the second instance of DetailViewController is being created in a statement which looks like an attempt to test code in the Playground so maybe you just need to think about how you are testing.

How do I call an interstitial ad to load on another UIViewController?

I have a UIViewController with a UIView displayed on it; pressing a button on the UIView loads my interstitial ad. When the UIView is subsequently displayed, I want the interstitial to be displayed with the rootVC as the UIViewController.
However, this code does not seem to work as intended:
1) My View Controller:
class MyViewController: UIViewController {
let button: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle("BUTTON", for: .normal)
btn.addTarget(self, action: #selector(showUIView), for: .touchUpInside)
return btn
}()
#objc func showUIView(_ sender: UIButton) {
let popUp = MyUIView()
self.view.addSubview(popUp)
}
2) My UIView:
class MyUIView: UIView {
var interstitial: GADInterstitial!
let button: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle("UIVIEW BUTTON", for: .normal)
btn.addTarget(self, action: #selector(prepareInterstitial), for: .touchUpInside)
return btn
}()
#objc func prepareInterstitial(_ sender: UIButton) {
interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
let request = GADRequest()
interstitial.load(request)
dismissPopUp()
if interstitial.isReady {
interstitial.present(fromRootViewController: MyViewController())
}
}
I get this in the console:
Warning: Attempt to present <GADFullScreenAdViewController: 0x7f8611e22fc0> on <Project.MyViewController: 0x7f8612884800> whose view is not in the window hierarchy!`
which I do not understand because MyViewController is still very much a part of the view hierarchy.
I'd be really grateful if someone could show me how to fix this error, I'm relatively new to coding and not sure what I am doing wrong. Thank you!
The reason why this doesn't work is because you are creating a brand new VC here:
interstitial.present(fromRootViewController: MyViewController())
This MyViewController() is not the VC that is shown on the screen! You just created by calling its initialiser.
You need to somehow get the VC that's shown on the screen. One simple way to do this is to add a rootVC property to your MyUIView:
weak var rootVC: UIViewController?
And then present this instead:
if let rootVC = self.rootVC { // nil check!
interstitial.present(fromRootViewController: rootVC)
}
In showUIView, set self as rootVC:
#objc func showUIView(_ sender: UIButton) {
let popUp = MyUIView()
popUp.rootVC = self
self.view.addSubview(popUp)
}

Call a View from another ViewController Swift

I have a ViewController that has a UIView set on top of it and a button that opens a popover to another ViewController. I want a button on the popover view controller to set the UIView to disable. How do I reference the UIView from the first view controller from a button in the second view controller?
EDIT:
Below is code that I use to call the popover view controller. Notice how I call dimView.isHidden = false from this first viewcontroller. I want to run dimView.isHidden = true from the popover view controller.
let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC")
popover?.modalPresentationStyle = .popover
popover?.popoverPresentationController?.delegate = self as? UIPopoverPresentationControllerDelegate
popover?.popoverPresentationController?.sourceView = self.view
popover?.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popover?.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
dimView.isHidden = false
self.present(popover!, animated: false)
EDIT 2:
Below is my popover view controller. Since it is not called PopoverVC. I updated the answer to include let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC") as! PopOverViewController but still no luck.
import UIKit
var parentController:UIViewController?
class PopOverViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func closeButton(_ sender: Any) {
self.dismiss(animated: false, completion: nil)
}
}
EDIT 3:
class ViewController: FormViewController {
override func viewWillAppear(_ animated: Bool) {
dimView.isHidden = true
}
#IBOutlet weak var dimView: UIView!
You can pass a reference to your current view controller when presenting your PopoverVC and then you can access its view from PopoverVC. Just create a property in PopoverVC that can store the reference, like var parentController:UIViewController?
let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC") as! PopoverViewController
popover?.modalPresentationStyle = .popover
popover?.popoverPresentationController?.delegate = self as? UIPopoverPresentationControllerDelegate
popover?.popoverPresentationController?.sourceView = self.view
popover?.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popover?.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
popover?.dimView = self.dimView
dimView.isHidden = false
self.present(popover!, animated: false)
PopOverViewController:
class PopOverViewController: UIViewController {
var dimView:UIView?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func closeButton(_ sender: Any) {
self.dismiss(animated: false, completion: nil)
}
}

dimiss modal and return to presented childViewController in containerView

I am having a bit of an issue with dismissing a modal view presented from a childviewController in a container view. I have a UINavigationController as the rootViewController (MainNavigationController), and present a modal from one of the childViewControllers from the selectedSegmentIndex 1 (secondViewController). The modal is presented fine, but when I dismiss the modal to go back to the secondViewController(a subclass of HomeController) it returns me back to selectedIndex 0, so not the selectedIndex 1 childViewController it was presented from. I would like the modal to dismiss and return the user back to childViewController it was presented from (the secondViewController) and not return back to selectedIndex 0. Thanks in advance!
// NavigationConroller as rootViewController
class MainNavigationController: UINavigationController {
var segmentedController: UISegmentedControl!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let vc1 = TravelersFeedVC()
let vc2 = ProfileVC()
if isLoggedIn() {
// assume user is logged in
let homeController = HomeController()
viewControllers = [homeController]
homeController.firstViewController = vc1
homeController.secondViewController = vc2
} else {
perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
}
}
fileprivate func isLoggedIn() -> Bool {
return UserDefaults.standard.isLoggedIn()
}
func showLoginController() {
let loginController = LoginController()
present(loginController, animated: true, completion: {
// perhaps do something here later
})
}
}
// HomeController as parentViewController
class HomeController: UIViewController, FBSDKLoginButtonDelegate {
// child view controllers to put inside content view
var firstViewController: TravelersFeedVC?
var secondViewController: ProfileVC?
private var activeViewController: UIViewController? {
didSet {
removeInactiveViewController(inactiveViewController: oldValue)
updateActiveViewController()
}
}
private func removeInactiveViewController(inactiveViewController: UIViewController?) {
if let inActiveVC = inactiveViewController {
// call before removing child view controller's view from hierarchy
inActiveVC.willMove(toParentViewController: nil)
inActiveVC.view.removeFromSuperview()
// call after removing child view controller's view from hierarchy
inActiveVC.removeFromParentViewController()
}
}
private func updateActiveViewController() {
if let activeVC = activeViewController {
// call before adding child view controller's view as subview
addChildViewController(activeVC)
activeVC.view.frame = contentView.bounds
contentView.addSubview(activeVC.view)
// call before adding child view controller's view as subview
activeVC.didMove(toParentViewController: self)
}
}
// UI elements
lazy var contentView: UIView = {
let tv = UIView()
tv.backgroundColor = UIColor.purple
tv.translatesAutoresizingMaskIntoConstraints = false
tv.layer.masksToBounds = true
return tv
}()
var segmentedController: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
activeViewController = firstViewController
checkIfUserIsLoggedIn()
view.addSubview(contentView)
setupProfileScreen()
let items = ["Travelers", "Me"]
segmentedController = UISegmentedControl(items: items)
navigationItem.titleView = segmentedController
segmentedController.tintColor = UIColor.black
segmentedController.selectedSegmentIndex = 0
// Add function to handle Value Changed events
segmentedController.addTarget(self, action: #selector(HomeController.segmentedValueChanged(_:)), for: .valueChanged)
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(handleSignOut))
navigationItem.leftBarButtonItem?.tintColor = UIColor.black
}
// reference to collectionViewController
var travelersFeedVC: TravelersFeedVC!
func segmentedValueChanged(_ sender:UISegmentedControl!)
{
switch segmentedController.selectedSegmentIndex {
case 0:
activeViewController = firstViewController
case 1:
activeViewController = secondViewController
default: // Do nothing
break
}
}
// secondViewcontroller in containerView where modal is presented from
class ProfileVC: UIViewController {
// button to present modal
lazy var placesButton: UIButton = {
let customButton = UIButton(type: .system)
customButton.backgroundColor = UIColor.clear
// customButton.frame = CGRect(x: 150, y: 50, width: 120, height: self.view.frame.height)
customButton.setTitle("## of Places", for: .normal)
customButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
customButton.setTitleColor(.white, for: .normal)
customButton.addTarget(self, action: #selector(handleShowPlacesVC), for: .touchUpInside)
return customButton
}()
// function to call to present modal
func handleShowPlacesVC() {
let placesVC = PlacesTableVC()
let navigationController = UINavigationController(rootViewController: placesVC)
present(navigationController, animated: true, completion: nil)
}
// modal view to dismiss
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "back", style: .plain, target: self, action: #selector(handleCancel))
}
// dismiss modal view to return to secondViewController in childViewController containerView
func handleCancel() {
dismiss(animated: true, completion: nil)
}
When closing the modal dialog the viewDidAppear function in MainNavigationController is called. There you set a new homeController with it's childs. This will trigger a viewDidload in the HomeController with setting of firstViewController. Try to set a breakpoint there and you will see it.
I suggest to avoid content creation in viewDidAppear, use viewDidLoad instead.
Another hint: 'dismiss' is defined as: 'Dismisses the view controller that was presented modally by the view controller.' - If you open for instance an alert above your modal vc it closes the alert, not the modal view (self). A correct implementation has to call dismiss on the presenting controller (same controller that opened it): "presentingViewController?.dismiss()"
It works in your code because apple has implemented a fallback for the case that nothing is presented, but it's a trap that cause some headache sometime.
The chances are that although you're calling present from the child view controller, it isn't in fact handling the presentation. From the Apple docs:
The object on which you call this method may not always be the one that handles the presentation. Each presentation style has different rules governing its behavior. For example, a full-screen presentation must be made by a view controller that itself covers the entire screen. If the current view controller is unable to fulfill a request, it forwards the request up the view controller hierarchy to its nearest parent, which can then handle or forward the request.
Since you're keeping a reference of the active view controller, one solution may be to explicitly set the index upon dismissal.

move to next view controller programmatically

I wonder how to create a function that move to the login page after click login button, with code/programmatic way.
There is my code:
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let loginButton = UIButton(frame: CGRectMake(20, 640, 175, 75))
loginButton.setTitle("Login", forState: UIControlState.Normal)
loginButton.backgroundColor = UIColor.init(red: 255, green: 215, blue: 0, alpha: 0.8)
loginButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
loginButton.titleLabel!.font = UIFont(name: "StarJediSpecialEdition", size: 30)
loginButton.addTarget(self,
action: #selector(ViewController.tapActionButton),
forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(loginButton)
}
func tapActionButton(sender:UIButton!) {
print("Button is working")
}
}
There are multiple ways how you create your login page UIViewController:
From xib, for example, you can inherit your controller from this class:
class XibLoadedVC: UIViewController {
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
required init() {
print("init \(type(of: self))")
let bundle = Bundle(for: type(of: self))
super.init(nibName: String(describing: type(of: self)), bundle: bundle)
}
}
or just
let controller = LoginPageViewController(nibName: "LoginPageViewController", bundle: nil)
From storyboard:
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let loginPage = storyboard.instantiateViewControllerWithIdentifier("LoginPageViewController") as! LoginPageViewController
There are multiple ways to show controller depending on how you are create it:
You can just present controller with:
func tapActionButton(sender: UIButton!) {
let loginPage = LoginPageViewController()
self.present(loginPage, animated: true)
}
Programmatically push using navigationController:
navigationController?.pushViewController(loginPage, animated: true)
Requires UINavigationController to work. As you can’t be sure, is your controller inside navigation controller stack or not, there is optional navigationController? to not crush the app :)
Storyboard and Segues:
Create segue between your ViewController and LoginPageViewController.
Give your segue an identifier and also presentation style. For this case it will be Show
Now in your ViewController override below method
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "YourSegueIdentifier"
let loginPage = segue.destinationViewController as! LoginPageViewController
}
}
On loginButton tap action:
func tapActionButton(sender: UIButton!) {
performSegueWithIdentifier("YourSegueIdentifier", sender: nil)
}
And you are done.
P.S.
Also, there is modern SwiftUI way to do all that, you can look into tutorials and official documentation.
You need to create an instance of the next view controller and then either present or push that view controller
To present modally:
self.presentViewController(nextVC, animated: true, completion: nil)
or to push:
self.navigationController?.pushViewController(nextVC, animated: true)
all that stuff with segues apply to storyboards
Cheers!