辞职第一响应者 UITextView
我正在使用UITEXTVIEW。我试图在按下完成按钮时发送一个 resignFirstResponder 。我不确定如何触发包含 resignFirstResponder 行的方法。
我已将 UITextView 的委托设置为 Interface Builder 中的文件所有者。这是我的视图控制器的头文件和实现文件:
#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];
}
I am using a UITEXTVIEW. I am trying to send a resignFirstResponder when the done button is pressed. I am not sure how to trigger the method containing the resignFirstResponder line.
I have set the UITextView's delegate to the File's Owner in Interface Builder. This is my viewcontroller's header and implementation file:
#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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试这样做:
当然,如果您这样做,您将无法键入实际的回车符。
You can try this:
Of course, you won't be able to type actual carriage returns if you do this.
看了你的源代码后,我发布的链接毫无意义。我以为你在谈论键盘上的完成键。但现在我看到你正在谈论导航栏上的一个实例
UIButton
,对吧?到目前为止,您的
addButton:isLast:
方法看起来很棒,但我会对其进行一些更改,以便它遵循 Apple HIG:几乎相同,除了这个创建了一个按钮具有适当的风格。
无论如何,我不知道如何添加按钮,因为在
viewDidLoad:
中没有调用addButton:isLast:
。在viewDidLoad:
中添加以下行。last
在您的addButton:isLast:
中是多余的,因为它甚至没有被使用,所以我只是传递任何内容(NO
)。您即将完成,但如果按下该按钮,您的应用程序将会崩溃,因为
finishQuestionnaire:
未在self
(CommentQuestionViewController
) 上实现。由于您希望在用户按下“完成”按钮时关闭键盘,因此我们还将放弃您作为第一响应者的文本视图。只需添加以下方法:After looking at your source code, the link that I had posted is pointless. I thought you were talking about the done key on the keyboard. But now I see that you were talking about an instance
UIButton
on the navigation bar, right?Your
addButton:isLast:
method is looking great so far, but I'd change it a bit so it adheres to the Apple HIG:It's pretty much the same, except this one creates a button with the appropriate style.
Anyway, I don't see how the button is being added since
addButton:isLast:
is not being called inviewDidLoad:
. Add the following line in yourviewDidLoad:
.last
is redundant in youraddButton:isLast:
since it's not even being use so I just pass whatever (NO
).You're almost done, but if you press that button, your application would crash since
finishQuestionnaire:
is not implemented onself
(CommentQuestionViewController
). And since you want to dismiss the keyboard when the user presses your done button, that's where we will also resign your text view as first responder. Just add the following method:就是 BOOL 就是这样。
如果这不起作用,请检查以下内容。
2.检查你的.h文件中是否写入了textView委托。
3.检查textView的delegate是否被分配。
It's BOOL that's it..
If that doesn't work please check the following things..
2.Check whether the textView delegate is written in your .h file.
3.Check whether the textView's delegate is assigned.
通过此方法检查换行符。
然后检查文本的长度,如果是一个字符长度,则输入新行字符,而不是粘贴。
Check for new line character by this method.
Then check the length of the text, if it is one character length, then new line character is entered, not pasted.