AAM
AAM

Reputation: 5

UIButton press wont call function

I am new to swift and have created a viewcontroller and a mainView.swift which holds all the styling and ui elements however the button in the mainView.swift doesn't call the function i have created i should add that all the elements load in fine its just after they load and i tap the login button nothing happens , i did some research into the hierarchy of views in swift however noting helped solve the issue , any help would be appreciated

MainView.swift

import Foundation
import UIKit
import PureLayout

class login: UIView {
var shouldSetupConstraints = true



var logoImageContainer = UIView()
var logoImage = UIImageView(image: UIImage(named: "logo"))
var loginButton = UIButton()
var registerButton = UIButton()

let screenSize = UIScreen.main.bounds
let gradient = CAGradientLayer()


override init(frame: CGRect) {
    super.init(frame: frame)

    logoImage.center = CGPoint(x: screenSize.width/2, y:0)
    logoImage.alpha = 0.0

    self.addSubview(logoImage)

    loginButton.frame = CGRect(x: screenSize.width/14, y: screenSize.height/1.35, width: 320, height: 50)
    gradient.frame = loginButton.bounds
    gradient.colors = [UIColor(red: (0/255.0), green: (114/255.0), blue: (255/255.0), alpha: 1).cgColor , UIColor(red: (0/255.0), green: (198/255.0), blue: (255/255.0), alpha: 1).cgColor]
    gradient.startPoint = CGPoint(x: 0, y: 0)
    gradient.endPoint = CGPoint(x: 1, y: 0)
    gradient.cornerRadius = 4
    loginButton.layer.addSublayer(gradient)
    loginButton.setTitle("SIGN IN", for: .normal)
    loginButton.titleLabel?.font = UIFont(name: "HelveticaNeue-medium", size: 30)
    loginButton.titleLabel?.textAlignment = .center
    loginButton.alpha = 0.0
    loginButton.addTarget(self, action: #selector(loginPressed), for: .touchUpInside)

    self.addSubview(loginButton)

    registerButton.frame = CGRect(x: screenSize.width/14, y: screenSize.height/1.175, width: 320, height: 50)
    registerButton.setTitle("Create Account", for: .normal)
    registerButton.setTitleColor(UIColor.gray, for: UIControlState.normal)
    registerButton.titleLabel?.font = UIFont(name: "HelveticaNeue-medium", size: 24)
    registerButton.titleLabel?.textAlignment = .center
    registerButton.alpha = 0.0

    self.addSubview(registerButton)

    UIView.animate(withDuration: 1.0, animations: { () -> Void in
        self.loginButton.alpha = 1.0
        self.registerButton.alpha = 1.0
        self.logoImage.alpha = 1.0
        self.logoImage.center = CGPoint(x: self.screenSize.width/2, y:self.screenSize.height/2.5)
    })



}

@objc func loginPressed() {
    print("press")
    /* UIView.animate(withDuration: 1.0, animations: { () -> Void in
     self.loginButton.center = CGPoint(x: self.screenSize.width/14, y: self.screenSize.height/1.175)
     self.registerButton.alpha = 0.0
     self.registerButton.center = CGPoint(x: self.screenSize.width/2, y:self.screenSize.height/1)
     self.logoImage.center = CGPoint(x: self.screenSize.width/2, y:self.screenSize.height/1.5)
     }) */
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func updateConstraints() {
    if(shouldSetupConstraints) {
        // AutoLayout constraints
        shouldSetupConstraints = false
    }
    super.updateConstraints()
}
}

MainViewController.swift

class MainViewController: UIViewController {

 var loginView: login!

override func viewDidLoad() {
    super.viewDidLoad()
   self.view.backgroundColor = UIColor(red: (255/255.0), green: (255/255.0), blue: (255/255.0), alpha: 1)
    loginView = login()
    loginView.isUserInteractionEnabled = true
    self.view.addSubview(loginView)
}
}

Upvotes: 0

Views: 57

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100549

Since all actions work on the basis of the superView's frame making it like this

loginView = login()

and

loginButton.frame = CGRect(x: screenSize.width/14, y: screenSize.height/1.35, width: 320, height: 50)

where elemnts have there frame values relative to non-zero value because of

let screenSize = UIScreen.main.bounds
screenSize.width/height

despite the superView has a zero frame , will cause all actions to drop

Upvotes: 0

Manish Malviya
Manish Malviya

Reputation: 586

the problem in your code is your loginPressed method is missing a parameter. When a button is clicked it will call your method and as parameter it will send a button object which is clicked to your method.

But the signature of method being called and your loginPressed method is not matching. It should be like this

@objc func loginPressed(sender: UIButton!) {  
          print("login Clicked")  
     } 

Upvotes: 1

Related Questions