1

我想知道是否有人知道如何将文本字段与滑块连接起来。我希望滑块列出 1-100 或 1-1000 的数字。我已经看过一些地方,但对于我正在寻找的东西没有真正的答案。您的帮助将不胜感激

4

4 回答 4

2

视图控制器.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;

视图控制器.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

更改滑块值:

在此处输入图像描述

于 2012-12-16T02:34:37.190 回答
1

我假设您想在更改滑块时更新文本字段的文本。

你没有把两者联系起来。您需要监听滑块值的变化。在为此设置的方法中,您需要将滑块的值转换为所需的文本。然后,您可以根据滑块的值使用此新文本更新文本字段的文本。

滑块的挂钩:

[someSlider addTarget:self action:@selector(sliderUpdate:) forControlEvents:UIControlEventValueChanged];

监听滑块的方法:

- (void)sliderUpdate:(UISlider *)slider {
    float newVal = slider.value;

    NSString *newText = [NSString stringWithFormat:@"%d", (int)newVal];

    someTextField.text = newText;
}

此代码假定在 ivar 中引用了滑块,并且在someSliderivar 中引用了文本字段someTextField。它还假设滑块minimumValue为 0,maximumValue根据需要为 100 或 1000。

于 2012-12-16T02:35:51.523 回答
1
#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
于 2012-12-16T21:29:11.497 回答
1
  #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
于 2012-12-16T23:02:37.807 回答