【问题标题】:Refresh or update the button action刷新或更新按钮操作
【发布时间】:2011-06-02 06:49:06
【问题描述】:
-(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 个按钮在视图中。我无法引用或更新按钮的操作。

任何人都知道我该怎么做。

请帮帮我。

非常感谢。

【问题讨论】:

    标签: iphone objective-c ios uibutton


    【解决方案1】:

    尝试从滚动视图中删除所有子视图。只需在将按钮添加到滚动视图之前创建一个枚举循环,例如

     for(UIView *v in scroll){
         [v removeFromSuperView];
      }
    

    把上面的代码放在你的循环之前。

    【讨论】:

    • 是的,在滚动视图中添加按钮之前,将它们全部删除。因为,一旦你添加了 4 个按钮,我猜如果你不删除它们,下次你添加 2 个按钮时,它们会出现在之前添加的 4 个按钮的前 2 个按钮上,给你一种仍然有 4 个按钮的印象。
    【解决方案2】:

    您必须记住当前的按钮数量。假设您将其存储在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.
        }
    }
    

    使用快速枚举的问题是您将修改数组,这将引发错误。

    【讨论】:

    • 这段代码清理了生成的按钮,但无法在同一个视图上重新生成按钮。
    • 你能把这个if ( scroll == nil ) { NSLog(@"Nil Scrollview"); }放在按钮创建代码之前并告诉我它的结果吗?
    • 我没想到它会起作用。它确实在控制台上打印了“Nil Scrollview”。
    • 是的,它使滚动视图为零。
    • 好的,从 40 或任何更高的数字开始分配标签。将更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    相关资源
    最近更新 更多