开发者

Refresh or update the button action

-(IBAction)button:(id)sender
{
    button = [UIButton buttonWithType:UIBarButtonSystemItemRefresh];
    NSLog(@"Value of txtfrequency in button fuinction :%@",txtfrequency.text);
    int z = [txtfrequency.text  intValue];
    NSLog(@"Val开发者_高级运维ue 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;


} 
}

Hii..

Above code generates no of buttons dynamically whenever user give input for buttons. But there is problem in this event.. e.g. User have given the input to generate 4 buttons ..then on view we get 4 buttons but now if user wantto have only 2 buttons then there is still 4 buttons on view. I am not able to refersh or update the action of button.

Anyone know how can I do this.

Please help me.

Thanks alot.


Try remove all subviews from scrollview. just create a enumeration loop before adding buttons to scrollview like

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

put the above code before your loop.


You will have to remember the current number of buttons. Let us say you store it in numberOfButtons.

You can clean up the existing text fields like this,

- (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.
    }
}

The problem with using fast enumeration is that you will be modifying the array which will raise an error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜