3

我是 iOS 开发新手,在UISlider. 我已在连接到文件中添加了一个UISlider,并且可以在文件中看到操作方法,但是当我尝试在 UISlider 属性中设置文件中的最小值和最大值时,XCode 崩溃。我认为这与在文件中的方法中创建值有关,但我做错了,因为我尝试的一切都会导致 XCode 崩溃。.xib.h.m.xib- (IBAction)slider:(id)sender.m

4

4 回答 4

7
CGRect frame = CGRectMake(0.0, 0.0, 200.0, 10.0);

// sliderAction will respond to the updated slider value
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];

// Set minimum and maximum value of Slider

    slider.minimumValue = 0.0;
    slider.maximumValue = 10.0;
    slider.continuous = YES;

    // Initial value
    slider.value = 25.0;

// Add slider to view

    [self.view addSubview:slider];


- (IBAction)sliderAction:(id)sender{
    UISlider *MYslider = (UISlider *)sender;
    int SliderValue = (int)roundf(MYslider.value);
    uilable *sliderLabel.text = [NSString stringWithFormat:@"%d", SliderValue];
}
于 2012-10-12T10:58:14.117 回答
2

试试这个代码...

@interface SliderDemoViewController : UIViewController {

      UILabel *sliderLabel;
}

@end

将以下代码放入“SliderDemoViewController.m”文件中。

@implementation SliderDemoViewController

@synthesize sliderLabel;

- (void)dealloc {
  [sliderLabel release];
  [super dealloc];

}

-(IBAction) sliderChanged:(id) sender{

  UISlider *slider = (UISlider *) sender;

  int progressAsInt =(int)(slider.value + 0.5f);

  NSString *newText =[[NSString alloc]
                  initWithFormat:@"%d",progressAsInt];

  self.sliderLabel.text = newText;
  [newText release];
}


@end

并从下面的链接中查看有关 UISlider 的更多信息。

http://www.xprogress.com/post-35-uislider-tutorial-example-how-to-use-slider-in-iphone-sdk-xcode/

我希望这对你有帮助..

:)

于 2012-10-12T10:42:34.273 回答
1

make sure your slider is connected to the action method..and assuming that the label has an outlet by the name sliderLabel..the action method for the slider to call as it’s value is changed can be implemented as:

- (IBAction)slider:(id)sender{
    UISlider *slider = (UISlider *)sender;
    int progressAsInt = (int)roundf(slider.value);
    sliderLabel.text = [NSString stringWithFormat:@"%d", progressAsInt];
}

here the value is rounded off to show integer values only..I am assuming that the sider isnt being implemented for a very small range like 0-1 but a larger range..and you have mentioned that you implemented slider in nib..minimum and maximum values can be set in the attribute inspector for the slider..if you do it programatically, it will be slightly different..

于 2012-10-12T10:54:59.587 回答
0

斯威夫特 4:

初始化:

override func viewDidLoad() {
    super.viewDidLoad()

    let slider = UISlider()
    slider.frame = CGRect(x: 0, y: 0, width: 250, height: 35)
    slider.center = self.view.center

    slider.minimumTrackTintColor = .green
    slider.maximumTrackTintColor = .red
    slider.thumbTintColor = .black

    slider.maximumValue = 100
    slider.minimumValue = 0
    slider.setValue(50, animated: false)

    slider.addTarget(self, action: #selector(ViewController.changeVlaue(_:)), for: .valueChanged)

    self.view.addSubview(slider)

}

处理程序

@objc func changeVlaue(_ sender: UISlider) {
    print("value is" , Int(sender.value));
}
于 2018-05-31T03:28:18.777 回答