辞职第一响应者 UITextView

发布于 2024-11-25 06:42:24 字数 2573 浏览 1 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花辞树 2024-12-02 06:42:24

您可以尝试这样做:

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

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

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.

草莓酥 2024-12-02 06:42:24

看了你的源代码后,我发布的链接毫无意义。我以为你在谈论键盘上的完成键。但现在我看到你正在谈论导航栏上的一个实例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];
}

几乎相同,除了这个创建了一个按钮具有适当的风格。

无论如何,我不知道如何添加按钮,因为在 viewDidLoad: 中没有调用 addButton:isLast: 。在 viewDidLoad: 中添加以下行。

[self addButton:self isLast:NO];

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

您即将完成,但如果按下该按钮,您的应用程序将会崩溃,因为 finishQuestionnaire: 未在 self (CommentQuestionViewController) 上实现。由于您希望在用户按下“完成”按钮时关闭键盘,因此我们还将放弃您作为第一响应者的文本视图。只需添加以下方法:

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

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:

- (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:.

[self addButton:self isLast:NO];

last is redundant in your addButton: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 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:

- (void)finishQuestionnaire:(id)sender
{
    [inputAnswer resignFirstResponder];
}
楠木可依 2024-12-02 06:42:24

就是 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.检查你的.h文件中是否写入了textView委托。

@interface YourViewController : UIViewController <UITextViewDelegate>

3.检查textView的delegate是否被分配。

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

It's BOOL that's it..

- (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).

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

@interface YourViewController : UIViewController <UITextViewDelegate>

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

//set this in the viewDidLoad method
self.textView.delegate = self;
挽袖吟 2024-12-02 06:42:24

通过此方法检查换行符。

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

Check for new line character by this method.

-(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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文