var starbuttonsCount = starbuttons.count@IBOutlet var starbuttons: [UIbutton]!
然后直接在starbuttonsCount变量声明旁边跟随错误出现红色.
Error: Cannot use instance member ‘starbuttons’ within property
initializer; property initializers run before ‘self’ is available.
所以我发现通过将starbuttonCount变量声明为惰性,我们可以解决此错误(从iOS应用程序开发过程的长期来看是暂时的).
我很想知道解决这个问题的其他方法是什么?
当starbuttons IBOutlets初始化时,有没有办法触发starbuttonCount的初始化?
解决方法 awakeFromNib是一个方法,当.xib中的每个对象都被反序列化并且所有出口的图形连接时调用.文件说:Declaration
func awakeFromNib()
discussion
message to each object recreated from a nib archive,but only after
all the objects in the archive have been loaded and initialized. When
an object receives an awakeFromNib message,it is guaranteed to have
all its outlet and action connections already established.The nib-loading infrastructure sends anawakeFromNib
You must call the super implementation of awakeFromNib to give parent classes the opportunity to perform any additional initialization they require. Although the default implementation of this method does nothing,many
UIKit
classes provIDe non-empty implementations. You may call the super implementation at any point during your ownawakeFromNib
method.
如:
class MyUIVIEwController : UIVIEwController { var starbuttonsCount = 0 @IBOutlet var starbuttons: [UIbutton]! overrIDe func awakeFromNib() { super.awakeFromNib() starbuttonsCount = startbuttons.count }}总结
以上是内存溢出为你收集整理的ios – 初始化IBOutlet时如何初始化变量?全部内容,希望文章能够帮你解决ios – 初始化IBOutlet时如何初始化变量?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)