将 UITextField 的 inputAccessoryView 设置为其超级视图

发布于 2024-11-25 19:55:01 字数 663 浏览 1 评论 0原文

我有一个带有“添加单元格...”行的 UITableView,并且希望弹出一个键盘,上面有一个视图,就像在“消息”应用程序中一样,用户可以在其中键入新单元格的名称。

我意识到还有几种其他方法来获取用户数据,以及多种方法来实现我想要实现的功能。

例如,当应创建新的播放列表时,iPod 应用程序会显示一个弹出窗口。

现在,我有一个隐藏文本字段,当按下“添加单元格...”行时,该字段设置为第一个响应者,并且我将包含输入字段和确认按钮的视图指定为该隐藏字段的 inputAccessoryView。或者,我可以将此视图添加为表的子视图,并使用键盘通知定位它。

我只是想知道是否有一种更清晰的方法来完成我想要做的事情(例如将输入文本字段的 inputAccessoryView 设置为显示为文本字段的超级视图)。我尝试了几种方法,但当视图应该关闭时,我似乎无法使用 resignFirstResponder 关闭键盘。我可以让 inputAccessoryView 消失,但键盘仍然保持打开状态,占用必要的屏幕空间。另外,当笔尖的视图是一个以 UITableViewController 作为文件所有者的 UITableView,而不是一个以 UIViewController 作为文件所有者的 UIView 时,我会收到一个非常奇怪的错误:“设置表的第一响应者视图,但我们不知道其类型(单元格/页眉/页脚)”

提前致谢, 朱利安·塞佩克

I have a UITableView with an Add cell... row and would like to have a keyboard pop up with a view above it like in the "Messages" application, where the user can type the name of the new cell.

I realize that there are several other ways to get user data, and several ways to implement the functionality that I am trying to achieve.

For example, the iPod application presents a popup when a new playlist should be created.

Right now, I have a hidden text field that is set to be the first responder when the Add cell... row is pressed, and I assign the view containing the input field and confirmation button as the inputAccessoryView for this hidden field. Alternatively, I could add this view as a subview of the table and position it using keyboard notifications.

I would just like to know if there is a cleaner way to accomplish what I am trying to do (such as setting the inputAccessoryView of the input textField to be displayed to be the textField's superview). I have tried several approaches, but I cannot seem to be able to dismiss the keyboard using resignFirstResponder when the view should close. I can get the inputAccessoryView to disappear, but the keyboard remains resolutely open, taking up necessary screen real estate. Also, when the nib's view is a UITableView with a UITableViewController as the File's Owner rather than a UIView with a UIViewController as the File's Owner, I get a very odd error: "setting the first responder view of the table but we don't know its type (cell/header/footer)"

Thanks in advance,
Julian Ceipek

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2024-12-02 19:55:01

通过 UITextView/UITextField 类上的 setInputAccessoryView,您的方向是正确的。此方法允许您将想要的任何视图添加到键盘顶部。
然后,您创建的视图将使用委托方法来告诉主视图控制器 resignFirstResponder。

因此,为了让您开始:

@protocol TextFieldAccessoryViewDelegate
@required
- (void)keyboardShouldDismiss;

@end

@interface TextFieldAccessoryView : UIView
@property (nonatomic, retain) id<TextFieldAccessoryViewDelegate> delegate;

- (id)initWithFrame:(CGRect)frame withDelegate (id<TextFieldAccessoryViewDelegate>)aDelegate;

@end

实现可能看起来有点像(仅发布生成视图的代码):

#pragma mark - Private methods
- (void)doneButtonTapped:(id)sender
{
    [delegate keyboardShouldDismiss];
}

- (void)setUpChildrenView
{
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonTapped:)];

    UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@""];
    [navigationItem setRightBarButtonItem:doneButton];
    [navigationItem setHidesBackButton:YES];

    UINavigationBar *navigationBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.width, 44.0f)] autorelease];
    [navigationBar pushNavigationItem:navigationItem animated:NO];

    [self addSubview:navigationBar];
}

我使用了标准的导航栏外观视图,但您可以放入任何您喜欢的内容,包括按钮、文本字段、图像机器人独角兽,工作

如果您没有了解上面代码中发生的所有内容,您可能需要温习委派并以编程方式创建视图。

You are on the right track with the setInputAccessoryView on the UITextView/UITextField classes' This method allows you to add any view you want to the top of a keyboard.
The view you create would then use a delegation method to tell the main view controller to resignFirstResponder.

So, to get you started:

@protocol TextFieldAccessoryViewDelegate
@required
- (void)keyboardShouldDismiss;

@end

@interface TextFieldAccessoryView : UIView
@property (nonatomic, retain) id<TextFieldAccessoryViewDelegate> delegate;

- (id)initWithFrame:(CGRect)frame withDelegate (id<TextFieldAccessoryViewDelegate>)aDelegate;

@end

The implementation might look a little like (only posting the code that makes the view):

#pragma mark - Private methods
- (void)doneButtonTapped:(id)sender
{
    [delegate keyboardShouldDismiss];
}

- (void)setUpChildrenView
{
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonTapped:)];

    UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@""];
    [navigationItem setRightBarButtonItem:doneButton];
    [navigationItem setHidesBackButton:YES];

    UINavigationBar *navigationBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.width, 44.0f)] autorelease];
    [navigationBar pushNavigationItem:navigationItem animated:NO];

    [self addSubview:navigationBar];
}

I have used a standard NavigationBar looking view, but you could put in anything you like and include buttons, textfields, images of robot unicorns, the works

If you don't get everything thats going on in the above code you might need to brush up on Delegation and creating views programmatically.

莫相离 2024-12-02 19:55:01
  - (void)viewDidLoad
  {
    [self createAccessoryView];

    [textField setDelegate:self];
    [textField setKeyboardType:UIKeyboardTypeDefault];
    [textField setInputAccessoryView:fieldAccessoryView];


  }


- (void)createAccessoryView
    {

        CGRect frame = CGRectMake(0.0, self.view.bounds.size.height, self.view.bounds.size.width, 44.0);
        fieldAccessoryView = [[UIToolbar alloc] initWithFrame:frame];
        fieldAccessoryView.barStyle = UIBarStyleBlackOpaque;
        fieldAccessoryView.tag = 200;

        [fieldAccessoryView setBarStyle:UIBarStyleBlack];

        UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone  target:self action:@selector(done:)];

        UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Previous", @""), NSLocalizedString(@"Next", @""), nil]];
        [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        [segmentedControl setMomentary:YES];
        UIBarButtonItem *segmentButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];

        [fieldAccessoryView setItems:[NSArray arrayWithObjects:segmentButton, spaceButton, doneButton, nil] animated:NO];
        [segmentButton release];
        [spaceButton release];
        [doneButton release];
        [segmentedControl release];

    }
  - (void)viewDidLoad
  {
    [self createAccessoryView];

    [textField setDelegate:self];
    [textField setKeyboardType:UIKeyboardTypeDefault];
    [textField setInputAccessoryView:fieldAccessoryView];


  }


- (void)createAccessoryView
    {

        CGRect frame = CGRectMake(0.0, self.view.bounds.size.height, self.view.bounds.size.width, 44.0);
        fieldAccessoryView = [[UIToolbar alloc] initWithFrame:frame];
        fieldAccessoryView.barStyle = UIBarStyleBlackOpaque;
        fieldAccessoryView.tag = 200;

        [fieldAccessoryView setBarStyle:UIBarStyleBlack];

        UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone  target:self action:@selector(done:)];

        UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Previous", @""), NSLocalizedString(@"Next", @""), nil]];
        [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        [segmentedControl setMomentary:YES];
        UIBarButtonItem *segmentButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];

        [fieldAccessoryView setItems:[NSArray arrayWithObjects:segmentButton, spaceButton, doneButton, nil] animated:NO];
        [segmentButton release];
        [spaceButton release];
        [doneButton release];
        [segmentedControl release];

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文