title: (Swift) Putting a NSNotificationCenter observer in ViewDidLoad isn't working for my project... Where should I put it?
body: <p>I'm using SWRevealController to display 3 (left, center, right) panels. Basically, the right panel is a tableview of numbers and the middle panel shows possible even divisions when a user clicks on a number. I connected a segue to the IBAction of a reusable tablecell in the right view controller which loads the MainViewController. This all works fine. The problem is that if the number can't be divided evenly it triggers a notification, which the main view controller observes on ViewDidLoad. This notification sets the alpha of a pseudo-"alert" (UIView at the bottom of screen) to 1.0 for 4 seconds, at which point it returns to 0. Unfortunately this is where the problem starts: the notification box appears for a brief second while the animation runs but when the main viewcontroller finishes animating, the alert box disappears. I have a hunch it's because the ViewDidLoad fires at this point and resets the NSNotificationOberser – if I remove the segue on Touch Up Inside and manually switch view controllers the alert-box remains present.</p>
  
  <p>Can you help me think of what I'm getting wrong? Like I said, I think it's because the observer is initialized in the ViewDidLoad. Assuming this is the case, where should I initialize the observer so that this doesn't happen anymore? </p>
  
  <hr>
  
  <p>Basically the main VC displays a calculator, the code for which runs in a Calculator.swift file. If, when the number is passed through Calculate(), there is an error, it triggers an "alert" which the main VC picks up on, then reveals the box so that the user knows. Each time Calculate() is called, it logs the user's calculation to a tableview in the RVC – idea being they can reload previous calculations. Is this an improper usage of Notification Center?</p>
  
  <p>The way I want the timeline of events to be:</p>
  
  <ol>
  <li>User clicks on cell in the right panel</li>
  <li>Main view controller (the calculator) is pushed via segue</li>
  <li>Calculate() is called on the selected number, if there is a remainder a notification is triggered</li>
  <li>The main view displays the results from Calculate(), if an alert fired then it would unhide a popup box on main view.</li>
  </ol>
  
  <p>What is currently happening: </p>
  
  <ol>
  <li>User clicks on the cell in right panel </li>
  <li>Calculate() is called on the number, if there is a remainder the notification fires</li>
  <li>Before the main view is pushed via segue, I can see briefly in the animation the result of the trigger firing and the calculation</li>
  <li>As soon as the segue animation completes the view hides</li>
  </ol>
  
  <p>My main VC Code (PopUpView is the alert box)</p>
  
  <pre><code>class CalculatorVC: UIViewController {
      override func viewDidLoad() {
      super.viewDidLoad()
      NSNotificationCenter.defaultCenter().addObserver(self, selector: "triggerAlert:", name: "alert", object: nil)
      self.PopUpView.alpha = 0.0
  }
  </code></pre>
  
  <p>the triggerAlert function: </p>
  
  <pre><code>func triggerAlert(notification: NSNotification) {
      PopUpView.alpha = 1.0      
      let returnedRemainder = (notification.userInfo)
      let sample: Double = (returnedRemainder!["userTotal"]as! Double)
      self.label.text = "Warning! Remainder: \(Double(round(100 * sample)/100))"
  }
  </code></pre>
  
  <p>the tableView didSelect of the right VC: </p>
  
  <pre><code>func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  
      newUser.calculate()
  
      //code here assigns the value in the selected table cell to newUser
  }
  </code></pre>
  
  <p>Then in the Calculate() function: </p>
  
  <pre><code>dispatch_async(dispatch_get_main_queue(), {
      NSNotificationCenter.defaultCenter().postNotificationName("alert", object: nil, userInfo: userTotalDictionary)
  }
  </code></pre>