2016-12-29 2 views
0

Ich habe eine IBAction in meinem ViewControllerA, die ich in ViewControllerB verwenden möchte, wie kann ich sie implementieren? Wenn ich den Code ausführen und auf die Abmeldeschaltfläche tippen, wird der Durchlaufbildschirm nicht angezeigt. HierVerwendung einer IBAction in einer anderen Ansicht Swift

ist der Code für 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) 
    } 

Und hier ist der Code für 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") 
     } 
    } 

} 

Antwort

0

Es mir aussieht wie du nie gesetzt View-Controller Ein Delegierter. Wenn es null ist, dann wird die Linie

delegate?.walkThroughButtonTapped!() 

... nichts tun. (Die ? bedeutet "Wenn der optionale 'Delegate' nicht Null ist, senden Sie ihm die folgende Nachricht. Wenn es Null ist, tun Sie nichts.")

+0

Wie setze ich den Delegaten von View Controller A? Ich dachte, ich hätte mit diesen Linien – user2135480

+0

Diese Zeilen? Welche Linien? –

Verwandte Themen