【发布时间】:2015-09-13 04:58:52
【问题描述】:
我想在点击按钮时显示键盘,但情况是键盘具有附件视图,即文本字段。简而言之,我想用文本字段的附件视图打开键盘。哪种方法是正确的..?
我进行了很多搜索,但这些解决方案都没有帮助我实现这一目标。 请帮帮我。
这是我的代码:
ViewController.m
@interface ViewController () <UITextFieldDelegate>
{
UITextField *txtNote;
}
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *accView = [self createAccessoryView];
[txtNote setInputAccessoryView:accView];
}
- (UIView *)createAccessoryView
{
UIView *accessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
[accessoryView setBackgroundColor:[UIColor lightGrayColor]];
[accessoryView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin];
CGFloat x = self.view.frame.size.width-65;
txtNote = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, x-5, 35)];
txtNote.delegate = self;
txtNote.font = [UIFont systemFontOfSize:IS_IPAD?20.0:14.0];
[accessoryView addSubview:txtNote];
UIButton *btnSave = [UIButton buttonWithType:UIButtonTypeCustom];
btnSave.frame = CGRectMake(x, 5, 60, 35);
btnSave.layer.cornerRadius = 5.0f;
btnSave.clipsToBounds = TRUE;
btnSave.titleLabel.font = [UIFont systemFontOfSize:18.0f];
[btnSave setTitle:@"Save" forState:UIControlStateNormal];
[btnSave setBackgroundColor:[UIColor blackColor]];
[btnSave setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnSave addTarget:self action:@selector(doneWithNumberPad:) forControlEvents:UIControlEventTouchUpInside];
[btnSave setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin];
[accessoryView addSubview:btnSave];
return accessoryView;
}
- (IBAction)addNote:(UIButton *)button
{
// On this click, I want to show keyboard.
[txtNote becomeFirstResponder];
}
- (IBAction)doneWithNumberPad:(id)sender
{
[txtNote resignFirstResponder];
}
【问题讨论】:
标签: ios keyboard uitextfield