我正在尝试在用户点击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)];
}
答案 0 :(得分:0)
本地年龄声明隐藏了实例变量
这意味着你已经定义了一个已经被类变量使用的变量名(所以在这种情况下,你可能在类的某个地方声明了UITextField *age
,但是你不能重用变量名这个类中的age
因为编译器会将它与类变量混淆起来。
类'PracticePickerViewController'没有实现'UIActionsheetDelegate'协议。
您尚未将PracticePickerViewController设为UIActionsheetDelegate
。为此,您已将接口标题(PracticePickerViewController.h)更改为:
@interface PracticePickerViewController : UIViewController <UITextFieldDelegate, UIActionsheetDelegate>{
不要忘记实现所需的委托方法!
最后,我强烈建议您查看ActionSheetPicker project 。
这是一个完全符合您要实施的功能的插入式课程,因此您可以或者用它替换你当前的代码,或者只是用它作为参考,看看如何正确地在UIPickerView中实现嵌入式ActionSheet。希望这一切都有所帮助!