xcode “IBOutlet”属性具有非可选类型“UIButton”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24942123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:15:58  来源:igfitidea点击:

'IBOutlet' property has non-optional type 'UIButton'

iosxcodeswift

提问by josmek

Here's my code:

这是我的代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var button: UIButton

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

It's a simple IBOutlet (straight from the Apple developer docs). It gives me the error "'IBOutlet' property has non-optional type 'UIButton'" and I have no idea how to fix it.

这是一个简单的 IBOutlet(直接来自 Apple 开发人员文档)。它给了我错误“'IBOutlet' 属性具有非可选类型'UIButton'”,我不知道如何修复它。

回答by idmean

It should be like that (in Beta 3 or before):

应该是这样的(在 Beta 3 或更早版本中):

@IBOutlet var button: UIButton?

IBOutlets must be optionals so place a ?behind the type.

IBOutlets 必须是可选的,所以?在类型后面放置一个。

回答by Avi

It can also be-

也可以是——

    @IBOutlet var button: UIButton! 

or

或者

    @IBOutlet var weak button: UIButton! (in case you are not doing view unloading)

if you are using XCODE 6 Beta 4

如果您使用的是 XCODE 6 Beta 4

回答by Varsha Vijayvargiya

class SecondViewController: UIViewController {

     @IBOutlet weak var name: UILabel! = nil

     override func viewDidLoad() {

           super.viewDidLoad()

           name.text="i m a label"
     }
 }

This code working fine

这段代码工作正常

But when i replace @IBOutlet weak var name: UILabel? it is not working.

但是当我替换@IBOutlet 弱变量名时:UILabel?它不工作。