2017-04-19 20 views
1

我有一個名爲colorChangerViewClass的自定義類,從UIView繼承更改次數爲textLabel:從不同的視圖

class colorChangerViewClass: UIView { 


@IBOutlet weak var controller: ViewController! 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    // I want to change a textLabel in the main ViewController based on the current finger position here! 
    } 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    // And here aswell! 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { ... } 


func setColor(color: UIColor) { 
    self.backgroundColor = color 
} 

} 

touchesBegantouchesMoved方法,我想在主視圖控制器改變textLabel內(一個不同)基於當前手指位置。

什麼是建立兩個類之間溝通的最佳方式colorChangerViewClassViewController

回答

1

在您的情況下,您嘗試將孩子與父母溝通,您可以通過多種方式獲得所需的輸出。

1.Delegation

2.通知

3傳遞父實例對象,以兒童(類似委託)

我與代表,你可以展示使用授權爲: -

1.聲明 聲明一個協議某處側的ColorChangerViewClass

protocol ColorChangerViewClassDelegate:class { 
    func fingerDidMoved() 
    } 

2.創建委託VAR您ColorChangerViewClass內

class ColorChangerViewClass: UIView { 

     //declare delegate var 

     weak var delegate:ColorChangerViewClassDelegate? 

     @IBOutlet weak var controller: ViewController! 

     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
      // I want to change a textLabel in the main ViewController based on the current finger position here! 
      self.notify() 

     } 

     override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 

      self.notify() 
      // And here aswell! 
     } 

     override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 


     } 

     func setColor(color: UIColor) { 
      self.backgroundColor = color 
     } 

     func notify() { 
      if let delegate = self.delegate { 
       delegate.fingerDidMoved() 
      } 
     } 

     } 

3.將代表您的視圖控制器

class SomeVC:UIViewController,ColorChangerViewClassDelegate { 

    var myLabel = UILabel() 
     override func viewDidLoad() { 
      super.viewDidLoad() 

      //your view 
      let theView = ColorChangerViewClass() // you might need auto layout or frame declared on your view to define view size 
      theView.delegate = self 
     } 

     //MARK:-deleate method of view 
     func fingerDidMoved() { 
      // set text from here 
      self.myLabel.text = "Your text" 
     } 
    } 
1

您太接近不能解決問題,您需要將您的ViewController的參考設置爲controllercolorChangerViewClass的屬性。也不需要聲明controller作爲插座,因此改變其聲明如下。

var controller: ViewController? 

現在touchesBegantouchesMoved使用該控制器訪問textLabel

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    self.controller?.textLabel.text = //set text 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    self.controller?.textLabel.text = //set text 
} 

現在,在您ViewController類要在其中創建的colorChangerViewClass對象設置它的controller財產。

@IOutlet var colorView: colorChangerViewClass! 

//set controller property in viewDidLoad 
colorView.controller = self 

注:類的名字總是以大寫字母開始,所以如果你從colorChangerViewClass改變了你的類名ColorChangerViewClass會更好。

+0

我不明白你答案的最後部分。 我在界面生成器中創建了「ColorChangerViewClass」的實例。不是以編程方式。 當試圖使用你的代碼Xcode可以理解地抱怨'colorChangerView'被無效重新聲明。 – Marmelador

+0

如果您在界面構建器中設置此類,那麼您可以在ViewController中創建此視圖的Outlet,並將控制器屬性設置爲'viewDidLoad'。 –