我想寫數據驅動器應用程序,我在解析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;
}
標籤是識別動態的有效途徑UIControls,如果你按名稱出發,那麼它會使事情變得更加複雜。 – rishi
我這麼認爲,但只是想知道如果有人有任何其他的解決方案。每次我想要訪問特定文本字段的值時,我都必須解析所有文本字段,並使用正確的標記獲取它的值!此外,我有許多其他控件,如按鈕,分段控件,uipicker等....我可以使用標籤爲他們所有? – applefreak