xcode resignFirstResponder UITextView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6754123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:31:49  来源:igfitidea点击:

resignFirstResponder UITextView

iphoneobjective-cxcodeiphone-softkeyboard

提问by user559142

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.

我正在使用UITEXTVIEW。当按下完成按钮时,我试图发送一个 resignFirstResponder。我不确定如何触发包含 resignFirstResponder 行的方法。

I have set the UITextView's delegate to the File's Owner in Interface Builder. This is my viewcontroller's header and implementation file:

我已将 UITextView 的委托设置为 Interface Builder 中的 File's Owner。这是我的视图控制器的头文件和实现文件:

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

采纳答案by Espresso

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 UIButtonon the navigation bar, right?

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

Your addButton:isLast:method is looking great so far, but I'd change it a bit so it adheres to the Apple HIG:

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

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 in viewDidLoad:. Add the following line in your viewDidLoad:.

无论如何,我看不到按钮是如何添加的,因为addButton:isLast:没有被调用viewDidLoad:。在您的viewDidLoad:.

[self addButton:self isLast:NO];

lastis redundant in your addButton:isLast:since it's not even being use so I just pass whatever (NO).

last对您addButton:isLast:来说是多余的,因为它甚至没有被使用,所以我只是通过了 ( NO) 。

You're almost done, but if you press that button, your application would crash since finishQuestionnaire:is not implemented on self(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:

你快完成了,但是如果你按下那个按钮,你的应用程序会崩溃,因为finishQuestionnaire:它没有在self( CommentQuestionViewController)上实现。并且由于您想在用户按下完成按钮时关闭键盘,因此我们还将您的文本视图退出为第一响应者。只需添加以下方法:

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

回答by med200

You can try this:

你可以试试这个:

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

Of course, you won't be able to type actual carriage returns if you do this.

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

回答by Gokul

It's BOOL that's it..

这是 BOOL 就是这样..

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

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

    return YES;
}

If that doesn't work please check the following things..

如果这不起作用,请检查以下事项..

  1. Whether your textView is connected to the textView property defined in your class.. (You can see this in your xib, with an arrow icon).
  1. 您的 textView 是否连接到您的类中定义的 textView 属性..(您可以在 xib 中看到这一点,带有箭头图标)。

2.Check whether the textView delegate is written in your .h file.

2.检查你的.h文件中是否写入了textView委托。

@interface YourViewController : UIViewController <UITextViewDelegate>

3.Check whether the textView's delegate is assigned.

3.检查textView的delegate是否被赋值。

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

回答by hsusmita

Check for new line character by this method.

通过这种方法检查换行符。

-(NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet

-(NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet

Then check the length of the text, if it is one character length, then new line character is entered, not pasted.

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

   - (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;
    }