0

EDIT

This is my current code with the programmed buttons.

    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)
}

This is what my Storyboard looks like: https://i.stack.imgur.com/MlEhI.png

How can I program these buttons further to switch into new view controllers? For instance, I want to click the register button and I want the current MainViewController to switch to a new one that will have the information for the user to sign up and so forth... Thanks!

ermalsh
  • 191
  • 1
  • 16

3 Answers3

1

First, you need to add a target to the button:

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

And then, add this function:

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

To switch to a new view controller, take a look at this

Community
  • 1
  • 1
Lucas Farah
  • 1,022
  • 10
  • 24
0

add target to your button..

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

Vtwot is my Second viewController and its Storyboard Id is ViewTwoId..

make UIButtons action like these..

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

i hope it helps..

krushnsinh
  • 874
  • 1
  • 10
  • 11
0

Well, to switch to a "new" view controller, you need to first create that view controller in your storyboard. After that, do you see that line that links the main view controller to the other two? Click that line and go to "Attributes Inspector". In the identifier blank space, fill with whatever you want and then, in the button that you created programmatically, you need to set this:

performSegueWithIdentifier("whateverYouWant", sender: self)

Oh, and don't forget this step: select your main view controller, go to Editor > Embed In > Navigation Controller. If you don't do this, your application will crash.

Hope that helps!

EDIT: Try this:

Write some function like this outside the viewDidLoad method:

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

Then, in your button in viewDidLoad, do like this:

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

Note: Don't forget the : in segueToNextVC:, that's important!

Nicholas Piccoli
  • 188
  • 1
  • 2
  • 14
  • Unfortunately this made my app open up that story board without clicking the button. After two seconds of opening the app, it goes to that storyboard. – ermalsh Sep 02 '15 at 12:56
  • That's because you set the button in viewDidLoad, what makes your code run right after the view is loaded. I'll update my answer with a possible solution. – Nicholas Piccoli Sep 02 '15 at 12:58
  • Still wasn't able to get it to work. After I run it on the emulator, nothing still happens when I click the buttons. Here's a video of my code. https://vid.me/xuQF – ermalsh Sep 02 '15 at 13:18
  • It worked for me! I see you didn't set the navigation controller. Select your main view controller, then in the option above select **Editor > Embed In > Navigation Controller**. – Nicholas Piccoli Sep 02 '15 at 13:46
  • That's how I have it, yet still no luck. Take a look: http://i58.tinypic.com/9k5fll.png -- The code is still the same as in the video – ermalsh Sep 02 '15 at 16:23
  • Maybe the error is this: try selecting those lines that link the views and delete them. After that, select your main view controller, and notice that a yellow circle appears in the top of the view. Right? Ctrl+drag from that yellow circle to the view you want to segue to, then select that line again, go to attributes inspector, fill the identifier with the same identifier as in your code and run the app again, I think it should do. – Nicholas Piccoli Sep 02 '15 at 16:37
  • I forgot to mention; once your ctrl drag, selec push – Nicholas Piccoli Sep 02 '15 at 16:41
  • Ok, I deleted the whole story board and put it back together. We're getting somewhere because now the buttons click, but I still get errors. Please view this video. Notice what happens when I run it. https://vid.me/Uw8Q Thank you for your help. – ermalsh Sep 02 '15 at 16:54
  • It works only with one button, I'd have to remove the other one in order to work. It's not possible to add **buttonLogin.addTarget(self, action: "segueToNextVC:", forControlEvents: UIControlEvents.TouchUpInside)** to two buttons even when I change "segueToNextVC" to something else? – ermalsh Sep 02 '15 at 17:06
  • I figured it out, you've been a lot of help. Thank you so so so much. – ermalsh Sep 02 '15 at 17:11
  • I saw your video but since I'm on the phone, I can't read your code. For each button, you need to set a different function and a different segue. If you made one button work, you've done it right, you'll only need to set another function for the others. Something like func goToLoginArea() and func goToSignUpArea() and for each of these, a different performSegueWithIdentifier and in viewDidLoad you need to set different buttonBlaBla.addTarget. In your case it would be buttonLogin.addTarget and maybe a buttonSignUp.addTarget. – Nicholas Piccoli Sep 02 '15 at 17:13