2016-03-10 11 views
0

我有一個非常簡單的用戶界面,只有一個NSPopUpButton。其selectedIndex綁定到intViewController.self.my_selection。只要我從UI中更改選擇(即選擇NSPopUpButton的第三項),我就會看到my_selection值發生了變化。到目前爲止這麼好,我試圖獲得的是相反的方向。我想以編程方式更改my_selection值,並查看NSPopUpButton選擇項目作爲我在my_selection中定義的索引。我錯誤地假設的行爲是對綁定的默認行爲......綁定和NSPopUpButton:更改當前鍵的值不會改變選擇

這就是我現在獲得:

NSPoPUpButton ---> select item at index 2 ----> my_selection becomes equal to 2 

這就是我想要的實現(也保持在以前的行爲)

my_selection ---> set value to 3----> NSPoPUpButton selected index = 3 
+0

你能告訴我們的實際代碼?特別是,你如何在你的''ViewController''類中定義''my_selection''? –

回答

0

沒有更多的信息(請參閱我的評論)很難確切地看到你做錯了什麼。以下是我得到它的工作:首先,創建一個簡單的類...

// Create a simple class 
class Beatle: NSObject { 
    convenience init(name: String) { 
     self.init() 
     self.name = name 
    } 
    dynamic var name: String? 
} 

然後,在AppDelegate我創建了一個四項目陣列稱爲beatles

dynamic var beatles: [Beatle]? 

func applicationDidFinishLaunching(aNotification: NSNotification) { 

    beatles = [Beatle(name: "John"), 
     Beatle(name: "Paul"), 
     Beatle(name: "Ringo"), 
     Beatle(name: "George")] 
} 

在Interface Builder我設置事情讓這個數組提供彈出其內容:

enter image description here

這個類也有一個綁定到彈出按鈕的屬性綁定 - 這個屬性提供讀寫訪問彈出按鈕的選擇:

// Set the pop-up selection by changing the value of this variable. 
// (Make sure you mark it as dynamic.) 
dynamic var selectedIndex: Int = 0 
相關問題