2012-06-28 36 views
0

我想寫數據驅動器應用程序,我在解析json字符串來創建用戶界面。我已經實現了這個並創建了所需的控件。我區分每個控件基於分配給他們的標籤,這是不高效的方式。有沒有辦法在動態創建UIControl時將名稱(以下示例中的標籤和文本字段除外)分配給UIControl?iOS:創建並分配名稱到UIControl動態

NSMutableArray *myArray = [[NSMutableArray alloc] initWithCapacity: myArrayCount]; 
for (loop = 0; loop< myArrayCount; loop++) { 
    NSString *propertyName = [NSString stringWithFormat:@"Label %d", loop]; 
    [myArray addObject:propertyName]; 

    CGRect labelFrame = CGRectMake(xLabel, yLabel, widthLabel, heightLabel); 
    UILabel *label = [[UILabel alloc] initWithFrame: labelFrame]; 
    label.tag = loop; 
    [label setText:propertyName]; 
    [label sizeToFit]; 
    [self.view addSubview:label]; 

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(xTextField, yTextField, widthTextField, heightTextField)]; 
    textField.tag = loop; 
    textField.borderStyle = UITextBorderStyleRoundedRect; 
    textField.font = [UIFont systemFontOfSize:15]; 
    textField.placeholder = @"Enter parameter value"; 
    textField.autocorrectionType = UITextAutocorrectionTypeNo; 
    textField.keyboardType = UIKeyboardTypeDefault; 
    textField.returnKeyType = UIReturnKeyDone; 
    textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    textField.returnKeyType = UIReturnKeyDone; 
    textField.delegate = self; 
    [self.view addSubview:textField]; 

    yLabel = yLabel+yOffset; 
    yTextField = yTextField+yOffset; 

} 
+0

標籤是識別動態的有效途徑UIControls,如果你按名稱出發,那麼它會使事情變得更加複雜。 – rishi

+0

我這麼認爲,但只是想知道如果有人有任何其他的解決方案。每次我想要訪問特定文本字段的值時,我都必須解析所有文本字段,並使用正確的標記獲取它的值!此外,我有許多其他控件,如按鈕,分段控件,uipicker等....我可以使用標籤爲他們所有? – applefreak

回答

0

假設你不想使用[self.view viewWithTag:tag]檢索您的控制,單向通過名稱引用一個動態創建的控制將是把它放在一個NSMutableDictionary與動態生成鍵。

所以也許像(帶有一些細節省略掉你的代碼的修改):

NSMutableDictionary *controlsDictionary = [[NSMutableDictionary alloc] init]; 
for (loop = 0; loop< myArrayCount; loop++) { 

CGRect labelFrame = CGRectMake(xLabel, yLabel, widthLabel, heightLabel); 
UILabel *label = [[UILabel alloc] initWithFrame: labelFrame]; 
[controlsDictionary setObject:label forKey:[NSString stringWithFormat:@"label%d", loop]]; 

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(xTextField, yTextField, widthTextField, heightTextField)]; 
[controlsDictionary setObject:label forKey:[NSString stringWithFormat:@"textField%d", loop]]; 

yLabel = yLabel+yOffset; 
yTextField = yTextField+yOffset; 

} 

然後你就可以通過報表檢索控制像[controlsDictionary objectForKey:@"label1"]

+0

這就是我正在做的。關鍵實際上是一個標籤值! – applefreak

+1

很酷。另請參閱'UIView'上的'viewWithTag'方法,它應該允許您直接找到標記的子視圖,而無需維護自己的字典。 –

+0

哇! viewWithTag看起來不錯,我想!感謝那。 – applefreak

0

雖然你無法通過類別添加伊娃到UIControl,您可以添加關聯的對象,可用於多執行相同的功能。

static char kControlNameKey; 

- (void) setControlName: (NSString *) name 
{ 
    objc_setAssociatedObject(self, &kControlNameKey, name, OBJC_ASSOCIATION_COPY); 
} 

- (NSString *) controlName 
{ 
    return (NSString *)objc_getAssociatedObject(array, &kControlNameKey); 
} 

還有更多的比,我想你需要檢查是否有關聯設置一個新的之前就存在,否則會泄漏:

因此,在UIControl這樣創建類,但這應該給你一個開始。

Apple Docs更多細節

+0

只是一個簡單的問題。這是否意味着在文本字段中輸入的值將由關聯對象保存? – applefreak

+0

不,關聯的對象就像一個可以添加到實例的iVar - 在這種情況下,就像爲所有UIControl添加NSString屬性controlName一樣。 –