ios - Why can't I make an array of type NSLayoutConstraint? -


this question has answer here:

basically, trying reset nsconstraints when user taps button.

my constraint outlets:

@iboutlet weak var firstbuttonheight: nslayoutconstraint! @iboutlet weak var secondbuttonheight: nslayoutconstraint! @iboutlet weak var thirdbuttonheight: nslayoutconstraint! 

example action:

@ibaction func firstbuttonheight(sender: uibutton) {     firstbuttonheight.constant = 100 } 



thought put them array (below) , iterate through them before running firstbuttonheight.constant = 100:

var constraints: [nslayoutconstraint] = [firstbuttonheight, secondbuttonheight, thirdbuttonheight] constraint in constraints {     constraint.constant = 50 //original constraint value } 

however, error instance member 'firstbuttonheight' cannot used on type 'viewcontroller'. when did firstbuttonheight become viewcontroller?

one major problem in constraints initializer mention names of other instance variables. can't that, because @ instance variable initialization time, instance doesn't exist yet — initializing!

you solve moving code function runs after initialization time:

class viewcontroller: uiviewcontroller {      @iboutlet weak var firstbuttonheight: nslayoutconstraint!     @iboutlet weak var secondbuttonheight: nslayoutconstraint!     @iboutlet weak var thirdbuttonheight: nslayoutconstraint!      func somefunction() {         var constraints: [nslayoutconstraint] = [firstbuttonheight, secondbuttonheight, thirdbuttonheight]         constraint in constraints {             constraint.constant = 50 //original constraint value         }     } } 

look @ difference between , you've been doing.

a common solution problem need mention instance variable in instance variable's initializer declare second instance variable lazy. note must use self when (and in general recommend using it). thus, compiles @ top level of view controller:

@iboutlet weak var firstbuttonheight: nslayoutconstraint! @iboutlet weak var secondbuttonheight: nslayoutconstraint! @iboutlet weak var thirdbuttonheight: nslayoutconstraint! lazy var constraints : [nslayoutconstraint] = [self.firstbuttonheight, self.secondbuttonheight, self.thirdbuttonheight] 

but rest, for loop, still needs live inside function body.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -