Delegate returns nil and not being called












3















I have an overlay that I want to remove when a button is clicked thereby dismissing the ViewController. I have debugged and the delegate is currently returning nil. I'm not sure what I'm doing wrong here. Have I missed implementing something else? I have even tried out print statements but can't see anything.



protocol DismissOverlayDelegate: class {
func dismissOverlay(_ sender: PlayersViewController)
}

class PlayersViewController: UIViewController {

weak var delegate: DismissOverlayDelegate?

@IBAction func getStartedTapped(_ sender: UIButton) {
self.delegate?.dismissOverlay(self)
}
}


and in my ViewController where I implement the delegate method



class HomeViewController: UIViewController, DismissOverlayDelegate {

@IBOutlet weak var customOverlay: CustomOverlayView!

let playersViewController = PlayersViewController()

override func viewDidLoad() {
super.viewDidLoad()
self.playersViewController.delegate = self
}

func dismissOverlay(_ sender: PlayersViewController) {
self.customOverlay.removeFromView()
}
}




delegate = (DismissOverlayDelegate?) nil





The PlayersViewController is embedded into a UIPageViewController



class HomeViewController: UIPageViewController, UIPageViewControllerDataSource {

var pages = [UIViewController]()

override func viewDidLoad() {
super.viewDidLoad()
for storyboardIDs in ["playersVC1","playersVC2"] {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: storyboardIDs)
self.pages.append(viewController!)
}
self.dataSource = self
self.setViewControllers([self.pages.first!], direction: .forward, animated: true)
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = self.pages.firstIndex(of: viewController)!
if currentIndex > 0 {
return self.pages[currentIndex - 1]
}
return nil
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = self.pages.firstIndex(of: viewController)!
if currentIndex < (self.pages.count - 1) {
return self.pages[currentIndex + 1]
}
return nil
}
}









share|improve this question

























  • your instance playersViewController is not the instance that is on-screen, you need to get hold of the actual instance in the view controller stack. You cannot just create an instance using PlayersViewController().

    – luk2302
    Nov 23 '18 at 12:06











  • how are you using playersViewController further in your code?

    – hardik parmar
    Nov 23 '18 at 12:07











  • Its just a help viewController that I want to get rid of when button is pressed. Remove it from the superView

    – mandem112
    Nov 23 '18 at 12:14






  • 1





    how you are adding the playersViewController? show that code to us.

    – hardik parmar
    Nov 23 '18 at 12:21











  • Its embedded in a UIPageViewController

    – mandem112
    Nov 23 '18 at 12:25
















3















I have an overlay that I want to remove when a button is clicked thereby dismissing the ViewController. I have debugged and the delegate is currently returning nil. I'm not sure what I'm doing wrong here. Have I missed implementing something else? I have even tried out print statements but can't see anything.



protocol DismissOverlayDelegate: class {
func dismissOverlay(_ sender: PlayersViewController)
}

class PlayersViewController: UIViewController {

weak var delegate: DismissOverlayDelegate?

@IBAction func getStartedTapped(_ sender: UIButton) {
self.delegate?.dismissOverlay(self)
}
}


and in my ViewController where I implement the delegate method



class HomeViewController: UIViewController, DismissOverlayDelegate {

@IBOutlet weak var customOverlay: CustomOverlayView!

let playersViewController = PlayersViewController()

override func viewDidLoad() {
super.viewDidLoad()
self.playersViewController.delegate = self
}

func dismissOverlay(_ sender: PlayersViewController) {
self.customOverlay.removeFromView()
}
}




delegate = (DismissOverlayDelegate?) nil





The PlayersViewController is embedded into a UIPageViewController



class HomeViewController: UIPageViewController, UIPageViewControllerDataSource {

var pages = [UIViewController]()

override func viewDidLoad() {
super.viewDidLoad()
for storyboardIDs in ["playersVC1","playersVC2"] {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: storyboardIDs)
self.pages.append(viewController!)
}
self.dataSource = self
self.setViewControllers([self.pages.first!], direction: .forward, animated: true)
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = self.pages.firstIndex(of: viewController)!
if currentIndex > 0 {
return self.pages[currentIndex - 1]
}
return nil
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = self.pages.firstIndex(of: viewController)!
if currentIndex < (self.pages.count - 1) {
return self.pages[currentIndex + 1]
}
return nil
}
}









share|improve this question

























  • your instance playersViewController is not the instance that is on-screen, you need to get hold of the actual instance in the view controller stack. You cannot just create an instance using PlayersViewController().

    – luk2302
    Nov 23 '18 at 12:06











  • how are you using playersViewController further in your code?

    – hardik parmar
    Nov 23 '18 at 12:07











  • Its just a help viewController that I want to get rid of when button is pressed. Remove it from the superView

    – mandem112
    Nov 23 '18 at 12:14






  • 1





    how you are adding the playersViewController? show that code to us.

    – hardik parmar
    Nov 23 '18 at 12:21











  • Its embedded in a UIPageViewController

    – mandem112
    Nov 23 '18 at 12:25














3












3








3








I have an overlay that I want to remove when a button is clicked thereby dismissing the ViewController. I have debugged and the delegate is currently returning nil. I'm not sure what I'm doing wrong here. Have I missed implementing something else? I have even tried out print statements but can't see anything.



protocol DismissOverlayDelegate: class {
func dismissOverlay(_ sender: PlayersViewController)
}

class PlayersViewController: UIViewController {

weak var delegate: DismissOverlayDelegate?

@IBAction func getStartedTapped(_ sender: UIButton) {
self.delegate?.dismissOverlay(self)
}
}


and in my ViewController where I implement the delegate method



class HomeViewController: UIViewController, DismissOverlayDelegate {

@IBOutlet weak var customOverlay: CustomOverlayView!

let playersViewController = PlayersViewController()

override func viewDidLoad() {
super.viewDidLoad()
self.playersViewController.delegate = self
}

func dismissOverlay(_ sender: PlayersViewController) {
self.customOverlay.removeFromView()
}
}




delegate = (DismissOverlayDelegate?) nil





The PlayersViewController is embedded into a UIPageViewController



class HomeViewController: UIPageViewController, UIPageViewControllerDataSource {

var pages = [UIViewController]()

override func viewDidLoad() {
super.viewDidLoad()
for storyboardIDs in ["playersVC1","playersVC2"] {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: storyboardIDs)
self.pages.append(viewController!)
}
self.dataSource = self
self.setViewControllers([self.pages.first!], direction: .forward, animated: true)
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = self.pages.firstIndex(of: viewController)!
if currentIndex > 0 {
return self.pages[currentIndex - 1]
}
return nil
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = self.pages.firstIndex(of: viewController)!
if currentIndex < (self.pages.count - 1) {
return self.pages[currentIndex + 1]
}
return nil
}
}









share|improve this question
















I have an overlay that I want to remove when a button is clicked thereby dismissing the ViewController. I have debugged and the delegate is currently returning nil. I'm not sure what I'm doing wrong here. Have I missed implementing something else? I have even tried out print statements but can't see anything.



protocol DismissOverlayDelegate: class {
func dismissOverlay(_ sender: PlayersViewController)
}

class PlayersViewController: UIViewController {

weak var delegate: DismissOverlayDelegate?

@IBAction func getStartedTapped(_ sender: UIButton) {
self.delegate?.dismissOverlay(self)
}
}


and in my ViewController where I implement the delegate method



class HomeViewController: UIViewController, DismissOverlayDelegate {

@IBOutlet weak var customOverlay: CustomOverlayView!

let playersViewController = PlayersViewController()

override func viewDidLoad() {
super.viewDidLoad()
self.playersViewController.delegate = self
}

func dismissOverlay(_ sender: PlayersViewController) {
self.customOverlay.removeFromView()
}
}




delegate = (DismissOverlayDelegate?) nil





The PlayersViewController is embedded into a UIPageViewController



class HomeViewController: UIPageViewController, UIPageViewControllerDataSource {

var pages = [UIViewController]()

override func viewDidLoad() {
super.viewDidLoad()
for storyboardIDs in ["playersVC1","playersVC2"] {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: storyboardIDs)
self.pages.append(viewController!)
}
self.dataSource = self
self.setViewControllers([self.pages.first!], direction: .forward, animated: true)
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = self.pages.firstIndex(of: viewController)!
if currentIndex > 0 {
return self.pages[currentIndex - 1]
}
return nil
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = self.pages.firstIndex(of: viewController)!
if currentIndex < (self.pages.count - 1) {
return self.pages[currentIndex + 1]
}
return nil
}
}






ios swift delegates overlay delegation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 12:47







mandem112

















asked Nov 23 '18 at 12:01









mandem112mandem112

818




818













  • your instance playersViewController is not the instance that is on-screen, you need to get hold of the actual instance in the view controller stack. You cannot just create an instance using PlayersViewController().

    – luk2302
    Nov 23 '18 at 12:06











  • how are you using playersViewController further in your code?

    – hardik parmar
    Nov 23 '18 at 12:07











  • Its just a help viewController that I want to get rid of when button is pressed. Remove it from the superView

    – mandem112
    Nov 23 '18 at 12:14






  • 1





    how you are adding the playersViewController? show that code to us.

    – hardik parmar
    Nov 23 '18 at 12:21











  • Its embedded in a UIPageViewController

    – mandem112
    Nov 23 '18 at 12:25



















  • your instance playersViewController is not the instance that is on-screen, you need to get hold of the actual instance in the view controller stack. You cannot just create an instance using PlayersViewController().

    – luk2302
    Nov 23 '18 at 12:06











  • how are you using playersViewController further in your code?

    – hardik parmar
    Nov 23 '18 at 12:07











  • Its just a help viewController that I want to get rid of when button is pressed. Remove it from the superView

    – mandem112
    Nov 23 '18 at 12:14






  • 1





    how you are adding the playersViewController? show that code to us.

    – hardik parmar
    Nov 23 '18 at 12:21











  • Its embedded in a UIPageViewController

    – mandem112
    Nov 23 '18 at 12:25

















your instance playersViewController is not the instance that is on-screen, you need to get hold of the actual instance in the view controller stack. You cannot just create an instance using PlayersViewController().

– luk2302
Nov 23 '18 at 12:06





your instance playersViewController is not the instance that is on-screen, you need to get hold of the actual instance in the view controller stack. You cannot just create an instance using PlayersViewController().

– luk2302
Nov 23 '18 at 12:06













how are you using playersViewController further in your code?

– hardik parmar
Nov 23 '18 at 12:07





how are you using playersViewController further in your code?

– hardik parmar
Nov 23 '18 at 12:07













Its just a help viewController that I want to get rid of when button is pressed. Remove it from the superView

– mandem112
Nov 23 '18 at 12:14





Its just a help viewController that I want to get rid of when button is pressed. Remove it from the superView

– mandem112
Nov 23 '18 at 12:14




1




1





how you are adding the playersViewController? show that code to us.

– hardik parmar
Nov 23 '18 at 12:21





how you are adding the playersViewController? show that code to us.

– hardik parmar
Nov 23 '18 at 12:21













Its embedded in a UIPageViewController

– mandem112
Nov 23 '18 at 12:25





Its embedded in a UIPageViewController

– mandem112
Nov 23 '18 at 12:25












2 Answers
2






active

oldest

votes


















0














It seems that PlayersViewController() is wrong.
You should use instantiateInitialViewController() or instantiateInitialViewController() when using StoryBoard.



https://developer.apple.com/documentation/uikit/uistoryboard/1616214-instantiateviewcontroller



https://developer.apple.com/documentation/uikit/uistoryboard/1616213-instantiateinitialviewcontroller






share|improve this answer
























  • This works but it just presents the HomeViewCotroller on top which is what I don't what. I just wanna press the button to make the overlay just disappear without presenting another viewController

    – mandem112
    Nov 23 '18 at 12:30



















0














Let me explain you if you are using or want to use delegate for call or implement something there is root and you have to that on a right way. You are doing well no issue but the implementation of delegate is not properly your method can't accept that so try this:-



Your Home View Controller



class HomeViewController: UIViewController, DismissOverlayDelegate {

@IBOutlet weak var customOverlay: UIView!

override func viewDidLoad() {
super.viewDidLoad()
}

func dismissOverlay(_ sender: PlayersViewController) {
self.customOverlay.removeFromSuperview()
}

@IBAction func go(_ sender: UIButton) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "PlayersViewController") as! PlayersViewController
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true);
}
}


this is your playground VC



protocol DismissOverlayDelegate: class {
func dismissOverlay(_ sender: PlayersViewController)
}

class PlayersViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

weak var delegate: DismissOverlayDelegate?

@IBAction func getStartedTapped(_ sender: UIButton) {
self.delegate?.dismissOverlay(self)
}

}


now hope you understand what you have to do...!






share|improve this answer
























  • this looks great, but where would I use the @IBAction func go(_ sender: UIButton) {}. I understand instantiating it but I don't have the button go button in the HomeViewController

    – mandem112
    Nov 23 '18 at 12:51











  • seems you edited your question

    – Naveen Yadav
    Nov 23 '18 at 12:58











  • yeah I did by adding it the code that embeds the vcs into a UIPageViewController

    – mandem112
    Nov 23 '18 at 12:59











  • can you explain exactly what you want

    – Naveen Yadav
    Nov 23 '18 at 13:05













  • I have a UIBarbuttonItem that shows these viewControllers as help pages in an overlay. I have a button on the on the last viewController that I want to tap to remove it the overlay hence removing it from the superview

    – mandem112
    Nov 23 '18 at 13:07














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446362%2fdelegate-returns-nil-and-not-being-called%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














It seems that PlayersViewController() is wrong.
You should use instantiateInitialViewController() or instantiateInitialViewController() when using StoryBoard.



https://developer.apple.com/documentation/uikit/uistoryboard/1616214-instantiateviewcontroller



https://developer.apple.com/documentation/uikit/uistoryboard/1616213-instantiateinitialviewcontroller






share|improve this answer
























  • This works but it just presents the HomeViewCotroller on top which is what I don't what. I just wanna press the button to make the overlay just disappear without presenting another viewController

    – mandem112
    Nov 23 '18 at 12:30
















0














It seems that PlayersViewController() is wrong.
You should use instantiateInitialViewController() or instantiateInitialViewController() when using StoryBoard.



https://developer.apple.com/documentation/uikit/uistoryboard/1616214-instantiateviewcontroller



https://developer.apple.com/documentation/uikit/uistoryboard/1616213-instantiateinitialviewcontroller






share|improve this answer
























  • This works but it just presents the HomeViewCotroller on top which is what I don't what. I just wanna press the button to make the overlay just disappear without presenting another viewController

    – mandem112
    Nov 23 '18 at 12:30














0












0








0







It seems that PlayersViewController() is wrong.
You should use instantiateInitialViewController() or instantiateInitialViewController() when using StoryBoard.



https://developer.apple.com/documentation/uikit/uistoryboard/1616214-instantiateviewcontroller



https://developer.apple.com/documentation/uikit/uistoryboard/1616213-instantiateinitialviewcontroller






share|improve this answer













It seems that PlayersViewController() is wrong.
You should use instantiateInitialViewController() or instantiateInitialViewController() when using StoryBoard.



https://developer.apple.com/documentation/uikit/uistoryboard/1616214-instantiateviewcontroller



https://developer.apple.com/documentation/uikit/uistoryboard/1616213-instantiateinitialviewcontroller







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 12:21









Shota NakagamiShota Nakagami

32448




32448













  • This works but it just presents the HomeViewCotroller on top which is what I don't what. I just wanna press the button to make the overlay just disappear without presenting another viewController

    – mandem112
    Nov 23 '18 at 12:30



















  • This works but it just presents the HomeViewCotroller on top which is what I don't what. I just wanna press the button to make the overlay just disappear without presenting another viewController

    – mandem112
    Nov 23 '18 at 12:30

















This works but it just presents the HomeViewCotroller on top which is what I don't what. I just wanna press the button to make the overlay just disappear without presenting another viewController

– mandem112
Nov 23 '18 at 12:30





This works but it just presents the HomeViewCotroller on top which is what I don't what. I just wanna press the button to make the overlay just disappear without presenting another viewController

– mandem112
Nov 23 '18 at 12:30













0














Let me explain you if you are using or want to use delegate for call or implement something there is root and you have to that on a right way. You are doing well no issue but the implementation of delegate is not properly your method can't accept that so try this:-



Your Home View Controller



class HomeViewController: UIViewController, DismissOverlayDelegate {

@IBOutlet weak var customOverlay: UIView!

override func viewDidLoad() {
super.viewDidLoad()
}

func dismissOverlay(_ sender: PlayersViewController) {
self.customOverlay.removeFromSuperview()
}

@IBAction func go(_ sender: UIButton) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "PlayersViewController") as! PlayersViewController
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true);
}
}


this is your playground VC



protocol DismissOverlayDelegate: class {
func dismissOverlay(_ sender: PlayersViewController)
}

class PlayersViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

weak var delegate: DismissOverlayDelegate?

@IBAction func getStartedTapped(_ sender: UIButton) {
self.delegate?.dismissOverlay(self)
}

}


now hope you understand what you have to do...!






share|improve this answer
























  • this looks great, but where would I use the @IBAction func go(_ sender: UIButton) {}. I understand instantiating it but I don't have the button go button in the HomeViewController

    – mandem112
    Nov 23 '18 at 12:51











  • seems you edited your question

    – Naveen Yadav
    Nov 23 '18 at 12:58











  • yeah I did by adding it the code that embeds the vcs into a UIPageViewController

    – mandem112
    Nov 23 '18 at 12:59











  • can you explain exactly what you want

    – Naveen Yadav
    Nov 23 '18 at 13:05













  • I have a UIBarbuttonItem that shows these viewControllers as help pages in an overlay. I have a button on the on the last viewController that I want to tap to remove it the overlay hence removing it from the superview

    – mandem112
    Nov 23 '18 at 13:07


















0














Let me explain you if you are using or want to use delegate for call or implement something there is root and you have to that on a right way. You are doing well no issue but the implementation of delegate is not properly your method can't accept that so try this:-



Your Home View Controller



class HomeViewController: UIViewController, DismissOverlayDelegate {

@IBOutlet weak var customOverlay: UIView!

override func viewDidLoad() {
super.viewDidLoad()
}

func dismissOverlay(_ sender: PlayersViewController) {
self.customOverlay.removeFromSuperview()
}

@IBAction func go(_ sender: UIButton) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "PlayersViewController") as! PlayersViewController
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true);
}
}


this is your playground VC



protocol DismissOverlayDelegate: class {
func dismissOverlay(_ sender: PlayersViewController)
}

class PlayersViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

weak var delegate: DismissOverlayDelegate?

@IBAction func getStartedTapped(_ sender: UIButton) {
self.delegate?.dismissOverlay(self)
}

}


now hope you understand what you have to do...!






share|improve this answer
























  • this looks great, but where would I use the @IBAction func go(_ sender: UIButton) {}. I understand instantiating it but I don't have the button go button in the HomeViewController

    – mandem112
    Nov 23 '18 at 12:51











  • seems you edited your question

    – Naveen Yadav
    Nov 23 '18 at 12:58











  • yeah I did by adding it the code that embeds the vcs into a UIPageViewController

    – mandem112
    Nov 23 '18 at 12:59











  • can you explain exactly what you want

    – Naveen Yadav
    Nov 23 '18 at 13:05













  • I have a UIBarbuttonItem that shows these viewControllers as help pages in an overlay. I have a button on the on the last viewController that I want to tap to remove it the overlay hence removing it from the superview

    – mandem112
    Nov 23 '18 at 13:07
















0












0








0







Let me explain you if you are using or want to use delegate for call or implement something there is root and you have to that on a right way. You are doing well no issue but the implementation of delegate is not properly your method can't accept that so try this:-



Your Home View Controller



class HomeViewController: UIViewController, DismissOverlayDelegate {

@IBOutlet weak var customOverlay: UIView!

override func viewDidLoad() {
super.viewDidLoad()
}

func dismissOverlay(_ sender: PlayersViewController) {
self.customOverlay.removeFromSuperview()
}

@IBAction func go(_ sender: UIButton) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "PlayersViewController") as! PlayersViewController
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true);
}
}


this is your playground VC



protocol DismissOverlayDelegate: class {
func dismissOverlay(_ sender: PlayersViewController)
}

class PlayersViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

weak var delegate: DismissOverlayDelegate?

@IBAction func getStartedTapped(_ sender: UIButton) {
self.delegate?.dismissOverlay(self)
}

}


now hope you understand what you have to do...!






share|improve this answer













Let me explain you if you are using or want to use delegate for call or implement something there is root and you have to that on a right way. You are doing well no issue but the implementation of delegate is not properly your method can't accept that so try this:-



Your Home View Controller



class HomeViewController: UIViewController, DismissOverlayDelegate {

@IBOutlet weak var customOverlay: UIView!

override func viewDidLoad() {
super.viewDidLoad()
}

func dismissOverlay(_ sender: PlayersViewController) {
self.customOverlay.removeFromSuperview()
}

@IBAction func go(_ sender: UIButton) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "PlayersViewController") as! PlayersViewController
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true);
}
}


this is your playground VC



protocol DismissOverlayDelegate: class {
func dismissOverlay(_ sender: PlayersViewController)
}

class PlayersViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

weak var delegate: DismissOverlayDelegate?

@IBAction func getStartedTapped(_ sender: UIButton) {
self.delegate?.dismissOverlay(self)
}

}


now hope you understand what you have to do...!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 12:46









Naveen YadavNaveen Yadav

5318




5318













  • this looks great, but where would I use the @IBAction func go(_ sender: UIButton) {}. I understand instantiating it but I don't have the button go button in the HomeViewController

    – mandem112
    Nov 23 '18 at 12:51











  • seems you edited your question

    – Naveen Yadav
    Nov 23 '18 at 12:58











  • yeah I did by adding it the code that embeds the vcs into a UIPageViewController

    – mandem112
    Nov 23 '18 at 12:59











  • can you explain exactly what you want

    – Naveen Yadav
    Nov 23 '18 at 13:05













  • I have a UIBarbuttonItem that shows these viewControllers as help pages in an overlay. I have a button on the on the last viewController that I want to tap to remove it the overlay hence removing it from the superview

    – mandem112
    Nov 23 '18 at 13:07





















  • this looks great, but where would I use the @IBAction func go(_ sender: UIButton) {}. I understand instantiating it but I don't have the button go button in the HomeViewController

    – mandem112
    Nov 23 '18 at 12:51











  • seems you edited your question

    – Naveen Yadav
    Nov 23 '18 at 12:58











  • yeah I did by adding it the code that embeds the vcs into a UIPageViewController

    – mandem112
    Nov 23 '18 at 12:59











  • can you explain exactly what you want

    – Naveen Yadav
    Nov 23 '18 at 13:05













  • I have a UIBarbuttonItem that shows these viewControllers as help pages in an overlay. I have a button on the on the last viewController that I want to tap to remove it the overlay hence removing it from the superview

    – mandem112
    Nov 23 '18 at 13:07



















this looks great, but where would I use the @IBAction func go(_ sender: UIButton) {}. I understand instantiating it but I don't have the button go button in the HomeViewController

– mandem112
Nov 23 '18 at 12:51





this looks great, but where would I use the @IBAction func go(_ sender: UIButton) {}. I understand instantiating it but I don't have the button go button in the HomeViewController

– mandem112
Nov 23 '18 at 12:51













seems you edited your question

– Naveen Yadav
Nov 23 '18 at 12:58





seems you edited your question

– Naveen Yadav
Nov 23 '18 at 12:58













yeah I did by adding it the code that embeds the vcs into a UIPageViewController

– mandem112
Nov 23 '18 at 12:59





yeah I did by adding it the code that embeds the vcs into a UIPageViewController

– mandem112
Nov 23 '18 at 12:59













can you explain exactly what you want

– Naveen Yadav
Nov 23 '18 at 13:05







can you explain exactly what you want

– Naveen Yadav
Nov 23 '18 at 13:05















I have a UIBarbuttonItem that shows these viewControllers as help pages in an overlay. I have a button on the on the last viewController that I want to tap to remove it the overlay hence removing it from the superview

– mandem112
Nov 23 '18 at 13:07







I have a UIBarbuttonItem that shows these viewControllers as help pages in an overlay. I have a button on the on the last viewController that I want to tap to remove it the overlay hence removing it from the superview

– mandem112
Nov 23 '18 at 13:07




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446362%2fdelegate-returns-nil-and-not-being-called%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini