在Swift XCode中单击编程按钮时,如何在控制器之间切换

时间:2015-09-02 01:47:45

标签: ios xcode swift uiviewcontroller uibutton

修改

这是我当前使用编程按钮的代码。

    override func viewDidLoad() {
    super.viewDidLoad()
    self.playVideo()
    var filter = UIView()
    filter.frame = self.view.frame
    filter.backgroundColor = UIColor.orangeColor()
    filter.alpha = 0.15
    self.view.addSubview(filter)
    // Do any additional setup after loading the view, typically from a nib.

    //Add Images
    var logoImage : UIImageView
    logoImage = UIImageView(frame:CGRectMake(136, 50, 102, 104));
    logoImage.image = UIImage(named:"patchicon.png")
    self.view.addSubview(logoImage)
    var textLogo : UIImageView
    textLogo = UIImageView(frame:CGRectMake(51, 0, 273, 371));
    textLogo.image = UIImage(named:"logopatch.png")
    self.view.addSubview(textLogo)

    //Add Buttons
    let buttonLogin = UIButton.buttonWithType(.Custom) as! UIButton
    buttonLogin.frame = CGRectMake(16, 519, 343, 60)
    buttonLogin.layer.cornerRadius = 0.2 * buttonLogin.bounds.size.width
    buttonLogin.addTarget(self, action: "buttonTouched", forControlEvents: UIControlEvents.TouchUpInside)
    buttonLogin.setImage(UIImage(named:"loginButton.png"), forState: .Normal)
    view.addSubview(buttonLogin)

    let buttonRegister = UIButton.buttonWithType(.Custom) as! UIButton
    buttonRegister.frame = CGRectMake(16, 587, 343, 60)
    buttonRegister.layer.cornerRadius = 0.2 * buttonRegister.bounds.size.width
    buttonRegister.addTarget(self, action: "buttonTouched2", forControlEvents: UIControlEvents.TouchUpInside)
    buttonRegister.setImage(UIImage(named:"registerButton.png"), forState: .Normal)
    view.addSubview(buttonRegister)


}
func buttonTouched()
{
    performSegueWithIdentifier("loginTouched", sender: nil)
}
func buttonTouched2()
{
    performSegueWithIdentifier("registerTouched", sender: nil)
}

这就是我的故事板的样子: http://i.stack.imgur.com/MlEhI.png

如何进一步编程这些按钮以切换到新的视图控制器?例如,我想点击注册按钮,我希望当前的MainViewController切换到一个新的,它将具有供用户注册的信息,等等...... 谢谢!

3 个答案:

答案 0 :(得分:1)

首先,您需要向按钮添加目标:

buttonLogin.addTarget(self, action: "buttonTouched", forControlEvents: UIControlEvents.TouchUpInside)

然后,添加此功能:

func buttonTouched()
{
   println("touched")
}

要切换到新的视图控制器take a look at this

答案 1 :(得分:0)

将目标添加到按钮..

buttonLogin.addTarget(self, action: "btnpressed:", forControlEvents: .TouchUpInside)

Vtwot是我的第二个viewController,它的Storyboard Id是ViewTwoId ..

使UIButton像这样行动..

func btnpressed(sender: UIButton!) 
{
    let jj = self.storyboard!.instantiateViewControllerWithIdentifier("ViewTwoId") as! Vtwot
    self.navigationController!.pushViewController(jj, animated: true)
}

我希望它有所帮助......

答案 2 :(得分:0)

好吧,切换到" new"在视图控制器中,您需要首先在故事板中创建该视图控制器。之后,您是否看到将主视图控制器链接到其他两个的那条线?单击该行并转到"属性检查器"。在标识符空白处填写您想要的任何内容,然后在您以编程方式创建的按钮中,您需要设置 this

performSegueWithIdentifier("whateverYouWant", sender: self)

哦,不要忘记这一步:选择主视图控制器,转到编辑器>嵌入>导航控制器。如果您不这样做,您的应用程序将崩溃。

希望有所帮助!

编辑:试试这个:

在viewDidLoad方法之外写一些这样的函数:

func segueToNextVC(sender: UIButton!) {
println("Button pressed, let's see what happens!")
performSegueWithIdentifier("whateverYouWant", sender: self)
}

然后,在viewDidLoad中的按钮中,执行以下操作:

buttonLogin.addTarget(self, action: "segueToNextVC:", forControlEvents: UIControlEvents.TouchUpInside)

注意:不要忘记segueToNextVC中的,这很重要!