0

I am trying to change the background color of a window in a NSViewController by using a drop down menu with different color choices. This is the method that should do the changes. What am I doing wrong?

class ViewController: NSViewController, SimplerTextViewDelegate {

    @IBAction func changeBackgroundColor(sender: BackgroundColorPopupButton) {          
        let backgroundColorName= (sender.selectedItem?.title)!
        let backgroundColor = BackgroundColor.GetColor(backgroundColorName)
        self.view.window!.backgroundColor = backgroundColor
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 3
    "What am I doing wrong?" What is your evidence that you are doing anything wrong? If the code compiles, if it runs, if it is not crashing, it is presumably working. What's the problem exactly? – matt May 04 '16 at 00:33
  • 1
    Likely it is getting changed. Check you're view heirachy. Perhaps `self.view.window` is just hidden behind another view. Btw @matt hi I'm a huge fan of yours. – NSGangster May 04 '16 at 02:42
  • @NSGangster Thanks, plus I think you may have hit the nail right on the head; this is exactly why I'm probing further in my comment. The window is usually impossible to see, so what's the evidence it isn't changing color? I suspect the OP really wants to change `self.view.backgroundColor`. – matt May 04 '16 at 02:55
  • I thought he was referring to a `subview` of view called `window`, but ifs the mainWindow. I think @Leo Dabus is right here. Another way to access it is `UIApplication.sharedApplication().keyWindow`. although assuming the programmer doesn't change windows Leo's way will work too. keyWindow will grab the current window displayed by the app – NSGangster May 04 '16 at 03:16
  • @LeoDabus point taken. I guess i am a little naivete to the practices of others since I almost always set my keyWindow in `didFinishLaunchingWithOptions:` – NSGangster May 04 '16 at 03:19
  • @Matt I was referring to the main window. My main problem is I want the background to change, but I didn't specify which. It seems I would want the view's background to change if the window is impossible to see. – Chris Procak May 04 '16 at 03:59
  • http://stackoverflow.com/questions/2962790/best-way-to-change-the-background-color-for-an-nsview/31214482#31214482 – Leo Dabus May 04 '16 at 05:33

1 Answers1

0

I am using below code to change the color for my background view

@IBAction func Color(_ sender: Any) {

        let alert = UIAlertController(title: "Title", message: "Please Select Background Color", preferredStyle: .actionSheet)

        alert.addAction(UIAlertAction(title: "White", style: .default , handler:{ (UIAlertAction)in

        self.view.backgroundColor = UIColor.white

        }))

        alert.addAction(UIAlertAction(title: "Blue", style: .default , handler:{ (UIAlertAction)in

        self.view.backgroundColor = UIColor.cyan


        }))

        alert.addAction(UIAlertAction(title: "Grey", style: .destructive , handler:{ (UIAlertAction)in

        self.view.backgroundColor = UIColor.lightGray

        }))

        alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.cancel, handler:{ (UIAlertAction)in
            print("User click Dismiss button")
        }))

        self.present(alert, animated: true, completion: {
            print("completion block")
        })

    }
Guri S
  • 1,083
  • 11
  • 12