简体   繁体   English

设置IBOutlet的财产

[英]Set property of IBOutlet

I created an outlet by draggin & dropping an UIImageView into the ViewController.swift file 我通过draggin创建了一个插座并将UIImageView放入ViewController.swift文件中

@IBOutlet weak var imageView: UIImageView!

I want to set the property of imageView to 我想将imageView的属性设置为

imageView.contentMode = .scaleToFill
imageView.clipsToBounds = true

Doing this 这样做

@IBOutlet weak var imageView: UIImageView! {
    self.imageView.contentMode = .scaleToFill
    self.imageView.clipsToBounds = true
}

XCode complains that XCode抱怨说

@IBOutlet attribute requires property to be mutable @IBOutlet属性要求属性是可变的

How do I go on about this? 我怎么继续这个?

What you're trying to do is a perfectly acceptable way to set up an @IBOutlet 's properties… just don't reference other @IBOutlet s, or the main view, etc, as this may force the main view to load earlier than expected. 您要做的是设置@IBOutlet属性的完全可接受的方式...只是不要引用其他@IBOutlet或主视图等,因为这可能会强制主视图加载比预期。

You just need to wrap your code inside a didSet block… 你只需要将你的代码包装在didSet块中......

@IBOutlet weak var imageView: UIImageView! {    
    didSet {
        imageView.contentMode = .scaleToFill
        imageView.clipsToBounds = true
    }    
}

An outlet is basically just a link to that UI element in the storyboard. 插座基本上只是故事板中该UI元素的链接。

So you link your UIImageView in the storyboard to an element in your UIViewController code and then you can access and change properties of that element once it is loaded. 因此,您将故事板中的UIImageView链接到UIViewController代码中的元素,然后您可以在加载后访问和更改该元素的属性。

The Problem 问题

This is the wrong syntax for doing this: 执行此操作的语法错误:

@IBOutlet weak var imageView: UIImageView! {
    self.imageView.contentMode = .scaleToFill
    self.imageView.clipsToBounds = true
}

The reason for the following error 出现以下错误的原因

@IBOutlet attribute requires property to be mutable @IBOutlet属性要求属性是可变的

is.. having a closure after a property declaration acts as a getter. 是...在属性声明充当吸气剂后有一个闭包。 As an example I can do this 作为一个例子,我可以做到这一点

let two = 2
let three = 3

var five: Int {
   return two + three
}

I cannot set any of the above values. 我无法设置上述任何值。 two and three are let only (read only constants) and five is a computed property, so I can also only read that. 两个和三个只允许(只读常量),五个是计算属性,所以我也只能读它。 I wont complicate the matter further but I would highly recommend giving this part of the documentation a read when you can. 我不会进一步使问题复杂化,但我强烈建议您尽可能阅读这部分文档

Solution

You should have an outlet only: 你应该只有一个出口:

@IBOutlet weak var imageView: UIImageView!

and then somewhere else in the code you can do things with it. 然后在代码中的其他地方你可以用它做事。

I think in your case it would be best on viewDidLoad. 我认为在你的情况下,它最适合viewDidLoad。 ViewDidLoad is called after the view and all of its outlets and properties have been set. 在视图之后调用ViewDidLoad,并设置其所有出口和属性。 So this is a safe place to start to use your outlets to further configure your view. 因此,这是一个安全的地方,可以开始使用您的商店进一步配置您的视图。

override func viewDidLoad() {
    super.viewDidLoad()

    self.imageView.contentMode = .scaleToFill
    self.imageView.clipsToBounds = true
}

First of all, it's not good approach to override outlet's getter. 首先,重写插座吸气器并不是一个好方法。 Secondly, technically you can override getter and setter of outlet. 其次,从技术上讲,你可以覆盖插座的getter和setter。

In your case you defined getter, and forgot about setter. 在你的情况下,你定义了getter,忘记了setter。 So you got get only variable. 所以你get only变数。

    private var _someView: UIImageView!

    @IBOutlet weak var someView: UIImageView! {
    get {
        self.someView.contentMode = .scaleToFill
        self.someView.clipsToBounds = true
        return _someView
    }
    set {
        _someView = newValue
    }
}

You should use private variable to avoid recursion in getter. 您应该使用私有变量来避免getter中的递归。

Just try in this way and invoke this method inside viewDidLod() 只需尝试这种方式并在viewDidLod()中调用此方法

func createRoundGreenImage(imageView : UIImageView, with image : 
String) {
self.imageView.contentMode = .scaleToFill
self.imageView.clipsToBounds = true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM