title: Why can't I pass data from my ViewController to my custom UIView using a delegate? (Swift)
body: <p>I have a ViewController (<code>oneUserDetailsViewController</code>) which I want to use to control a UIView (<code>weeklyLog</code>). To make the UIView work, I need to pass data in two NSArray's (<code>morningRecord</code> and <code>nighttimeRecord</code>) from the ViewController to the UIView. </p>
  
  <p>I have set up a Protocol (<code>WeeklyLogDataSource</code>) in the UIView with two functions (<code>morningLogForWeeklyLogView</code> and <code>nighttimeLogForWeeklyLogView</code>) designed to return the two NSArray's from the delegate (in this case the ViewController) to the UIView.</p>
  
  <p>In the UIView, I have declared the protocol (<code>dataSource</code>) and then created the code in the UIView to access the returned NSArrays. </p>
  
  <p>In the ViewController, I have added the delegate to the class declaration, set the delegate to self and then implemented the two functions to return the two NSArray's.</p>
  
  <p>As far as I know, I have set this up correctly. However, when I run the application, the data is not passed from the View Controller to the UIView and instead the UIView displays the default data <code>[0, 0, 0, 0, 0, 0, 0]</code>. Using a Print command, I can see that the declared Protocol (<code>dataSource</code>) in the UIView is showing as nil, which I assume is the root of my problem. But I do not know how to resolve it. </p>
  
  <p>Can you help?</p>
  
  <p>Here is the code from the UIView:</p>
  
  <pre><code>import UIKit
  
  // Delegate
  protocol WeeklyLogDataSource: class {
  
    func morningLogForWeeklyLogView(sender: weeklyLog) -&gt; NSArray?
    func nighttimeLogForWeeklyLogView(sender: weeklyLog) -&gt; NSArray?
  
  }
  
  
  class weeklyLog: UIView
  {
  
    // MARK: Properties
  
    weak var dataSource: WeeklyLogDataSource?
  
    var topRowImages = [UIImageView]()
    var secondRowImages = [UIImageView]()
    var bottomRowLabels = [UITextView]()
    let labelsForTextViews = ["", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
    let spacing = 5
    let columns = 8
    var morningRecord:NSArray! {
      didSet {
        setNeedsLayout()
        setNeedsDisplay()
      }
    }
    var nighttimeRecord:NSArray! {
      didSet {
        setNeedsLayout()
        setNeedsDisplay()
      }
    }
  
  
  
    func setup() {
      print("setup in weeklyLog called")
  
      if let morningLog:NSArray = dataSource?.morningLogForWeeklyLogView(self) {
        morningRecord = morningLog
      } else {
        morningRecord = [0, 0, 0, 0, 0, 0, 0]
      }
  
      if let nighttimeLog:NSArray = dataSource?.nighttimeLogForWeeklyLogView(self) {
        nighttimeRecord = nighttimeLog
      } else {
        nighttimeRecord = [0, 0, 0, 0, 0, 0, 0]
      }
  
      print("morningRecord: \(morningRecord)")
      print("nighttimeRecord: \(nighttimeRecord)")
  
    }
  
  
    // MARK: Initialisation
  
    required init?(coder aDecoder: NSCoder) {
  
      super.init(coder: aDecoder)
  
      self.backgroundColor = UIColor.whiteColor()
  
      setup()
      setNeedsLayout()
  
      print("dataSource: \(dataSource)")
  
    }
  
    override func layoutSubviews() {
  
      // Set the width and height of each image in proportion to the frame's size.
      let imageViewHeight = Int(Double(frame.size.height) * (Double(frame.size.height)/Double(frame.size.width)))
      let imageViewWidth = Int(Double(frame.size.width)/Double(columns)) - spacing
      var imageViewFrame = CGRect(x: 0, y: 0, width: imageViewWidth, height: imageViewHeight)
  
      // Offset each image's origin by the width of the image.
      // top row
      for (index, imageView) in topRowImages.enumerate() {
  
        imageViewFrame.origin.x = CGFloat(index * (imageViewWidth + spacing))    // spacing is set in the Properties section near the top
        imageView.frame = imageViewFrame
  
      }
  
      // second row
      for (index, imageView) in secondRowImages.enumerate() {
  
        imageViewFrame.origin.x = CGFloat(index * (imageViewWidth + spacing))    // spacing is set in the Properties section near the top
        imageViewFrame.origin.y = CGFloat(imageViewHeight)
        imageView.frame = imageViewFrame
  
      }
  
      // bottom row
      for (index, textView) in bottomRowLabels.enumerate() {
  
        imageViewFrame.origin.x = CGFloat(index * (imageViewWidth + spacing))    // spacing is set in the Properties section near the top
        imageViewFrame.origin.y = CGFloat(imageViewHeight*2)
        textView.frame = imageViewFrame
  
      }
  
  
      // set images for top row
      for var i = 0; i &lt; columns; i++ {
  
        let imageView = UIImageView()
  
        if i == 0 {
          imageView.image = UIImage(named: "sun")
        } else {
          if (morningRecord[i-1] as! NSNumber) == 0 {
            imageView.image = UIImage(named: "star_grey")
          } else {
            imageView.image = UIImage(named: "star_gold")
          }
        }
  
        topRowImages += [imageView]
        addSubview(imageView)
  
      }
  
      // set images for second row
      for var i = 0; i &lt; columns; i++ {
  
        let imageView = UIImageView()
  
        if i == 0 {
          imageView.image = UIImage(named: "moon")
        } else {
          if (nighttimeRecord[i-1] as! NSNumber) == 0 {
            imageView.image = UIImage(named: "star_grey")
          } else {
            imageView.image = UIImage(named: "star_gold")
          }
        }
  
        secondRowImages += [imageView]
        addSubview(imageView)
  
      }
  
      // set labels for bottom row
      for var i = 0; i &lt; columns; i++ {
  
        let logLabel = UITextView()
  
        logLabel.text = labelsForTextViews[i]
        logLabel.textAlignment = .Center
        logLabel.textContainer.maximumNumberOfLines = 1;
        logLabel.editable = false
        logLabel.selectable = false
  
        bottomRowLabels += [logLabel]
        addSubview(logLabel)
  
      }
  
    }
  
    override func intrinsicContentSize() -&gt; CGSize {
  
      let superViewHeight = Int(frame.size.height)
      let superViewWidth = Int(frame.size.width)
  
      return CGSize(width: superViewWidth, height: superViewHeight)
  
    }
  
  }
  </code></pre>
  
  <p>Here is the code from the ViewController:</p>
  
  <pre><code>import UIKit
  
  class oneUserDetailsViewController: UIViewController, WeeklyLogDataSource
      {
  
    @IBOutlet var logView: weeklyLog! {  // this is an outlet to the UIView in my Storyboard
      didSet {
        logView.dataSource = self
      }
    }
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
      }
  
    func morningLogForWeeklyLogView(sender: weeklyLog) -&gt; NSArray? {
  
      let morningLog:NSArray = [0,1,0,1,0,1,0]
      return morningLog
    }
  
    func nighttimeLogForWeeklyLogView(sender: weeklyLog) -&gt; NSArray? {
  
      let nightLog:NSArray = [0,1,0,1,0,1,0]
      return nightLog
    }
  
  }
  </code></pre>