UISlider how to set the initial value

橙三吉。 提交于 2019-12-31 04:41:07

问题


I'm pretty new at this iphone dev stuff and i'm working on some existing code at a company. The uislider i'm trying to set the initial value on is actually in a UITableViewCell and is a custom control. I was thinking in the cell init

cell = (QuantitiesCell *)[self loadCellWithNibName:@"QuantitiesCell" forTableView:ltableView withStyle:UITableViewCellStyleDefault];

i could just call something like

((QuantitiesCell *)cell).value = 5;

The actual QuantitiesCell class has the member value and the following functions

-(void)initCell;{
    if (listOfValues == nil) {
        [self initListOfValues];
    }
    quantitiesSLider.maximumValue = [listOfValues count]-1;
    quantitiesSLider.minimumValue = 0;
    quantitiesSLider.value = self.value;
}

-(void)initListOfValues;{
    listOfValues = [[NSMutableArray alloc] initWithCapacity:10];
    int j =0;
    for (float i = minValue; i <= maxValue+increment; i=i+increment) {
        [listOfValues addObject:[NSNumber numberWithFloat: i]];
        if (i == value) {
            quantitiesSLider.value = j;
        }
        j++;
    }
}

like i said, i'm pretty new at this so if i need to post more of the code to show whats going to get help, let me know,

This slider always is defaulting to 1, the slider ranges usually from 1-10, and i want to set it to a specific value of the item i'm trying to edit.


回答1:


Setting slider.value is the correct way if your linkage is correct (you have made the connections in Interface Builder). If you have created a UISlider in code, all you have to do is set the value property. If that is not working then be sure firstly that the object is "live" (correctly allocated, not released, not out of scope etc.) and secondly that the functions in which you set slider.value are actually being called.

If you are using Interface Builder and are not sure of how to connect your slider as an IBOutlet, you should check out iTunes University - search for "CS193P" to find some excellent videos from Stanford University. The first couple will take you through making those connections. If using IB and you have not made the connection - nothing will happen.




回答2:


PS I had the same issuer - you need to add the UISlide view first then you can change the value.



来源:https://stackoverflow.com/questions/2878944/uislider-how-to-set-the-initial-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!