I have an IBAction in my ViewControllerA that I am trying to use in ViewControllerB, how can I implement it? When I run the code and tap on the sign out button, the walkthrough screen does not display.
Here is the code for ViewControllerA
@objc protocol walkThroughViewControllerDelegate{
@objc optional func walkThroughButtonTapped()
}
class walkThroughViewController: UIViewController, BWWalkthroughViewControllerDelegate {
var delegate: walkThroughViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
let userId = NSUserDefaults().stringForKey("userId")
if(userId != nil){
print ("user is signed in")
} else{
print ("user is nil show walkthrough")
walkThroughButtonTapped()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func walkThroughButtonTapped() {
delegate?.walkThroughButtonTapped!()
let stb = UIStoryboard(name: "Main", bundle: nil)
let walkthrough = stb.instantiateViewControllerWithIdentifier("walk0") as! BWWalkthroughViewController
let page_one = stb.instantiateViewControllerWithIdentifier("walk1") as UIViewController
let page_two = stb.instantiateViewControllerWithIdentifier("walk2") as UIViewController
let page_three = stb.instantiateViewControllerWithIdentifier("walk3") as UIViewController
// Attach the pages to the master
walkthrough.delegate = self
walkthrough.addViewController(page_one)
walkthrough.addViewController(page_two)
walkthrough.addViewController(page_three)
self.presentViewController(walkthrough, animated: true, completion: nil)
}
func walkthroughCloseButtonPressed() {
print("closeButtonPrt")
self.dismissViewControllerAnimated(true, completion: nil)
}
And here is the code for ViewControllerB
class LeftSideViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, walkThroughViewControllerDelegate {
var menuItems: [String] = ["Main", "About", "Sign Out"]
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.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuItems.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var myCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! UITableViewCell
myCell.textLabel?.text = menuItems[indexPath.row]
return myCell
}
func tableView(tableVIew: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
switch(indexPath.row)
{
case 0:
var mainPageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("MainPageViewController") as! MainPageViewController
var mainPageNav = UINavigationController(rootViewController:mainPageViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.drawerContainer!.centerViewController = mainPageNav
appDelegate.drawerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
break
case 1:
var aboutViewController = self.storyboard?.instantiateViewControllerWithIdentifier("AboutViewController") as! AboutViewController
var aboutPageNav = UINavigationController(rootViewController:aboutViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.drawerContainer!.centerViewController = aboutPageNav
appDelegate.drawerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
break
case 2:
NSUserDefaults.standardUserDefaults().removeObjectForKey("userFirstName")
NSUserDefaults.standardUserDefaults().removeObjectForKey("userLastName")
NSUserDefaults.standardUserDefaults().removeObjectForKey("userId")
NSUserDefaults.standardUserDefaults().synchronize()
func walkThroughButtonTapped(){
print("worked")
}
/*
let signInPage = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
let signInNav = UINavigationController(rootVixewController: signInPage)
let appDelegate = UIApplication.sharedApplication().delegate
appDelegate?.window??.rootViewController = signInNav
*/
break
default:
print("Not handled")
}
}
}
It looks to me like you never set view controller A's delegate. If it's nil then the line
...won't do anything. (The
?means "If the optional 'delegate' is not nil, send it the following message. If it's nil, don't do anything.")