-(IBAction)button:(id)sender
{
button = [UIButton buttonWithType:UIBarButtonSystemItemRefresh];
NSLog(@"Value of txtfrequency in button fuinction :%@",txtfrequency.text);
int z = [txtfrequency.text intValue];
NSLog(@"Value of z is :%d",z);
int y = 10;
//create a new dynamic button
for (int j=0; j<z; j++)
{
CGRect frame = CGRectMake(20, y , 50, 30);
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTag:j];
button.frame = frame;
CGRect frame1 = CGRectMake(100, y , 50, 30);
textFieldRounded = [[UITextField alloc] initWithFrame:frame1];
textFieldRounded.borderStyle =UIButtonTypeInfoDark; //UITextBorderStyleNone;
textFieldRounded.textColor = [UIColor blackColor]; //text color
textFieldRounded.font = [UIFont systemFontOfSize:17.0]; //font size
textFieldRounded.backgroundColor = [UIColor whiteColor]; //background color
textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textFieldRounded.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
textFieldRounded.returnKeyType = UIReturnKeyDone; // type of the return key
textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldRounded.delegate = self;
[textFieldRounded setTag:j];
//[[scroll viewWithTag:j] removeFromSuperview];
[button reloadInputViews];
[textFieldRounded removeFromSuperview];
//[scroll
[scroll addSubview:button];
scroll.contentSize= CGSizeMake(100, 1400);
[scroll addSubview:textFieldRounded];
y= y+50;
}
}
斐伊川..
只要用户输入按钮,上面的代码就会动态生成按钮。但是在这种情况下存在问题......例如用户已经给出输入以生成4个按钮..然后在视图中我们得到4个按钮但是现在如果用户想要只有2个按钮那么在视图上仍然有4个按钮。我无法引用或更新按钮的动作。
任何人都知道我该怎么做。
请帮帮我。
非常感谢。
答案 0 :(得分:0)
尝试从scrollview中删除所有子视图。只需在向scrollview添加按钮之前创建一个枚举循环,如
for(UIView *v in scroll){
[v removeFromSuperView];
}
将上面的代码放在循环之前。
答案 1 :(得分:0)
您必须记住当前的按钮数量。我们假设您将其存储在numberOfButtons
。
您可以清理现有的文本字段,
- (IBAction)button:(id)sender
{
button = [UIButton buttonWithType:UIBarButtonSystemItemRefresh];
int y = 10;
for ( int i = 40; i < numberOfButtons+40; i++ ) {
[[scroll viewWithTag:i] removeFromSuperview];
}
numberOfButtons = [txtfrequency.text intValue]; // Make `numberOfButtons` an ivar.
for ( int i = 40; i < numberOfButtons+40; i++ ) {
[..] // Add a button here.
}
}
使用快速枚举的问题在于您将修改将引发错误的数组。