resignFirstResponder UITextView

时间:2011-07-19 21:03:57

标签: iphone objective-c xcode iphone-softkeyboard

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

我已将UITextView的委托设置为Interface Builder中的文件所有者。这是我的viewcontroller的头文件和实现文件:

#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];
}

4 个答案:

答案 0 :(得分:2)

你可以试试这个:

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

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

答案 1 :(得分:1)

查看源代码后,我发布的链接毫无意义。我以为你在谈论键盘上的完成键。但是现在我看到你在导航栏上讨论了一个实例UIButton,对吧?

到目前为止,您的addButton:isLast:方法看起来很棒,但我会稍微更改一下,以便它遵守Apple 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];

lastaddButton:isLast:中是多余的,因为它甚至没有使用,所以我只是传递任何内容(NO)。

你差不多完成了,但如果你按下那个按钮,你的应用程序就会崩溃,因为finishQuestionnaire:self)上没有实现CommentQuestionViewController。而且,由于您想在用户按下完成按钮时关闭键盘,我们也会将您的文本视图作为第一响应者进行重新调整。只需添加以下方法:

- (void)finishQuestionnaire:(id)sender
{
    [inputAnswer resignFirstResponder];
}

答案 2 :(得分:1)

它的BOOL是它的......

- (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. 2.检查textView委托是否写在.h文件中。

    @interface YourViewController : UIViewController <UITextViewDelegate>
    

    3.检查是否已分配textView的代理。

    //set this in the viewDidLoad method
    self.textView.delegate = self;
    

答案 3 :(得分:0)

通过此方法检查新行字符。

  

- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet

然后检查文本的长度,如果是一个字符长度,则输入新行字符,而不是粘贴。

   - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
     BOOL hasNewLineCharacterTyped = (text.length == 1) && [text rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location != NSNotFound;
     if (hasNewLineCharacterTyped) {
         [textView resignFirstResponder];
         return NO;
     }
      return YES;
    }