3

キーボードの代わりにtextFieldsinputViewプロパティを使用してUIPickerViewsを表示しています。私がプログラムした方法では、.HファイルでPickerViewsを宣言したり、InterfaceBuilderのビューでそれらを接続したりしません。ピッカービューのプロパティを実装するとき、それぞれを異なるプロパティで作成することはできません。次のように、使用されているピッカーに応じて異なるプロパティを持つように「if」ステートメントを使用してみました。

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)agePickerView {
    if ([pickerView isEqual: agePickerView]){
                 return 1;
    }
            if ([pickerView isEqual: weightPickerView]){
                 return 1;
    }

}

ただし、これを行うと、weightPickerViewが宣言されていないことがわかります。これがpickerViewsを実装するための私のコードです。

@implementation MainViewController

//WEIGHT FIELD CODE 

-(IBAction)weightFieldDidBeginEditing{
UIPickerView *weightPickerView = [[UIPickerView alloc] init];
weightField.inputView = weightPickerView;
weightPickerView.showsSelectionIndicator = YES;
weightPickerView.delegate = self;
weightPickerView.dataSource = self;
[weightPickerView release];

UIToolbar *weightToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
[weightToolBar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
 weightField.inputAccessoryView = weightToolBar;
}

-(NSInteger)weightpickerView:(UIPickerView *)weightPickerView numberOfRowsInComponent: (NSInteger)component{
return [weightPickerArray count];
}

-(NSString *)weightpickerView:(UIPickerView*)weightPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [weightPickerArray objectAtIndex:row];
}

// AGE FIELD CODE

-(IBAction)ageFieldDidBeginEditing{
UIPickerView *agePickerView = [[UIPickerView alloc] init];
ageField.inputView = agePickerView;
agePickerView.showsSelectionIndicator = YES;
agePickerView.delegate = self;
agePickerView.dataSource = self;
[agePickerView release];

UIToolbar *ageToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
[ageToolbar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
ageField.inputAccessoryView = ageToolbar; 
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)agePickerView {
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)agePickerView numberOfRowsInComponent: (NSInteger)component {
return [agePickerArray count];
}

-(NSString *)pickerView:(UIPickerView*)agePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [agePickerArray objectAtIndex:row];

}

-(void)pickerView:(UIPickerView *)agePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    ageField.text = (NSString *) [agePickerArray objectAtIndex:row];
}

-(void)pickerDoneClicked:(id)sender{
[ageField resignFirstResponder];
[weightField resignFirstResponder];
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a    nib.
- (void)viewDidLoad {
[super viewDidLoad];

agePickerArray = [[NSMutableArray alloc] init];
for (int age=1; age<=100; age++) {
    NSString *ageString = [NSString stringWithFormat:@"%d%",age];
                           [agePickerArray    addObject:ageString];


weightPickerArray = [[NSMutableArray alloc] init];
for (int weight = 50; weight<=500; weight++) {
    NSString *weightString = [NSString stringWithFormat:@"%d%",weight];
                            [weightPickerArray   addObject:weightString];
    }
}

私がやろうとしていることを行う他の方法はありますか?その方法は機能していません...そして、各ピッカービューの実装をやり直そうとすると、「...の再定義」「numberOfRowsInComponent」のエラーが発生します""または私が行っている他のプロパティ。変更して.HファイルでpickerViewsを宣言する必要がある場合でも、これを取得できる限り、何をしなければならないかは気にしません。仕事!!

ありがとう

アップデート!!!

   @interface MainViewController : UIViewController   <FlipsideViewControllerDelegate,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource> {

IBOutlet UITextField*ageField;

NSMutableArray *agePickerArray;
IBOutlet UITextField*weightField;
NSMutableArray *weightPickerArray;
UIPickerView *weightPickerView;
UIPickerView *agePickerView;    

   }

- (IBAction)ageFieldDidBeginEditing;
- (IBAction)weightFieldDidBeginEditing;
- (IBAction)showInfo:(id)sender;
- (IBAction)activityButtonClicked;
@end

.mファイル

-(IBAction)weightFieldDidBeginEditing{
//UIPickerView *weightPickerView = [[UIPickerView alloc] init];
weightField.inputView = weightPickerView;
weightPickerView.showsSelectionIndicator = YES;
weightPickerView.delegate = self;
weightPickerView.dataSource = self;
[weightPickerView release];

UIToolbar *weightToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
[weightToolBar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
 weightField.inputAccessoryView = weightToolBar;
}

-(NSInteger)weightpickerView:(UIPickerView *)weightPickerView numberOfRowsInComponent:(NSInteger)component{
return [weightPickerArray count];
}

-(NSString *)weightpickerView:(UIPickerView*)weightPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [weightPickerArray objectAtIndex:row];
}

//年齢フィールドコード

-(IBAction)ageFieldDidBeginEditing{
//UIPickerView *agePickerView = [[UIPickerView alloc] init];
ageField.inputView = agePickerView;
agePickerView.showsSelectionIndicator = YES;
agePickerView.delegate = self;
agePickerView.dataSource = self;
[agePickerView release];

UIToolbar *ageToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
[ageToolbar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
ageField.inputAccessoryView = ageToolbar; 
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)agePickerView {
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if ([pickerView isEqual: agePickerView]) {
    return [agePickerArray count];
}
if ([pickerView isEqual:weightPickerView]) {
    return [weightPickerArray count];
}
}

-(NSString *)pickerView:(UIPickerView*)agePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [agePickerArray objectAtIndex:row];

}

-(void)pickerView:(UIPickerView *)agePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    ageField.text = (NSString *) [agePickerArray objectAtIndex:row];
}

-(void)pickerDoneClicked:(id)sender{
[ageField resignFirstResponder];
[weightField resignFirstResponder];
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a   nib.
- (void)viewDidLoad {
[super viewDidLoad];

agePickerArray = [[NSMutableArray alloc] init];
for (int age=1; age<=100; age++) {
    NSString *ageString = [NSString stringWithFormat:@"%d%",age];
                           [agePickerArray   addObject:ageString];


weightPickerArray = [[NSMutableArray alloc] init];
for (int weight = 50; weight<=500; weight++) {
    NSString *weightString = [NSString stringWithFormat:@"%d%",weight];
                            [weightPickerArray addObject:weightString];
    }
}
4

2 に答える 2

7
UIPickerView *weightPickerView = [[UIPickerView alloc] init];

.hファイル(またはインターフェース)でそれを宣言する必要があります。

サブクラス化していUIPickerViewますか?

編集*何らかの理由で.hファイルでそれらを宣言したくない場合はtag、ピッカーにを設定します。

すべてのUIPickerViewがUIPickerViewDelegateメソッドを呼び出します。つまり、-(NSInteger)weightpickerView:(UIPickerView *)weightPickerView numberOfRowsInComponent: (NSInteger)componentサブクラス化して独自のメソッドを作成しない限り、呼び出されません(私はあなたがそうではないと信じています)。

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
  if([pickerView isEqual: agePickerView]){
   return [agePickerArray count];
  }else if([pickerView isEqual: weightPickerView]){
   return [weightPickerArray count];
  }else return 0;
}

タグを使用してそれを実行したい場合;

weightPickerView.tag = 100; //set the tag

if(pickerView.tag == 100) //check which picker it is
于 2012-07-29T05:00:29.940 に答える
1

各ピッカービューにタグを設定し、タグに基づいて異なる方法で処理します

于 2012-07-30T05:58:52.007 に答える