在iOS Swift的不同ViewController中设置struct属性的值

时间:2018-08-03 06:36:07

标签: ios swift struct

我用struct设置常数。我用整数形式的maxTextLength我必须为不同的控制器设置不同的值,例如一个为300,另一个为1000。这是我的代码

    struct Validations {
    static let maxAudioRecSec:Int = 150
    static var maxTextLength = 300 // Default value
    }
    SecondVC :ViewController {
    override func viewDidLoad () {
          Validations.maxTextLength = 1000
    } 
   }

因此,在SecondVC内更改的值仅保留在该控制器内,即1000。如果我在另一个控制器内访问此值,则默认值为300。

1 个答案:

答案 0 :(得分:-1)

您必须像下面那样使用maxTextLength时不要使用静态变量。

struct Validations {
    static let maxAudioRecSec:Int = 150
    var maxTextLength = 300 // Default value
 }

现在在任何ViewController中,您都必须像下面这样使用。

class SecondVC :ViewController {
var validations = Validations() // create Validations struct object
    override func viewDidLoad () {
          validations.maxTextLength = 1000 // use like this
    } 
   }

任何疑问,请发表评论。