Ios UIButton press不调用功能
我是swift新手,创建了一个viewcontroller和一个mainView.swift,其中包含所有样式和ui元素,但是mainView.swift中的按钮不调用我创建的函数。我应该补充一点,所有元素在加载后都会加载,我点击登录按钮,什么都不会发生,我在swift中对视图的层次结构进行了一些研究,但如果没有任何帮助,我们将不胜感激 MainView.swiftIos UIButton press不调用功能,ios,swift,Ios,Swift,我是swift新手,创建了一个viewcontroller和一个mainView.swift,其中包含所有样式和ui元素,但是mainView.swift中的按钮不调用我创建的函数。我应该补充一点,所有元素在加载后都会加载,我点击登录按钮,什么都不会发生,我在swift中对视图的层次结构进行了一些研究,但如果没有任何帮助,我们将不胜感激 MainView.swift import Foundation import UIKit import PureLayout class login: UI
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)
}
}
代码中的问题是loginPressed方法缺少参数。单击按钮时,它将调用您的方法,并作为参数将单击的按钮对象发送到您的方法 但是正在调用的方法的签名与您的loginPressed方法不匹配。应该是这样的
@objc func loginPressed(sender: UIButton!) {
print("login Clicked")
}
loginView = login()
因为所有动作都是基于superView的框架进行的,所以它是这样的
@objc func loginPressed(sender: UIButton!) {
print("login Clicked")
}
loginView = login()
及
其中,元素具有相对于非零值的帧值,因为
let screenSize = UIScreen.main.bounds
screenSize.width/height
尽管superView有一个零帧,但会导致所有操作停止是否有任何错误?完全没有错误,我打开控制台查看我要打印的行是否显示,但没有显示。SPL使用autolayout而不是CgrectAll元素在mainView中。swift作为子视图添加到UIView中,即使使用参数,按下按钮仍不显示在屏幕中output@AAM尝试给loginViewtry一个框架。loginButton.addTargetself,操作:selectorloginPressedsender:,for:。TouchUpInside发件人参数不是强制性的,如果指定了该参数,则类型在Swift 3+中永远不会是IUO。此外,最好将loginButton.layer.addSublayergradient替换为loginButton.layer.insertSublayergradient,位置:0