仅在代码中viewController之间的转换

时间:2014-09-30 18:15:31

标签: ios swift viewcontroller

我想点击按钮在两个场景之间进行转换。

我有一个自定义按钮:

@IBOutlet weak var button: UIButton!

override func viewDidLoad() {

    super.viewDidLoad()

    let image = UIImage(named: "playButton.png") as UIImage

    button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
    button.frame = CGRectMake(0, 0, 100, 100)
    button.center = CGPointMake(self.view.frame.width/2, self.view.frame.height/2)

    button.addTarget(self, action: "transition:", forControlEvents: UIControlEvents.TouchUpInside)
    button .setBackgroundImage(image, forState: UIControlState.Normal)

    self.view.addSubview(button)

}

我的过渡功能

func transition(sender:UIButton!)
{
    println("Button tapped")

    let sec: GameViewController = GameViewController(nibName: nil, bundle: nil)
    self.presentViewController(sec, animated: true, completion: nil)
}

我有这个错误:“无法识别的选择器发送到实例0x7fbd71ca7270”

我不知道该怎么做?

2 个答案:

答案 0 :(得分:1)

您的按钮正在调用一种不存在的方法。见这一行:

button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

由于您的transition:方法会运行转换,因此您应将其添加为目标操作。

button.addTarget(self, action: "transition:", forControlEvents: UIControlEvents.TouchUpInside)

答案 1 :(得分:0)

func transition(sender:UIButton!)
{
    let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("GameViewController")
    self.presentViewController(VC1, animated: true, completion: nil)
}

在Storyboard中为GameViewController提供故事板ID。这必须奏效。