我在ViewControllerA中有一个IBAction,我试图在ViewControllerB中使用它,我该如何实现它? 当我运行代码并点击退出按钮时,不会显示演练屏幕。
以下是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)
}
这是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")
}
}
}
答案 0 :(得分:0)
在我看来,你永远不会设置视图控制器A的委托。如果它没有,则行
delegate?.walkThroughButtonTapped!()
...不会做任何事情。 (?
表示"如果可选的'代表'不是nil,请将以下消息发送给它。如果没有,请不要做任何事情。&#39} #34)