ios - Passing data between views in ONE ViewController in Swift -
all of searches i've done focus on passing data between view controllers. that's not i'm trying do. have viewcontroller has multiple views in it. viewcontroller has slider works fine:
var throttlesetting = float() @ibaction func changethrottlesetting(sender: uislider) { throttlesetting = sender.value }
then, in 1 of views contained in same viewcontroller, have basic line (for now) sets initial value used later in drawrect portion of code:
var rpmpointerangle: cgfloat { var angle: cgfloat = 2.0 return angle }
what want have slider's value viewcontroller passed view contained in viewcontroller allow drawrect dynamic.
thanks help!
edit: sorry, when created answer having viewcontrollers in mind. easier way create method in someview , talk directly it.
example:
class mainviewcontroller: uiviewcontroller { var view1: someview! var view2: someview! override func viewdidload() { super.viewdidload() // create views here view1 = someview() view2 = someview() view.addsubview(view1) view.addsubview(view2) } @ibaction func someaction(sender: uibutton) { view1.changestring("blabla") } } class someview: uiview { var somestring: string? func changestring(sometext: string) { somestring = sometext } }
delegate:
first create protocol:
protocol nameofdelegate: class { // ": class" isn't mandatory, when want set delegate property weak func somefunction() // function has implemented in mainviewcontroller can access properties , other methods in there }
in views have add:
class someview: uiview, nameofdelegate { // code func somefunction() { // change slider settings } }
and last step, you'll have add property of delegate, can "talk" it. imagine property gate of sort, between 2 classes can talk each other.
class mainviewcontroller: uiviewcontroller { weak var delegate: nameofdelegate? @ibaction func button(sender: uibutton) { if delegate != nil { let somestring = delegate.somefunction() } } }
i used button here show how use delegate. replace slider change properties of views
edit: 1 thing forgot mention is, you'll somehow need assign someview delegate. said, don't know how you're creating views etc can't that.
Comments
Post a Comment