我试图在用户单击aUIPickerView
时显示 a 。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)];
}