0

約 2 か月前に iOS プログラミングの学習を開始しましたが、今のところ気に入っていますが、作成中のアプリで UIPicker(s) と UIDatePickers に少し苦労しています。正しい方向。アプリは、既存の PHP Web アプリを模倣することになっています。これは、多数のドロップダウンを備えたフォームにすぎません。

たくさんの入力フィールドを持つ GetAddressViewController があるとしましょう。簡単にするために、国、都市、番地の 3 つの入力フィールドのみを使用するとします。

ユーザーが「country」入力フィールドをタップすると、UIPicker がキーボードのように表示され、国を選択すると、Web アプリはその国のすべての都市を含む JSON 配列を返します。ユーザーが「city」入力フィールドをタップすると、同じプロセスが繰り返され、返された都市のリストが UIPicker にポップアップ表示され、ユーザーが通りを選択すると、UIPicker がスライドアウトし、Web アプリが通りの配列を返します。

私の GetAddressViewController.h ファイルには、次のものがあるとしましょう。

#import <UIKit/UIKit.h>

@interface GetAddressViewController : UIViewController
{
    UITextField *countryField;
    UITextField *cityField;
    UITextField *streetField;

    NSArray *countries;
    NSArray *cities;
    NSArray *streets;
}

@property(nonatomic, strong) IBOutlet UITextField *countryField;
@property(nonatomic, strong) IBOutlet UITextField *cityField;
@property(nonatomic, strong) IBOutlet UITextField *streetField;

@property(nonatomic, strong) IBOutlet NSArray *countries;
@property(nonatomic, strong) IBOutlet NSArray *cities;
@property(nonatomic, strong) IBOutlet NSArray *streets;

@end

GetAddressViewController.m ファイルには、合成されたプロパティがあり、ストーリーボードには、適切なアウトレットに接続された 3 つの入力フィールドしかありません。既存のコードに、知っておくべき基本的な間違いはありますか?

StackOverflow で見つけたほとんどの例は、あまり意味をなさないため、ピッカー ビューに関するチュートリアルを読んでいて、何かを見逃しているように感じます。

誰かが私を助けることができる基本的な同様の例を教えてくれますか? UIPickers は進むべき道ですか、それともより良いアプローチがありますか?

しばらくの間、この無力さを感じたことはありません。どんな助けでも大歓迎です、ありがとう

編集:

同じ問題を抱えている他の誰かを助けることができることを願っています。これを機能させ、ピッカービューを入力フィールドのinputViewプロパティに接続するには、これを行う必要があります。

.h ファイルに、UIPickerViewDelegate と UIPickerViewDataSource の 2 つのデリゲートを追加する必要があります。

@interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>{ ...

次に、.m ファイルに次のようなものが必要です。wiewDidLoad メソッドにこのコードがあります。

UIPickerView *cityPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
    cityPicker.delegate = self;
    cityPicker.dataSource = self;
    [cityPicker setShowsSelectionIndicator:YES];
    cityField.inputView = cityPicker;

これは単に、cityField をタップすると cityPicker pickerview が表示されることを意味します。その後、pickerview に正しいデータを入力する次の pickerview デリゲート メソッドを追加する必要があります。私の場合のデータは、都市のリストを保持する配列です。

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return cities.count;
}

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

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

UIPicker を非表示にする "Done" が必要な場合は、次のコードが必要です。

UIToolbar*  mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
    mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
    [mypickerToolbar sizeToFit];

    NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked)];
    [barItems addObject:doneBtn];

    [mypickerToolbar setItems:barItems animated:YES];

    cityField.inputAccessoryView = mypickerToolbar;

新しいメソッド pickerDoneClicked を追加します

-(void)pickerDoneClicked
{
    NSLog(@"Done Clicked");
    [cityField resignFirstResponder];
}

これが誰かを助けることを願っています。

4

2 に答える 2

2

こんにちは、複数のピッカービューのこのソリューション全体に従うか、-(void)createActionsheetを使用して問題を解決してください

.h で定義

UIActionSheet *actionSheet;
NSString *pickerType;
BOOL select

.m で定義

-(IBAction)countryBtnPressed:(id)sender
{
    [self createActionSheet];
    pickerType = @"picker";
    select = NO;
    UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
    chPicker.dataSource = self;
    chPicker.delegate = self;
    chPicker.showsSelectionIndicator = YES;
    [actionSheet addSubview:chPicker];
    Txt.text = [array objectAtIndex:0];
    [chPicker release];
}



-(IBAction)stateBtnPressed:(id)sender
    {
        [self createActionSheet];
        pickerType = @"statepicker";
        select = NO;
        UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
        chPicker.dataSource = self;
        chPicker.delegate = self;
        chPicker.showsSelectionIndicator = YES;
        [actionSheet addSubview:chPicker];
        stateTxt.text = [stateArray objectAtIndex:0];
        [chPicker release];
    }

/// Create Action Sheet
- (void)createActionSheet {
if (actionSheet == nil) {
    // setup actionsheet to contain the UIPicker
    actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select"
                                              delegate:self
                                     cancelButtonTitle:nil
                                destructiveButtonTitle:nil
                                     otherButtonTitles:nil];

    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    pickerToolbar.barStyle = UIBarStyleBlackOpaque;
    [pickerToolbar sizeToFit];

    NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];
    [flexSpace release];

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDone:)];
    [barItems addObject:doneBtn];
    [doneBtn release];

    [pickerToolbar setItems:barItems animated:YES];
    [barItems release];

    [actionSheet addSubview:pickerToolbar];
    [pickerToolbar release];

    [actionSheet showInView:self.view];
    [actionSheet setBounds:CGRectMake(0,0,320, 464)];
}
}


#pragma mark UIPickerViewDelegate Methods

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    int count;
    if ([pickerType isEqualToString:@"picker"])
        count = [array count];
    else if([pickerType isEqualToString:@"picker"])
            count = [statearray count];
return count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *string;

    if ([pickerType isEqualToString:@"picker"])
        string = [array objectAtIndex:row];
    if ([pickerType isEqualToString:@"statepicker"])
        string = [statearray objectAtIndex:row];

return string;
}

// Set the width of the component inside the picker
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 300;
}

// Item picked
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    select = YES;
    if ([pickerType isEqualToString:@"picker"])
    {
        Txt.text = [array objectAtIndex:row];
    }
    if ([pickerType isEqualToString:@"statepicker"])
    {
        stateTxt.text = [statearray objectAtIndex:row];
    }

}

- (void)pickerDone:(id)sender
{
    if(select == NO)
    {
        if ([pickerType isEqualToString:@"picker"])
        {
            Txt.text = [array objectAtIndex:0];
        }
        else if ([pickerType isEqualToString:@"statepicker"])
        {
            stateTxt.text = [statearray objectAtIndex:0];
        }

}
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
    [actionSheet release];
    actionSheet = nil;

}

}
于 2012-05-24T09:28:46.477 に答える
1

UIPickersは使用しても問題ありませんが、この特定のケースでは、エントリのリストが非常に長いリストになる可能性があり、国名が「ジンバブエ共和国」の場合など、エントリが単純な文字列を超える場合もあります。 、アフリカ」またはそのようなUIPickerコントローラーは、可能な限り最良の方法でデータを表示できません。テーブルビューを使用して、それらをモーダルに表示できます。テーブルビューは、エントリのリストが100以上の場合に、選択を行うためのはるかに優れた方法を提供します。

于 2012-05-23T16:11:18.117 に答える