簡體   English   中英

單擊UITextField時使用工具欄的UIPickerView

[英]UIPickerView with toolbar when UITextField is clicked

我正在嘗試讓UIPickerView在用戶單擊UITextField時顯示。 我還希望UIPickerView在頂部具有一個工具欄,其按鈕用於提交選擇和取消。

我需要在界面生成器中連接UIPickerView在其他位置連接操作? 構建時也會收到警告。 resignFirstResponder下有一個警告。 它說:

本地age聲明隱藏實例變量

與以上針對不同變量的警告類似,還有其他幾種警告。

另一個警告是:

類“ PracticePickerViewController”未實現“ UIActionsheetDelegate”協議。

這是我的.h.m文件:

。H

#import <UIKit/UIKit.h>

@interface PracticePickerViewController : UIViewController <UITextFieldDelegate>{

    IBOutlet UITextField *age;
    IBOutlet UIPickerView *agePickerView;
    IBOutlet UIActionSheet *actionSheet;
    IBOutlet UIToolbar *pickerToolbar;

}


@end

.m

#import "PracticePickerViewController.h"

@implementation PracticePickerViewController

-(void)textFieldDidBeginEditing:(UITextField *)age{
    [age resignFirstResponder];

    actionSheet = [[UIActionSheet alloc] initWithTitle:@"Pick Your Age" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    CGRect pickerFrame = CGRectMake(0,40,0,0);

    UIPickerView *agePickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    agePickerView.showsSelectionIndicator = YES;
    agePickerView.dataSource = self;
    agePickerView.delegate = self;
    [actionSheet addSubview:agePickerView];

    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];

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

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
    [barItems addObject:cancelBtn];

    [pickerToolbar setItems:barItems animated:YES];

    [actionSheet addSubview:pickerToolbar];
    [actionSheet addSubview:agePickerView];
    [actionSheet showInView:self.view];
    [actionSheet setBounds:CGRectMake(0,0,320,485)];

}

本地年齡聲明隱藏實例變量

這意味着您已經定義了一個已經由類變量使用的變量名(因此,在這種情況下,您可能在類的某個地方聲明了UITextField *age ,但是您不能在此類中重復使用變量名age ,因為編譯器會將其與class變量混淆)。

類“ PracticePickerViewController”未實現“ UIActionsheetDelegate”協議。

您尚未將PracticePickerViewController設置為UIActionsheetDelegate 為此,您已將接口標頭(PracticePickerViewController.h)更改為:

@interface PracticePickerViewController : UIViewController <UITextFieldDelegate, UIActionsheetDelegate>{

不要忘記也實現必需的委托方法!

最后,我強烈建議您檢查ActionSheetPicker項目
這是一個嵌入式類,完全具有您要實現的功能,因此您可以用該類替換當前代碼,或者僅將其用作參考,以了解如何在UIPickerView中正確實現嵌入式ActionSheet。 希望所有的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM