我正在尝试从我的MainClass类中调用TheTimer类中的函数。但是,在TheTimer类中有一个必需的init,当我尝试从TheTimer类调用Start函数时(我从MainClass类中调用它)它会抛出错误:在调用中缺少参数'coder'的参数
TheTimer类负责作为故事板一部分的UIView。
TheTimer:
class TheTimer: UIView {
var timeLabel: UILabel
required init?(coder aDecoder: NSCoder) {
timeLabel = UILabel()
super.init(coder: aDecoder)
let viewWidth = self.frame.size.width
let viewHeight = self.frame.size.height
timeLabel.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight)
timeLabel.text = "0"
timeLabel.font = UIFont(name: "Arial", size: 45)
timeLabel.textAlignment = NSTextAlignment.center
self.addSubview(timeLabel)
}
//MARK: Properties
var time: Double = 0.000
var timer = Timer()
func Start() {
timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(countUp), userInfo: nil, repeats: true)
}
@objc func countUp() {
time += 0.001
}
MainClass Class
import UIKit
class MainClass: UIViewController {
//MARK: Properties
@IBOutlet weak var answerLabel: UILabel!
//MARK: Go
@IBAction func startAll(_ sender: UIButton) {
TheTimer().Start() //Throws error 'Missing argument for parameter 'coder' in call'
}
}
答案 0 :(得分:1)
在MainClass
课程中,为TheTimer
视图创建一个属性,之后将故事板中的TheTimer
与此IBOutlet
相关联。最后,请致电theTimer.Start()
而不是TheTimer().Start()
import UIKit
class MainClass: UIViewController {
//MARK: Properties
@IBOutlet weak var answerLabel: UILabel!
@IBOutlet weak var theTimer: TheTimer!
//MARK: Go
@IBAction func startAll(_ sender: UIButton) {
theTimer.Start() // It will work fine
}
答案 1 :(得分:0)
您正在调用一个尚未编写的空init()函数。你唯一的init是必需的init,你不匹配它的参数。我建议编写第二个init函数,其中包含所有初始化内容。
init() {
timeLabel = UILabel()
super.init()
let viewWidth = self.frame.size.width
let viewHeight = self.frame.size.height
timeLabel.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight)
timeLabel.text = "0"
timeLabel.font = UIFont(name: "Arial", size: 45)
timeLabel.textAlignment = NSTextAlignment.center
self.addSubview(timeLabel)
}
然后
TheTimer().Start()
答案 2 :(得分:0)
Yasin是正确的,你必须创建另一个init。另外,您可以将代码分成单独的方法。我不明白,为什么你不需要它是一个UIView并且如果你不使用任何这些标签就有标签。
init() {
timeLabel = UILabel()
super.init(frame: .zero)
setupView()
}
required init?(coder aDecoder: NSCoder) {
timeLabel = UILabel()
super.init(coder: aDecoder)
setupView()
}
func setupView() {
let viewWidth = self.frame.size.width
let viewHeight = self.frame.size.height
timeLabel.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight)
timeLabel.text = "0"
timeLabel.font = UIFont(name: "Arial", size: 45)
timeLabel.textAlignment = NSTextAlignment.center
self.addSubview(timeLabel)
}
答案 3 :(得分:0)
因为您提供了所需的初始化程序convenience initializer
,所以默认init()
已经消失。因此,TheTimer()类没有awakeFromNib()
。您的错误发生在TheTimer()而不是Start()。
有两种方法:
1。在init?(coder aDecoder: NSCoder)
而不是awakeFromNib()
中调用初始化代码。
从Interface Builder归档或nib加载视图后,将立即调用var timeLabel: UILabel
然后,您应该将var timeLabel: UILabel! or ?
更改为init() {
super.init(frame: CGRect.zero) // you need to give it a frame, right?
// Your initialization code
timeLabel = UILabel()
let viewWidth = self.frame.size.width
let viewHeight = self.frame.size.height
timeLabel.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight)
timeLabel.text = "0"
timeLabel.font = UIFont(name: "Arial", size: 45)
timeLabel.textAlignment = NSTextAlignment.center
self.addSubview(timeLabel)
}
,因为您没有在初始化程序中对其进行初始化。
2。自己编写初始化程序
12 Nov 2017 23:48:51,292 [DEBUG] (DefaultNamespaceHandlerResolver.java:128) - Ignoring namespace handler [org.springframework.ejb.config.JeeNamespaceHandler]: handler class not found
java.lang.ClassNotFoundException: org.springframework.ejb.config.JeeNamespaceHandler
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:229)
at org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver.initHandlerMappings(DefaultNamespaceHandlerResolver.java:117)
at ......................
org.springframework.beans.factory.access.SingletonBeanFactoryLocator.useBeanFactory(SingletonBeanFactoryLocator.java:382)
at com.xxx.pci.util.ServiceLocator.getApplicationContext(ServiceLocator.java:24)
at com.xxx.pci.decrypt.Decrypt.<init>(Decrypt.java:48)
at com.xxx.mapper.SProductApplicationLogMapper.mapRow(SProductApplicationLogMapper.java:17)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:92)
at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:395)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:343)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:405)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:413)
at com.xxx.AwipDataBean.getAllProductApplications(AwipDataBean.java:44)
at
public class ServiceLocator {
private static ApplicationContext applicationContext;
protected ServiceLocator() {
}
public static ApplicationContext getApplicationContext() {
if (applicationContext == null) {
BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance("applicationContextDef.xml");
BeanFactoryReference bfr = locator.useBeanFactory("applicationContext");
BeanFactory factory = bfr.getFactory();
applicationContext = (ApplicationContext) factory;
//bfr.release();
}
return applicationContext;
}
}
顺便说一下,你应该用小写命名一个函数 The Swift Programming Language (Swift 4): Initialization developer.apple.com