2

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];
}
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
user559142
  • 12,279
  • 49
  • 116
  • 179

4 Answers4

2

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.

buildsucceeded
  • 4,203
  • 4
  • 34
  • 72
med200
  • 222
  • 1
  • 4
1

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;
Gokul
  • 1,236
  • 1
  • 14
  • 26
1

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];
}
Espresso
  • 4,722
  • 1
  • 24
  • 33
0

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;
    }
hsusmita
  • 282
  • 3
  • 16