xcode 中的 UISlider
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13898034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UISlider in xcode
提问by trludt
I was wondering if anyone has any idea how to connect a text field with a slider. I want the slider to list numbers from 1-100 or 1-1000. I've looked places but there is no true answer to what I'm looking for. Your help would be greatly appreciated
我想知道是否有人知道如何将文本字段与滑块连接起来。我希望滑块列出 1-100 或 1-1000 之间的数字。我已经看过很多地方,但我在寻找什么没有真正的答案。您的帮助将不胜感激
回答by Programmer...
ViewController.h
视图控制器.h
@interface ViewController : UIViewController {
IBOutlet UISlider *mySlider;
IBOutlet UITextField *myTextField;
}
@property (nonatomic, retain) IBOutlet UISlider *mySlider;
@property (nonatomic, retain) IBOutlet UITextField *myTextField;
- (IBAction) sliderValueChanged:(id)sender;
- (IBAction) changeButtonPressed:(id)sender;
ViewController.m
视图控制器.m
#import "ViewController.h"
@implementation TutorialProjectViewController
@synthesize mySlider, myTextField;
- (void)viewDidLoad {
[super viewDidLoad];
self.myTextField.delegate = self;
}
- (IBAction) sliderValueChanged:(UISlider *)sender {
myTextField.text = [NSString stringWithFormat:@" %.1f", [sender value]];
}
- (IBAction) changeButtonPressed:(id)sender {
NSString *textValue = [myTextField text];
float value = [textValue floatValue];
if (value < 0) value = 0;
if (value > 100) value = 100;
mySlider.value = value;
myTextField.text = [NSString stringWithFormat:@"%.1f", value];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[myTextField resignFirstResponder];
//[put your textfield name here resignFirstResponder];
return NO;
}
@end
Change Slider Value:
更改滑块值:
回答by rmaddy
I'm assuming you want to update the text field's text as the slider is changed.
我假设您想在更改滑块时更新文本字段的文本。
You don't connect the two. You need to listen for changes to the slider's value. In the method that you setup for this, you need to convert the slider's value to the text you want. You then update the text field's text with this new text based on the slider's value.
你没有把两者联系起来。您需要监听滑块值的变化。在为此设置的方法中,您需要将滑块的值转换为所需的文本。然后,您可以根据滑块的值使用此新文本更新文本字段的文本。
The hook for the slider:
滑块挂钩:
[someSlider addTarget:self action:@selector(sliderUpdate:) forControlEvents:UIControlEventValueChanged];
The method that listens to the slider:
监听滑块的方法:
- (void)sliderUpdate:(UISlider *)slider {
float newVal = slider.value;
NSString *newText = [NSString stringWithFormat:@"%d", (int)newVal];
someTextField.text = newText;
}
This code assumes the slider is referenced in the someSlider
ivar and the text field is referenced by the someTextField
ivar. It also assumes the slider's minimumValue
is 0 and the maximumValue
is 100 or 1000 as needed.
此代码假定滑块在someSlider
ivar 中引用,而文本字段由someTextField
ivar引用。它还假定滑块minimumValue
为 0,maximumValue
根据需要为 100 或 1000。
回答by trludt
#import "FirstViewController.h"
@interface FirstViewController ()
@property (weak, nonatomic) IBOutlet UITextField *myTextBox;
@property (weak, nonatomic) IBOutlet UISlider *mySlider;
@property (weak, nonatomic) IBOutlet UINavigationBar *titleOnTop;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *backButton;
- (IBAction)changeGreeting:(id)sender;
- (IBAction)sliderValueChanged:(id)sender;
- (IBAction)changeButtonPressed:(id)sender;
@end
@implementation FirstViewController
@synthesize mySlider, myTextBox;
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTextBox.delegate = self;
}
- (IBAction) sliderValueChanged:(UISlider *)sender
{
myTextBox.text = [NSString stringWithFormat:@"%.1f", [sender value]];
}
- (void)sliderUpdate:(UISlider *)slider {
float newVal = slider.value;
NSString *newText = [NSString stringWithFormat:@"%d", (int)newVal];
myTextBox.text = newText;
}
- (IBAction) changeButtonPressed:(id)sender
{
NSString *textValue = [myTextBox text];
float value = [textValue floatValue];
if (value < 0) value = 0;
if (value > 1000) value = 1000;
mySlider.value = value;
myTextBox.text = [NSString stringWithFormat:@"%.1f", value];
if ([myTextBox canResignFirstResponder]) [myTextBox resignFirstResponder];
}
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
if (myTextBox)
{
if ([myTextBox canResignFirstResponder]) [myTextBox resignFirstResponder];
}
[super touchesBegan: touches withEvent: event];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload
{
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (IBAction)changeGreeting:(id)sender {
self.username = self.myTextBox.text;
NSString *nameString = self.username;
if ([nameString length] == 0) {
nameString = @"World";
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[myTextBox resignFirstResponder];
return YES;
}
@end
回答by trludt
#import "FirstViewController.h"
@interface FirstViewController ()
@property (weak, nonatomic) IBOutlet UITextField *myTextBox;
@property (weak, nonatomic) IBOutlet UISlider *mySlider;
@property (weak, nonatomic) IBOutlet UINavigationBar *titleOnTop;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *backButton;
- (IBAction)changeGreeting:(id)sender;
- (IBAction)sliderValueChanged:(id)sender;
- (IBAction)changeButtonPressed:(id)sender;
- (IBAction)backgroundTouched:(id)sender;
- (IBAction)textFieldReturn:(id)sender;
@end
@implementation FirstViewController
@synthesize mySlider, myTextBox;
- (IBAction)backgroundTouched:(id)sender;
{
[myTextBox resignFirstResponder];
}
- (IBAction)textFieldReturn:(id)sender;
{
[sender resignFirstResponder];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTextBox.delegate = self;
}
- (IBAction) sliderValueChanged:(UISlider *)sender
{
myTextBox.text = [NSString stringWithFormat:@"%.1f", [sender value]];
}
- (void)sliderUpdate:(UISlider *)slider {
float newVal = slider.value;
NSString *newText = [NSString stringWithFormat:@"%d", (int)newVal];
myTextBox.text = newText;
}
- (void)textFieldDidChange:(UITextField*)sender { slider.value = [sender.text floatValue]; }
- (IBAction) changeButtonPressed:(id)sender
{
NSString *textValue = [myTextBox text];
float value = [textValue floatValue];
if (value < 0) value = 0;
if (value > 1000) value = 1000;
mySlider.value = value;
myTextBox.text = [NSString stringWithFormat:@"%.1f", value];
if ([myTextBox canResignFirstResponder]) [myTextBox resignFirstResponder];
}
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
if (myTextBox)
{
if ([myTextBox canResignFirstResponder]) [myTextBox resignFirstResponder];
}
[super touchesBegan: touches withEvent: event];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload
{
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (IBAction)changeGreeting:(id)sender {
self.username = self.myTextBox.text;
NSString *nameString = self.username;
if ([nameString length] == 0) {
nameString = @"World";
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[myTextBox resignFirstResponder];
return NO;
}
@end