Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

gwelmarten

macrumors 6502
Original poster
Jan 17, 2011
476
0
England!
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
 
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.
 
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.

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).
 

Attachments

  • Screen Shot 2012-07-10 at 13.25.16.png
    Screen Shot 2012-07-10 at 13.25.16.png
    23.1 KB · Views: 115
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).

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

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.
 

Attachments

  • Screen Shot 2012-07-10 at 13.30.30.png
    Screen Shot 2012-07-10 at 13.30.30.png
    180.5 KB · Views: 134
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.

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

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).
 
And I haven't declared skipQuestionSlider other than in this method, because I wasn't sure what to declare it as (UIBarButtonItem or UISlider).

The compiler warnings say otherwise. They are telling you that you have a local variable hiding an instance variable.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.