将 UIToolbar 添加到某些文本字段的输入附件视图

2024-01-11

在我寻找我的第一个 iPhone 应用程序时,我发布了有关处理 iOS 键盘上的返回键的正确方法。现在我需要找出键盘上方的工具栏,其中包含上一个/下一个和完成按钮。我一直在使用以下网站的示例:输入配件视图 http://gabriel-tips.blogspot.com/2011/05/input-accessory-view-how-to-add-extra.html

该示例有效,但我想使用 UIToolbar 和 UIBarButtonItem's 而不仅仅是带有按钮的常规视图。我尝试过各种组合,但还没有成功。这是我到目前为止所拥有的。

Header:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>
{
    UITextField* txtActiveField;
    UIToolbar* keyboardToolbar;
    UIBarButtonItem* btnDone;
    UIBarButtonItem* btnNext;
    UIBarButtonItem* btnPrev;
}

@property (nonatomic, strong) IBOutlet UITextField* firstNameTextField;
@property (nonatomic, strong) IBOutlet UITextField* lastNameTextField;
@property (nonatomic, strong) IBOutlet UITextField* cityTextField;

@property (nonatomic, strong) UITextField* txtActiveField;
@property (nonatomic, strong) UIToolbar* keyboardToolbar;
@property (nonatomic, strong) UIBarButtonItem* btnDone;
@property (nonatomic, strong) UIBarButtonItem* btnNext;
@property (nonatomic, strong) UIBarButtonItem* btnPrev;

-(void)createInputAccessoryView;

@end

Source:

#import "ViewController.h"

@implementation ViewController

@synthesize firstNameTextField = _firstNameTextField;
@synthesize lastNameTextField = _lastNameTextField;
@synthesize cityTextField = _cityTextField;

@synthesize txtActiveField = _txtActiveField;
@synthesize keyboardToolbar = _keyboardToolbar;
@synthesize btnDone = _btnDone;
@synthesize btnNext = _btnNext;
@synthesize btnPrev = _btnPrev;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self createInputAccessoryView];
}

- (void)viewDidUnload
{
    [self setFirstNameTextField:nil];
    [self setLastNameTextField:nil];
    [self setCityTextField:nil];

    [self setTxtActiveField:nil];
    [self setKeyboardToolbar:nil];
    [self setBtnDone:nil];
    [self setBtnNext:nil];
    [self setBtnPrev:nil];

    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

-(void)gotoPrevTextfield
{
    if (self.txtActiveField == self.firstNameTextField)
    {
        return;
    }
    else if (self.txtActiveField == self.lastNameTextField)
    {
        [self.firstNameTextField becomeFirstResponder];
    }
    else if (self.txtActiveField == self.cityTextField)
    {
        [self.lastNameTextField becomeFirstResponder];
    }           
}

-(void)gotoNextTextfield
{
    if (self.txtActiveField == self.firstNameTextField)
    {
        [self.lastNameTextField becomeFirstResponder];
    }
    else if (self.txtActiveField == self.lastNameTextField)
    {
        [self.cityTextField becomeFirstResponder];
    }
    else if (self.txtActiveField == self.cityTextField)
    {
        return;
    }           
}

-(void)doneTyping
{
    [self.txtActiveField resignFirstResponder];
}

-(void)createInputAccessoryView
{
    self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent;
    self.keyboardToolbar.tintColor = [UIColor darkGrayColor];

    btnPrev = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(gotoPrevTextfield:)];
    btnNext = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(gotoNextTextfield:)];
    btnDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTyping:)];

    [self.keyboardToolbar addSubview:(UIView*)btnPrev];
    [self.keyboardToolbar addSubview:(UIView*)btnNext];
    [self.keyboardToolbar addSubview:(UIView*)btnDone];

    [self.firstNameTextField setInputAccessoryView:self.keyboardToolbar];
    [self.lastNameTextField setInputAccessoryView:self.keyboardToolbar];
    [self.cityTextField setInputAccessoryView:self.keyboardToolbar];
}

-(void)textFieldDidBeginEditing:(UITextField*)textField
{
    self.txtActiveField = textField;
}
@end

当我启动应用程序时,我得到:

012-01-21 09:31:19.798 KeyboardToolbar[14772:f803] -[UIBarButtonItem superview]: unrecognized selector sent to instance 0x6b19350
2012-01-21 09:31:19.799 KeyboardToolbar[14772:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIBarButtonItem superview]: unrecognized selector sent to instance 0x6b19350'
*** First throw call stack:
(0x13bc052 0x154dd0a 0x13bdced 0x1322f00 0x1322ce2 0x5042f 0x4a72b 0x3422 0x2a58 0xd964e 0x39a73 0x39ce2 0x39ea8 0x40d9a 0x11be6 0x128a6 0x21743 0x221f8 0x15aa9 0x12a6fa9 0x13901c5 0x12f5022 0x12f390a 0x12f2db4 0x12f2ccb 0x122a7 0x13a9b 0x2728 0x2685 0x1)
terminate called throwing an exception

我的假设是我没有正确分配其中一个元素。我还想知道是否需要在类中为按钮添加字段,或者是否可以将它们添加到工具栏中。我在标题中添加了一行,这样我就不必将所有方法都放在文件的开头。还不知道像在 C 中那样转发声明方法的语法。

Thanks.

Update:

所以我对 createInputAccessoryView 做了一些更改。我现在使用 addItems 来添加按钮。我将按钮变量设置为本地变量(但与以前一样时遇到了相同的问题)。好消息是应用程序不再崩溃,坏消息是工具栏显示,但没有任何按钮。不知道我还需要做什么才能让按钮实际显示。我见过的所有例子都是相似的。

-(void)createInputAccessoryView
{
    self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent;
    self.keyboardToolbar.tintColor = [UIColor darkGrayColor];

    UIBarButtonItem* previousButton = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStyleBordered target:self action:@selector(gotoPrevTextfield)];
    UIBarButtonItem* nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(gotoNextTextfield)];
    UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTyping:)];

    [keyboardToolbar setItems:[NSArray arrayWithObjects: previousButton, nextButton, flexSpace, doneButton, nil] animated:NO];

    [self.firstNameTextField setInputAccessoryView:self.keyboardToolbar];
    [self.lastNameTextField setInputAccessoryView:self.keyboardToolbar];
    [self.cityTextField setInputAccessoryView:self.keyboardToolbar];
}

UIBarButtonItem不是一个UIView子类,因此您不能将其添加为子视图UIToolbar。相反,使用– setItems:animated:工具栏上的方法来添加这些按钮。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 UIToolbar 添加到某些文本字段的输入附件视图 的相关文章

  • 是否有针对不同屏幕尺寸的单独故事板?

    基本上我已经完成了一个应用程序 我唯一的问题是 ATM 机应用程序在设计时只考虑了 4 英寸显示屏 当在 3 5 英寸模拟器上运行时 应用程序会丢失 0 5 英寸 显然 那么我的问题是 如何在 Xcode 5 中为不同的屏幕尺寸设置不同的故
  • SwiftUI 列表与右侧的部分索引?

    是否可以有一个在右侧有索引的列表 就像下面 SwiftUI 中的示例一样 我在 SwiftUI 中做了这个 Contacts swift TestCalendar Created by Christopher Riner on 9 11 2
  • 如何在 NSMutableArray 中实现“按值分组”?

    我正在使用 NSMutableArray 我想像在 SQL 中那样按日期获取值group by log date logMuArray log currenttime 4 30pm log date 11 12 2011 log durat
  • 在 Objective-C 中比较 2 个字符串

    我写了以下代码 if depSelectedIndice gt 1 comSelectedIndice gt 1 NSLog depart elemet d depSelectedIndice NSLog depart elemet d c
  • ios 用户如何取消 Facebook 登录?

    当用户到达此屏幕时 无法取消 我能做些什么 为了首先获得这个视图 我正在运行 NSMutableDictionary params NSMutableDictionary dictionaryWithObjectsAndKeys vid l
  • 使用 iOS 8 自定义键盘发送图像?

    我一直在为 iOS 8 开发自定义键盘 但在尝试使用键盘发送图像时偶然发现了一个问题 我做了一些研究 似乎没有一种简单的方法可以做到这一点UITextDocumentProxy因为只有NSStrings被允许 我是否忽略了使用自定义键盘发送
  • 从 UIImagePickerController 相机视图推送 viewController

    我正在开发一款消息应用程序 类似于 WhatsApp 用户可以互相发送文本和图像消息 当用户想要发送图像时 他可以从相机胶卷中选择一张图像 也可以用相机拍摄一张图像 这就是我介绍的方式UIImagePickerController对于这两种
  • 本地化现有的 iOS 应用程序

    我不敢相信以前没有人问过这个问题 要么是我的编码实践太无组织性 要么是我没有使用正确的关键字 How can I localize an existing iOS app that does not use NSLocalizedStrin
  • 网站在 iPhone 屏幕右侧显示空白区域

    我遇到问题http eiglaw com http eiglaw com iPhone 屏幕右侧显示约 25 像素宽的空白 边框 我在 stackoverflow 上研究了这个问题 这些帖子是相关的 但是当我尝试提供的各种解决方案时 我无法
  • 在横向中自动调整 UITableCells 内容的大小

    在 UITableView 中 我通过 UILabels 将内容添加到单元格中 定义最佳尺寸 与单元格宽度允许的一样大 我注意到只有tableView contentSize width是可靠的 因为cell contentView bou
  • GeoFire Swift 3 - 保存和更新坐标

    我正在尝试使用 GeoFire 将坐标存储到 Firebase 数据库中 我不确定如何更新新坐标 因为它们每秒都会更改 更新 随着childByAutoId 它正在为每辆自行车生成一个新的唯一 ID 如何引用这个唯一的自行车 ID 例如 用
  • 当地图视图只是屏幕的一部分时,如何在 iOS 模拟器中进行捏合?

    我在 iPad 上有一个视图 我正在添加MKMapView也就是说 全屏高度的一半 然而 当我尝试在 iOS 模拟器上进行捏合时 它不起作用 因为 to nubs 填充了模拟器上的整个 iPad 视图 And so with the map
  • 在 Xcode 5 中重命名 iOS 项目[重复]

    这个问题在这里已经有答案了 我需要重命名一个 iOS 项目 有没有办法在不开始一个全新项目的情况下做到这一点 我发现的所有其他信息都与 Xcode 4 或旧版本相关 这些方法似乎使项目崩溃 我在尝试任何名称更改之前创建了一个快照 在 Xco
  • 如何将 UILabel 的值绑定到实例变量?

    我是 mac objective c 的新手 我的问题是 我想知道是否可以将 UILabel 文本绑定到变量 而不必在值更改时手动设置文本 例如 在 Mac OS 上 当我打开新的 Finder 窗口并删除文件时 任务栏中的全局可用空间就会
  • 适用于 iPhone / iPad / iOS 的快速、精益 PDF 查看器 - 提示和提示?

    最近有很多关于绘制 PDF 的问题 是的 您可以使用UIWebView但这无法提供您所期望的优秀 PDF 查看器的性能和功能 您可以绘制PDF页面到 CALayer http www cocoabuilder com archive coc
  • 在 XCode 中链接静态 ObjC 库的过程

    我正在尝试链接到静态库 但不断收到链接器错误 我发现了一些发布示例的网站 但我无法看到我做错了什么 首先 我创建一个链接到我的库的项目 添加 gt 现有文件找到我的 xcodeproj 文件选择 将项目复制到目标组文件夹 选择我的宿主项目作
  • Firebase 身份验证问题 - 通过电子邮件地址检查用户是否存在

    我在 Firebase 上创建了一个帐户 它有效 但现在我想阻止人们使用已存在的电子邮件地址创建帐户 这是代码 DatabaseManager shared userExists with email completion weak sel
  • UIPanGestureRecognizer 对坐标的限制

    我在主 UIView 中添加了一个子视图 称为panel 并且我向其中添加了gestureRecognizer 因为我希望它只能在Y轴上拖动并且只能在某些限制下 即160 300 超过300它不能拖动 我以这种方式实现了手势处理 IBAct
  • 在 iOS 中,如何创建一个始终位于所有其他视图控制器之上的按钮?

    无论是否呈现模态或用户执行任何类型的转场 有没有办法让按钮在整个应用程序中 始终位于顶部 而不是屏幕顶部 有什么方法可以让这个按钮可拖动并可捕捉到屏幕上吗 我正在以苹果自己的辅助触摸作为此类按钮的示例 您可以通过创建自己的子类来做到这一点U
  • 如何让按钮闪烁?

    我试图在扫描正确时将按钮的颜色 只是闪烁 闪烁 更改为绿色 在出现问题时将按钮的颜色更改为红色 我可以用这样的视图来做到这一点 func flashBG UIView animateWithDuration 0 7 animations s

随机推荐