2016-05-04 21 views
0

NSViewControllerのウィンドウの背景色を、異なる色の選択肢を持つドロップダウンメニューを使用して変更しようとしています。これは変更を行うべき方法です。私は間違って何をしていますか?どんな助けもありがとうございます。Swift:プログラムでウィンドウの背景色を変更する

class ViewController: NSViewController, SimplerTextViewDelegate { 

    @IBAction func changeBackgroundColor(sender: BackgroundColorPopupButton) {   
     let backgroundColorName= (sender.selectedItem?.title)! 
     let backgroundColor = BackgroundColor.GetColor(backgroundColorName) 
     self.view.window!.backgroundColor = backgroundColor 
    } 
} 
+2

を色を変更するには、コードの下に使用していますあなたが何か悪いことをしているあなたの証拠は何ですか?コードがコンパイルされ、実行されていれば、クラッシュしていない場合はおそらく動作しています。何が問題なの? – matt

+1

変更される可能性があります。あなたはヒーローを見ているか確認してください。おそらく 'self.view.window'は別のビューの後ろに隠れているだけかもしれません。 Btw @mattこんにちは私はあなたの巨大なファンです。 – NSGangster

+0

@NSGangsterありがとう、プラス私はあなたが頭の上で釘を打つかもしれないと思う;これはまさに私のコメントでさらに探求している理由です。ウィンドウは通常見えないので、色が変わっていないという証拠は何ですか?私はOPが本当に 'self.view.backgroundColor'を変更したいと思う。 – matt

答えて

0

私は「私が間違って何をしているのですか?」私の背景ビューの

@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") 
     }) 

    } 
関連する問題