UIButton按不会调用功能

时间:2018-08-25 12:53:25

标签: ios swift

我是swift的新手,已经创建了一个viewcontroller和一个mainView.swift,其中包含所有样式和ui元素,但是mainView.swift中的按钮未调用我创建的函数,我应该添加所有元素加载后就很好了,我点击登录按钮没有任何反应,我对视图的层次结构进行了一些快速研究,但注意到这有助于解决问题,我们将不胜感激

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

2 个答案:

答案 0 :(得分:1)

代码中的问题是您的loginPressed方法缺少参数。单击按钮时,它将调用您的方法,并作为参数发送一个被单击到您的方法中的按钮对象。

但是正在调用的方法签名与您的loginPressed方法不匹配。应该是这样

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

答案 1 :(得分:0)

由于所有操作都基于superView的框架而定,因此

loginView = login()

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

由于

,其中元素的帧值相对于非零值
let screenSize = UIScreen.main.bounds
screenSize.width/height

尽管superView的帧为零,但将导致所有操作掉落