1

我有一个屏幕,上面有很多文本字段。对于一个特定的文本字段,我需要它显示一个选择器,用户可以从中选择状态名称,一旦用户通过单击状态名称选择了一个状态,选择器应该消失。

我不太确定如何做到这一点,我制作了一个看起来像这样的 Nib 文件。

笔尖

这是我的头文件:

#import <UIKit/UIKit.h>
#import "APIHelper.h"

@interface AddNewAddressViewController : UIViewController <UIPickerViewDelegate,              
UIPickerViewDataSource, UITextFieldDelegate>
{
    APIHelper *myAPIHelper;    
    NSMutableData *responseData;
    NSURLConnection *theConnection;

    UIPickerView *picker;
    NSArray *stateNames;

    @public IBOutlet UIImageView *imageNewAddressBox;
    @public IBOutlet UIImageView *imageNewAddressBox1;    
    @public IBOutlet UIImageView *imageNewAddressBox2;    
    @public IBOutlet UIImageView *imageNewAddressBox3;

    @public IBOutlet UILabel *labelNewAddress;    
    @public IBOutlet UILabel *labelStreetAddress;    
    @public IBOutlet UILabel *labelStreetAddressTwo;    
    @public IBOutlet UILabel *labelZipCode;    
    @public IBOutlet UILabel *labelState;    
    @public IBOutlet UILabel *labelCity;    
    @public IBOutlet UILabel *labelPhoneNumber;    

    @public IBOutlet UITextField *textFieldFullName;
    @public IBOutlet UITextField *textFieldStreetAddress;
    @public IBOutlet UITextField *textFieldPhoneNumber;
    @public IBOutlet UITextField *textFieldStreetAddressTwo;
    @public IBOutlet UITextField *textFieldCity;
    @public IBOutlet UITextField *textFieldState;
    @public IBOutlet UITextField *textFieldZipCode;

    @public IBOutlet UIButton *buttonNext;
}

@property (nonatomic, strong) IBOutlet UIPickerView *picker;
@property (nonatomic, strong) NSArray *stateNames;

@property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox;
@property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox1;
@property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox2;
@property (nonatomic, strong) IBOutlet UIImageView *imageNewAddressBox3;

@property (nonatomic, strong) IBOutlet UILabel *labelNewAddress;
@property (nonatomic, strong) IBOutlet UILabel *labelStreetAddress;
@property (nonatomic, strong) IBOutlet UILabel *labelStreetAddressTwo;
@property (nonatomic, strong) IBOutlet UILabel *labelZipCode;
@property (nonatomic, strong) IBOutlet UILabel *labelState;
@property (nonatomic, strong) IBOutlet UILabel *labelCity;
@property (nonatomic, strong) IBOutlet UILabel *labelPhoneNumber;

@property (nonatomic, strong) IBOutlet UITextField *textFieldFullName;
@property (nonatomic, strong) IBOutlet UITextField *textFieldStreetAddress;
@property (nonatomic, strong) IBOutlet UITextField *textFieldPhoneNumber;
@property (nonatomic, strong) IBOutlet UITextField *textFieldStreetAddressTwo;
@property (nonatomic, strong) IBOutlet UITextField *textFieldCity;
@property (nonatomic, strong) IBOutlet UITextField *textFieldState;
@property (nonatomic, strong) IBOutlet UITextField *textFieldZipCode;

@property (nonatomic, strong) IBOutlet UIButton *buttonNext;

-(IBAction)addressTextFieldShouldReturn:(id)sender;
-(IBAction)nextPressed;

@end

这是我的 .M 文件:

#import "AddNewAddressViewController.h"

@implementation AddNewAddressViewController

@synthesize imageNewAddressBox;
@synthesize imageNewAddressBox1;
@synthesize imageNewAddressBox2;
@synthesize imageNewAddressBox3;
@synthesize labelNewAddress;
@synthesize labelStreetAddress;
@synthesize labelStreetAddressTwo;
@synthesize labelZipCode;
@synthesize labelState;
@synthesize labelCity;
@synthesize labelPhoneNumber;
@synthesize textFieldFullName;
@synthesize textFieldStreetAddress;
@synthesize textFieldPhoneNumber;
@synthesize textFieldStreetAddressTwo;
@synthesize textFieldCity;
@synthesize textFieldState;
@synthesize textFieldZipCode;
@synthesize buttonNext;
@synthesize stateNames;
@synthesize picker;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.stateNames = [[NSArray alloc] initWithObjects:@"Alabama", @"Alaska", @"Arizona", @"Arkansas", @"California", @"Colorado", @"Connecticut", @"Delaware", @"District of Columbia", @"Florida", @"Georgia", @"Guam", @"Hawaii", @"Idaho", @"Illinois", @"Indiana", @"Iowa", @"Kansas", @"Kentucky", @"Louisiana", @"Maine", @"Maryland", @"Massachusetts", @"Michigan", @"Minnesota", @"Mississippi", @"Missouri", @"Montana", @"Nebraska", @"Nevada", @"New Hampshire", @"New Jersey", @"New Mexico", @"New York", @"North Carolina", @"North Dakota", @"Ohio", @"Oklahoma", @"Oregon", @"Pennsylvania", @"Puerto Rico", @"Rhode Island", @"South Carolina", @"South Dakota", @"Tennessee", @"Texas", @"Utah", @"Vermont", @"Virginia", @"Virgin Islands", @"Washington", @"West Virginia", @"Wisconsin", @"Wyoming", nil];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    // clicking outside the textfields removes keyboard
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(addressTextFieldShouldReturn:)];
    [self.view addGestureRecognizer:tap];    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setTitle: @"Update Address Book"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

-(IBAction)addressTextFieldShouldReturn:(id)sender
{
    [textFieldFullName resignFirstResponder];
    [textFieldStreetAddress resignFirstResponder];
    [textFieldStreetAddressTwo resignFirstResponder];
    [textFieldPhoneNumber resignFirstResponder];
    [textFieldState resignFirstResponder];
    [textFieldCity resignFirstResponder];
    [textFieldZipCode resignFirstResponder];
    [sender resignFirstResponder];
}

#pragma mark -
#pragma mark PickerView DataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [stateNames count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:    (NSInteger)component
{
    return [stateNames objectAtIndex:row];
}
@end

所以,我想要做的就是,当用户单击 textFieldState 时,会从底部弹出一个包含所有状态名称的选择器,然后用户单击状态名称,该名称将出现在 textFieldState 中。

任何帮助将不胜感激。谢谢。

4

2 回答 2

3

你可以使用 TextFielđelegate ,工作

#pragma  mark- text delegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if (textField==txtMonth) {
    [txtMonth resignFirstResponder];
    if (monthClick==0) {
        monthPicker.hidden=NO;
        monthPicker = [[AFPickerView alloc] initWithFrame:CGRectMake(98.0, 123.0, 126.0, 197.0)];
        monthPicker.dataSource = self;
        monthPicker.delegate = self;
        [monthPicker reloadData];
        if ([txtMonth.text isEqualToString:@""]) {

            txtMonth.text=[NSString stringWithFormat:@"%i",1];
        }
        [self.view addSubview:monthPicker];
        monthClick=1;
    }


}

}

于 2013-07-10T02:20:16.740 回答
2

我认为你需要为 UITextField 实现委托方法,当

- (void)textFieldDidBeginEditing:(UITextField *)textField 

方法被称为你设置uipicker的框架并显示它。

于 2013-07-10T02:17:05.347 回答