UISlider in UIToolbar -> can't get slider.value and process it. iOS SDK.

Discussion in 'iOS Programming' started by gwelmarten, Jul 10, 2012.

  1. gwelmarten macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #1
    Hi
    I'm trying to get a UISlider into my UIToolbar and to take the value from it and process it. I also want to be able to change the slider.value via other methods.
    So, I set it up for the UISlider by it's self (not on a UIToolBar) and everything worked fine. Then, when I put it into the UIToolBar, the value stopped getting sent to and from it (I am NSLogging it whenever it changes and I keep getting 0 returned to me).
    Here's the relevant section of my code - does anybody have any ideas?

    Code:
        - (void)setupToolBarAndSlider {
            CGRect frame = CGRectMake(0.0, 0.0, 200.0, 30.0);
            UISlider *slider = [[UISlider alloc] initWithFrame:frame];
            [slider addTarget:self action:@selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];
            [slider setBackgroundColor:[UIColor clearColor]];
            slider.minimumValue = 0;
            slider.maximumValue = 8;
            slider.continuous = YES;
            slider.value = questionNumberForArray;
            //Change slider for UIToolbar
            UIBarButtonItem *skipQuestionSlider = [[UIBarButtonItem alloc] initWithCustomView:slider];
            // Set the width of aSlider
            [skipQuestionSlider setWidth:400.0];
            //Set up UIToolBar with Slider in it.
            UIToolbar *toolBar;
            CGRect rect2 = CGRectMake(0, toolBar.frame.origin.y , self.view.frame.size.width , 44);
            toolBar = [[UIToolbar alloc]initWithFrame:rect2];
            toolBar.barStyle = UIBarStyleBlackTranslucent;
            [toolBar setItems:[NSArray arrayWithObjects:skipQuestionSlider, nil]];
            [self.view addSubview:toolBar];
        }
        - (void)sliderValueChanged {
            NSInteger roundedSliderNumber = round(skipQuestionSlider.value);
            NSLog(@"Slider Value = %d", roundedSliderNumber);
        } 
    I've read the relevant class references and looked at alternative code for putting the UISlider into the UIToolbar, but no luck so far. I know I could have put the slider into the UIToolbar in interface builder, but I want to be able to slide to slide the toolbar down to the top of the screen and back out again, which I can't do with IB.
    Thanks for your help in advanced,

    Sam
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Just a thought: have you checked that the customView of skipQuestionSlider is the same as slider? It's possible initWithCustomView: copies the view passed...
     
  3. gwelmarten thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #3
    Sorry - I don't completely know what you mean and how I'd do this.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Code:
    if (slider != [skipQuestionSlider customView])
    {
    NSLog(@"slider is not the view on the toolbar);
    slider=[skipQuestionSlider customView]; // Now slider is pointing at the correct view
    }
    
    Also you are leaking slider as you alloc/init it but never release or autorelease it.


    Edit: Also you are are declaring slider and skipQuestionSlider as local variables in setupToolBarAndSlider. So these go out of scope at the end of that method: you cannot reference them in any other method. Finally you seem to try and get the value from the UIBarButtonItem skipQuestionSlider. I doubt that works. You probably need to send that message to the customView instead.
     
  5. gwelmarten thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #5
    I've got automatic reference counting on, so yes, I should remove the 3x alloc's in the code block I posted.
    However, when I do this, I get a 'no known class' error (screenshot).
     

    Attached Files:

  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Probably because that's an instance method not a class method. I don't use ARC so I can't advise on whether you should remove the alloc or not. I very much doubt it: without that you won't be allocating space in memory for the object.
     
  7. gwelmarten thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #7
    Sorry, I'm a little new to this specific area.

    So, I made that change and ran it again and moved the UISlider - I still get only 0's in my log.
     

    Attached Files:

  8. gwelmarten thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #8
    I've only just moved to ARC for this project, so again, not completely sure.
    I've looked on the relevant documents and googled a bit, and the consensus is to leave it (which does not give errors in my project, whereas removing it does).
    ARC does excuse me from release's though.
     
  9. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    Read the warnings. They are telling you exactly what I told you above: you have local variables in that method. They are in scope for that one method only. If you don't understand variable scope stop typing and go and learn the basics of C and then Objective-C as this is core, basic stuff.
     
  10. gwelmarten thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #10
    I do understand the basics of this, it's just that we are declaring UIBarButtonItem as the slider, and I am confused as to how this works when the slider is actually a UISlider. And I haven't declared skipQuestionSlider other than in this method, because I wasn't sure what to declare it as (UIBarButtonItem or UISlider).
     
  11. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    The compiler warnings say otherwise. They are telling you that you have a local variable hiding an instance variable.
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    This is a similar issue to the one you encountered in another thread of yours. I suggest you step back from the real coding and learn about 'variable scope'. It's a very important concept to understand.
     

Share This Page