2012-07-16 93 views
3

我有一個NSMutableArray實例,名爲myArray。它包含8個按鈕對象。我怎樣才能得到我已經錄製到一個整型變量的按鈕的索引?如何找到數組的索引?

回答

3

NSArray有一個名爲方法indexOfObject

- (IBAction)pushButton:(id)sender { 
    NSButton *button = (NSButton *)sender; 
    NSUInteger index = [Array indexOfObject:button]; 

} 
+0

我做了這樣的NSUInteger POS; pos = [MyArray indexOfObject:btn];但是當我在NSLOg(NSLog(@「Pos%i」,pos))中顯示pos時,我得到的輸出爲Pos2147483647我需要輸出爲0或1或2至7 – Shaan 2012-07-16 06:40:41

+1

您好Shaan,請您提供更多詳細信息到您的查詢。因爲Parag應該提供的解決方案。 – Subrat 2012-07-16 06:54:28

+0

Shaan是否嘗試用不同的格式說明符記錄pos?檢查String編程指南(http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html)中的「Platform Dependencies」部分。據說你可以在NSUInteger中使用%lu。 NSUInteger被定義爲unsigned int(或long,取決於平臺),所以%i和%d不應該工作。 – stan0 2012-07-16 08:51:17