How to Add and Remove UIButtons in SubView using UISilder?

I have a subview which is allmost half to my mainview and also I have a UISlider on my mainview . My silder has a value from 0 to 10. i want to add some UIButtons to my Subview with the UISilder.Now if my silder get the value 2 then i want to add 2 UIButtons to my Subview as if my silder pass the value 2 and gets the value 4 then I want to remove the previous UIButtons from Subview and Add some new UIButtons to Subview .

Answers


Note: The code is untested.

  1. Initiate a 10 UIButton and add them to the view, set them as hidden:

    for (int x = 0; x < 10; x++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, x * 100, 100, 50)];
        [btn setText:[NSString stringWithFormat:@"Button %d",(x + 1)]];
        [btn setHidden:true];
        [self.view addObject:btn];
        [btn release];
    }
    
  2. Use the slider change value method to hide/show the buttons like this:

        -(IBAction) sliderChanged:(id) sender{
              UISlider *slider = (UISlider *) sender;
              for (int x = 0; x < slider.value; x++) {
                   UIButton *btn = (UIButton *)[savedBtn objectAtIndex:x];
                   [btn setHidden:false];
              }
        }
    

Update

To use a UIScrollView, you have to add the UIButtons to the scrollView instead of the view, like this:

    UIScrollView *sView = [[UIScrollView alloc] initWithFrame:CGRectMake(10,10,200,200)];
    [sView setDelegate:self];
    for (int x = 0; x < 10; x++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, x * 100, 100, 50)];
        [btn setText:[NSString stringWithFormat:@"Button %d",(x + 1)]];
        [btn setHidden:true];
        [sView addSubview:btn];
        [btn release];
    }

then add the scrollView to the view:

     [self.view addSubview:sView];

hope this can help you.


First of all you add a selector method to your UISlider,

[customSlider addTarget:selfaction:@selector(sliderEnd:)forControlEvents:UIControlEventTouchUpInside];

now the selector method is supposed to be defined in the Mainview class of your project.

Now in the selector method you determine the slider value in the function, and depending on its value you may add or remove or add buttons to the other view. You can store the reference to all the button when you add them to your view and then remove them from superview when the slider value suggests to do so.

Hope it helps.


Need Your Help

Resolving an ADO timeout issue in VB6

sql vb6 timeout ado

I am running into an issue when populating an ADO recordset in VB6. The query (hitting SQLServer 2008) only takes about 1 second to run when I run it using SSMS. It works fine when the result set...

How do I set the headers to “application/x-www-form-urlencoded” on a XML based API Call

php xml api

I am looking over the documentation for the VerMail API and they specify that I need to set the headers to "application/x-www-form-urlencoded" but I have to send the data as XML.. I know this is