首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >resignFirstResponder UITextView

resignFirstResponder UITextView
EN

Stack Overflow用户
提问于 2011-07-19 21:03:57
回答 4查看 8.4K关注 0票数 2

我使用的是UITEXTVIEW。当按下“完成”按钮时,我正在尝试发送一个resignFirstResponder。我不知道如何触发包含resignFirstResponder行的方法。

我已经将UITextView的委托设置为Interface中的文件所有者。这是我的视图控制器的头和实现文件:

代码语言:javascript
复制
#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];
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-07-20 02:08:22

在查看了您的源代码之后,我发布的链接是毫无意义的。我以为你说的是键盘上的键。但是现在我看到你在说导航条上的实例UIButton,对吗?

到目前为止,您的addButton:isLast:方法看起来很不错,但是我会对它做一些修改,以使它符合苹果HIG

代码语言:javascript
复制
- (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:中添加以下行。

代码语言:javascript
复制
[self addButton:self isLast:NO];

last在您的addButton:isLast:中是多余的,因为它甚至没有被使用,所以我只传递任何东西(NO)。

您几乎完成了,但是如果按下该按钮,您的应用程序就会崩溃,因为finishQuestionnaire:不是在self (CommentQuestionViewController)上实现的。因为当用户按下“完成”按钮时,您想要关闭键盘,因此我们也将在这里辞去您作为第一个响应程序的文本视图。只需添加以下方法:

代码语言:javascript
复制
- (void)finishQuestionnaire:(id)sender
{
    [inputAnswer resignFirstResponder];
}
票数 1
EN

Stack Overflow用户

发布于 2011-07-19 21:38:30

你可以试试这个:

代码语言:javascript
复制
–(BOOL)textView:(UITextView*)textView shouldChangeCharactersInRange:(NSRange)range replacementText:(NSString*)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    else
        return YES;
}

当然,如果您这样做,您将无法键入实际的回车。

票数 2
EN

Stack Overflow用户

发布于 2014-06-30 09:36:48

是BOOL就这样..。

代码语言:javascript
复制
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }

    return YES;
}

如果不起作用,请检查下列事项。

  1. 是否将textView连接到类中定义的textView属性。(您可以在xib中看到这一点,并带有箭头图标)。

2.检查textView委托是否写入.h文件。

代码语言:javascript
复制
@interface YourViewController : UIViewController <UITextViewDelegate>

3.检查textView的委托是否已分配。

代码语言:javascript
复制
//set this in the viewDidLoad method
self.textView.delegate = self;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6754123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档