如何在uiviewcontroller中使用协议,swift

时间:2016-05-10 12:36:03

标签: ios swift delegates protocols

如何使用协议?我无法使用它。我只想打印出来" abc"当我点击开始按钮。它没有用。请帮助我

import UIKit
import SpriteKit


protocol GameDelegate {
    func gameOver()
}

class MainMenuScene: SKScene
{
    var Gdelegate: GameDelegate?

    override func didMoveToView(view: SKView)
    {
        let background = SKSpriteNode(imageNamed: "DiscsBackground")
        background.size = self.size
        background.zPosition = 0
        background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
        self.addChild(background)

        let gameTitleLabel1 = SKLabelNode(fontNamed: "Pusab")
        gameTitleLabel1.text = "Disappearing"
        gameTitleLabel1.fontSize = 90
        gameTitleLabel1.fontColor = SKColor.whiteColor()
        gameTitleLabel1.position = CGPoint(x: self.size.width/2, y: self.size.height*0.75)
        gameTitleLabel1.zPosition = 1
        self.addChild(gameTitleLabel1)

        let gameTitleLabel2 = SKLabelNode(fontNamed: "Pusab")
        gameTitleLabel2.text = "Discs"
        gameTitleLabel2.fontSize = 250
        gameTitleLabel2.fontColor = SKColor.whiteColor()
        gameTitleLabel2.position = CGPoint(x: self.size.width/2, y: self.size.height*0.6)
        gameTitleLabel2.zPosition = 1
        self.addChild(gameTitleLabel2)

        let gameByLabel = SKLabelNode(fontNamed: "Pusab")
        gameByLabel.text = "Disappearing"
        gameByLabel.fontSize = 90
        gameByLabel.fontColor = SKColor.whiteColor()
        gameTitleLabel2.position = CGPoint(x: self.size.width/2, y: self.size.height*0.95)
        gameByLabel.zPosition = 1
        self.addChild(gameByLabel)


        let startLabel = SKLabelNode(fontNamed: "Pusab")
        startLabel.text = "start"
        startLabel.fontSize = 150
        startLabel.fontColor = SKColor.whiteColor()
        startLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.35)
        startLabel.zPosition = 1
        startLabel.name = "startButton"
        self.addChild(startLabel)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        for touch: AnyObject in touches
        {

            let pointOfTouch = touch.locationInNode(self)
            let tappedNode = nodeAtPoint(pointOfTouch)
            let tappedNodeName = tappedNode.name

            if tappedNodeName == "startButton"
            {
                //self.scene?.view?.presentScene(nil)
                Gdelegate?.gameOver()
            }
        }


    }

}

class MHAGameViewController: UIViewController,GameDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let scene = MainMenuScene(size: CGSize(width: 1536, height: 2048))
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)

    }

    func gameOver() {/*
        self.performSegueWithIdentifier("segue1", sender: self)
        let alertController = UIAlertController(title: "HELP", message:
            "Please wait....", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "close", style: UIAlertActionStyle.Default,handler: nil))

        self.presentViewController(alertController, animated: true, completion: nil)*/

        print("abc")

    }


    override func shouldAutorotate() -> Bool {
        return true
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            return .AllButUpsideDown
        } else {
            return .All
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

2 个答案:

答案 0 :(得分:1)

您应该将代理设置如下:

let scene = MainMenuScene(size: CGSize(width: 1536, height: 2048))
scene.Gdelegate = self

答案 1 :(得分:0)

步骤:1-在类声明之前和在Import stmt下面声明协议。 (单元格内部)

    protocol BtnsAction: NSObjectProtocol
{
    func btnLearnAction(_ sender: Any?)
}
  

weak var delegate: BtnsAction?
  

步骤:2-在单元格类中声明按钮操作方法。

@IBAction func btnLearnMoreAction(_ sender: UIButton) {
    self.delegate?.btnLearnAction(self)
}
  

步骤:3-现在,在ViewController内部。

class MyViewController_VC:ViewController, BtnsAction
  

 //MARK: - Protocol Method -
func btnLearnAction(_ sender: Any?) {
    let cell = sender as! SubCategoryCell
    let indexpath = self.TableView1.indexPath(for: cell)
    let dictData = self.arrFree_Listing[(indexpath?.row)!] as! NSMutableDictionary

    let strId = String(dictData.value(forKey: "id") as! Int)
    let detailVC = self.storyboard?.instantiateViewController(withIdentifier: "ListingDetails_InnerVC") as! ListingDetails_InnerVC
    detailVC.strID = strId
    self.navigationController?.pushViewController(detailVC, animated: true)
}