2

我正在使用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];
}
4

4 回答 4

2

你可以试试这个:

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

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

于 2011-07-19T21:38:30.243 回答
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];

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

您几乎完成了,但是如果您按下该按钮,您的应用程序将崩溃,因为finishQuestionnaire:未在self( CommentQuestionViewController) 上实现。而且由于您想在用户按下完成按钮时关闭键盘,因此我们还将在此处将您的文本视图作为第一响应者。只需添加以下方法:

- (void)finishQuestionnaire:(id)sender
{
    [inputAnswer resignFirstResponder];
}
于 2011-07-20T02:08:22.387 回答
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.检查textView委托是否写在你的.h文件中。

@interface YourViewController : UIViewController <UITextViewDelegate>

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

//set this in the viewDidLoad method
self.textView.delegate = self;
于 2014-06-30T09:36:48.857 回答
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;
    }
于 2015-06-03T12:32:00.187 回答