Swift4:UIButton无法正常工作

时间:2018-01-14 11:11:26

标签: ios swift uibutton

在我的应用程序中,我正在尝试支持UIButton,我只是想要打印“这是一个测试”当我按下按钮时它会执行经典按钮动画,但是控制台中没有打印任何内容。

var tbutton: UIButton = {
    let button = UIButton(type: .system)
    button.frame = CGRect(x: 40.0, y:400.0, width: 300.0, height: 300.0)
    let image = UIImage(named: "backb")
    button.setBackgroundImage(image, for: .normal)
    button.addTarget(self, action: #selector(dothings), for: .touchUpInside)
    return button
}()

@objc func dothings(){
    print("this is a test")

}

然后我将按钮添加到视图中:

view.addSubview(tbutton)

我是否缺少一段代码,或者我编写了错误的代码?

1 个答案:

答案 0 :(得分:2)

你不应该以这种方式初始化你的按钮。

Setting a Default Property Value with a Closure or Function引用Apple文档:

  

如果使用闭包来初始化属性,请记住在执行闭包时尚未初始化实例的其余部分。这意味着您无法从闭包中访问任何其他属性值,即使这些属性具有默认值也是如此。此外:

您也不能使用隐式自身属性,也不能调用任何实例的方法,因此问题在于:

{ "routes": [ { "geometry": "ybjzFcdijD}}BuqAhhOcmRfb[ham@mlFnfj@nsKf`q@uzBpnPxvEx|b@plK`p`@sdA~kuB_tKt{Ruf@vvOvcEzdS}u@vfYpgLr~o@_{l@~ez@enDjdYzZl~{@f`Jlu[ad@bhQjiNxtU~qDzan@pyOdiQk}@bkLnbQz}k@o`@bc_@zxVj}l@qhD~bn@|tBhcTkuIjen@xhGljKfa@pcTzfFuRlsVllr@~p^ilDpmQjpOhqp@q~IriBlzErhCby@mwB_sKoht@f{M{uQypOof]x_EuuLeoQs_Jer`@ajFvVk]}yS}pGw~LvyI_{l@_uBgbT|gDuen@wwVsxl@n`@eb_@ubQe_l@p|@qnLsxOmfQuqDq_n@gjNqxUlb@woQo}Iq`[_\\kk|@~mDedYr~l@qh|@wkLc`n@tv@acYycE_cS`f@oxOrtKm{RzdAwiuB{lKwr`@qvEg}b@rzBymPwuKusq@jrFgdi@kbK_pLocPmzi@|aM{sg@e}I_kYhiI_yQyjHuh~@fbAi`QdcG{vFaBsu[h{FzB~mCgsIt~FzfBtz[mle@fzJqXrqR~pWrx_@vgMfiIvl_@xsNfpDxjHpsPhmPad~@liJo{Ef`Wc`r@l}JmxAh`Kmwc@gaDc{]hmSqfe@jnf@`lHlzGvnOfyYdkNtbKofM~cPrmSldNle_Ah~s@df_A_fFtuUakVpxZ{jFxld@ck@zkYjvIw~@bcWpki@yvWllXxvWmlXccWqki@kvIv~@bk@{kYzjFyld@`kVqxZ~eFuuUi~s@ef_AmdNme_Ag`PgvSqmKh_M{qY{{McxGenOamf@orHarSjke@jaDl|]eaKlyc@u|JnvA}_W|_r@kdKjqFipOpb}@}kHaiPsoNwiDcmIur_@kx_@}gMuxR{rWw|JzVypFpzPafT~tSwzFigBapCpuIm{FiIhEnz[edG|wFmaAfjQrjHba~@{hIvuQn}I~fYkvLrgp@dz[rwn@ioFt{i@nsKf`q@uzBpnPxvEx|b@plK`p`@sdA~kuB_tKt{Ruf@vvOvcEzdS}u@vfYzjLp|m@i~l@`h|@enDjdY`\\pk|@`_Jhh[ad@bhQjiNxtU~qDzan@pyOdiQk}@bkLnbQz}k@o`@bc_@zxVj}l@qhD~bn@|tBhcTqyIt{l@~lGbtLfa@pcT`fF_T|xOlok@toLpoHr{Wq~F|tQbpOvri@i~E`hK}cLihKzbL{oi@j}Eg{QipOqvWt}F{lMwcJ_yNq|i@m_GsYkGofR}pGw~LvyI_{l@_uBgbTvhDcan@ewVcyl@b_@gf_@ubQe_l@v{@mqLywOqcQksD{en@elNa~U`f@}cQa~Iwe[i]mez@n}Dmb\\|ll@key@ytP{m~A`f@oxOrsK_tR|]gui@~rEd`AdlWohUjyOovXlqXzj_@~}OltHj{EqbKubDyoe@jtKmiNdcRkwr@hhWgPpfIaqHzaX~uGwbFibXgvGbcB", "legs": [ { "summary": "", "weight": 59053.3, "duration": 29868.5, "steps": [], "distance": 633021.8 }, { "summary": "", "weight": 98982.7, "duration": 60860.9, "steps": [], "distance": 1270431 }, { "summary": "", "weight": 85723.3, "duration": 54659.9, "steps": [], "distance": 1255026.5 }, { "summary": "", "weight": 80038.7, "duration": 39865, "steps": [], "distance": 668853.2 } ], "weight_name": "routability", "weight": 323798, "duration": 185254.3, "distance": 3827332.5 } ], "waypoints": [ { "hint": "AFmhh4uHV4iAAgAAPgAAAIcBAAAAAAAAQAEAAB8AAADDAAAAAAAAAKPfAAAvS6wBZl50AmRSrAF0YHQCAgA_FTrv2WQ=", "name": "", "location": [ 28.068655, 41.180774 ] }, { "hint": "F4gUhJHWs4QcAAAAQQAAALV8AABiNAAADQAAACEAAABdPgAAOBoAAKPfAAAH-FcB_m9iAuT-VwHsbWICzABfATrv2WQ=", "name": "", "location": [ 22.542343, 40.00563 ] }, { "hint": "fIxchNifEYd3AAAAAAAAACFKAAAAAAAAOwAAAAAAAAAbJQAAAAAAAKPfAAB7E7EB-1hUAsQksQHkulQCiwCPEjrv2WQ=", "name": "", "location": [ 28.382075, 39.082235 ] }, { "hint": "S4UOhLRX8YmHBAAAAAAAAMEAAAAAAAAAQwIAAAAAAABgAAAAAAAAAKPfAAA9OFkBdkNiAlDIWQHo2mICAwDfATrv2WQ=", "name": "", "location": [ 22.624317, 39.99423 ] }, { "hint": "bsy0hnvMtIZ0AAAAPgAAAOsIAACDKwAAOgAAAB8AAAB0BAAAxBUAAKPfAAB8u6EB1JhiAoTioQH8lGICEgB_ADrv2WQ=", "name": "", "location": [ 27.376508, 40.016084 ] } ], "code": "Ok"

所以为了解决这个问题,你应该在ViewController完全初始化之后移动按钮初始化(或移动addTarget)(例如:button.addTarget(self, action: #selector(dothings), for: .touchUpInside))。

另一种解决问题的方法,假设您仅在viewDidLoad之后使用此类按钮,则将其定义为viewDidLoad

  

惰性存储属性是一个属性,其初始值在第一次使用之前不会计算