我使用的是UITEXTVIEW。当按下“完成”按钮时,我正在尝试发送一个resignFirstResponder。我不知道如何触发包含resignFirstResponder行的方法。
我已经将UITextView的委托设置为Interface中的文件所有者。这是我的视图控制器的头和实现文件:
#import <Foundation/Foundation.h>
#import "Question.h"
@interface CommentQuestionViewController : UIViewController {
Question *question;
IBOutlet UILabel *questionTitle;
IBOutlet UITextView *inputAnswer; //this is the textview
}
@property (nonatomic, retain) UILabel *questionTitle;
@property (nonatomic, retain) Question *question;
@property (nonatomic, retain) UITextView *inputAnswer;
- (void) addButton:(id)sender isLast:(BOOL)last;
- (void) setQuestionId:(NSString*)quId withTitle:(NSString*)quTitle number:(NSString*)quNum section:(NSString*)sId questionType:(NSString*)qt;
@end
#import "CommentQuestionViewController.h"
@implementation CommentQuestionViewController
@synthesize questionTitle, question, inputAnswer;
- (void) addButton:(id)delegate isLast:(BOOL)last{
UIBarButtonItem *anotherButton;
anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:delegate action:@selector(finishQuestionnaire:)];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
}
-(void)viewDidLoad{
//self.questionTitle.text = question.qTitle;
[[self questionTitle] setText:[question qTitle]];
[super viewDidLoad];
NSString *str = [NSString stringWithFormat:@"Question %@", [question qNumber]];
//self.title = str;
[self setTitle:str];
[str release];
}
-(void) setQuestionId:(NSString*)quId withTitle:(NSString*)quTitle number:(NSString*)quNum section:(NSString*)sId questionType:(NSString*)qt{
question = [[Question alloc]init];
[question setQId:quId];
[question setQTitle:quTitle];
[question setQNumber:quNum];
[question setSectionId:sId];
[question setQType:qt];
}
- (void) viewWillAppear:(BOOL)animated{
self.navigationItem.hidesBackButton = YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void) textViewDidEndEditing:(UITextView*)textView{
[textView resignFirstResponder];
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[inputAnswer release];
[questionTitle release];
[question release];
[super dealloc];
}发布于 2011-07-20 02:08:22
在查看了您的源代码之后,我发布的链接是毫无意义的。我以为你说的是键盘上的键。但是现在我看到你在说导航条上的实例UIButton,对吗?
到目前为止,您的addButton:isLast:方法看起来很不错,但是我会对它做一些修改,以使它符合苹果HIG
- (void)addButton:(id)delegate isLast:(BOOL)last
{
UIBarButtonItem *anotherButton;
anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:delegate
action:@selector(finishQuestionnaire:)];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
}它几乎是一样的,除了这一个创建一个按钮与适当的风格。
无论如何,我不知道如何添加按钮,因为addButton:isLast:在viewDidLoad:中没有被调用。在viewDidLoad:中添加以下行。
[self addButton:self isLast:NO];last在您的addButton:isLast:中是多余的,因为它甚至没有被使用,所以我只传递任何东西(NO)。
您几乎完成了,但是如果按下该按钮,您的应用程序就会崩溃,因为finishQuestionnaire:不是在self (CommentQuestionViewController)上实现的。因为当用户按下“完成”按钮时,您想要关闭键盘,因此我们也将在这里辞去您作为第一个响应程序的文本视图。只需添加以下方法:
- (void)finishQuestionnaire:(id)sender
{
[inputAnswer resignFirstResponder];
}发布于 2011-07-19 21:38:30
你可以试试这个:
–(BOOL)textView:(UITextView*)textView shouldChangeCharactersInRange:(NSRange)range replacementText:(NSString*)text {
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
else
return YES;
}当然,如果您这样做,您将无法键入实际的回车。
发布于 2014-06-30 09:36:48
是BOOL就这样..。
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}如果不起作用,请检查下列事项。
2.检查textView委托是否写入.h文件。
@interface YourViewController : UIViewController <UITextViewDelegate>3.检查textView的委托是否已分配。
//set this in the viewDidLoad method
self.textView.delegate = self;https://stackoverflow.com/questions/6754123
复制相似问题