在寻找我的第一个 iPhone 应用程序时,我发布了有关处理 iOS 键盘上返回键的正确方法。现在我需要找出键盘上方的工具栏,其中包含上一个/下一个和完成按钮。我一直在使用来自以下站点的示例: Input Accessory View
该示例有效,但我想使用 UIToolbar 和 UIBarButtonItem 而不仅仅是带有按钮的常规 View 。我尝试了各种组合,但都没有用。这是我目前所拥有的。
标题:
#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
来源:
#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 中可以做的那样。
谢谢。
更新:
所以我对 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 的 subview 。相反,使用工具栏上的 – setItems:animated: 方法来添加这些按钮。
关于objective-c - 将 UIToolbar 添加到某些文本字段的输入附件 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8954331/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以