views:

197

answers:

3

Im trying to Insert the current date/time whenever the users hits the return key or starts typing on a UITextView in Xcode but not quite sure where to start.

I know this method is for when clicking into the TextView but doesn't seem to work:

- (void)textViewDidBeginEditing:(UITextView *)textView {
    self.txtDetail.text = [self.txtDetail.text stringByAppendingString:@"Hello!"];
}

Thanks

Update:

DetailViewController.h 
@interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate> { 
    UITextView *txtDetail;
 } 
@property (nonatomic, retain) IBOutlet UITextView *txtDetail;

 - (void)textViewDidBeginEditing:(UITextView *)textView;

DetailViewController.m 
@synthesize txtDetail;

 - (void)textViewDidBeginEditing:(UITextView *)textView { 
    NSLog(@"Hello");
    self.txtDetail.text = [self.txtDetail.text stringByAppendingString:@"Hello!"];
}

Update02:

Ive added this to my .m files:

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

    BOOL shouldChangeText = YES;  

    if ([text isEqualToString:@"\n"]) {  
        // Find the next entry field  

        txtDetail.text = [txtDetail.text stringByAppendingString:@"\nhey\n"];       }  
        textView.editable = YES;

        shouldChangeText = NO; 
    [textView becomeFirstResponder];

    return shouldChangeText;  
}

I get the desired effect (Hey is added in whenever i press return on the keyboard) but i now can't type anything... any ideas?

A: 

You have to declare you controller as implementing the UITextViewDelegate Protocol and then connect it as the text view's delegate. Otherwise it never receives the delegate messages at all.

Then you have to implement:

– textView:shouldChangeTextInRange:replacementText:

... to return YES/TRUE otherwise no text gets added.

Update02:

You need to implement all the UITextViewDelegate protocol methods that have a return. If you don't, the text view assumes a NO by default.

See: Text and Web Programming Guide: Managing TextFields and TextViews

You also need to understand the Delegate Pattern.

TechZen
Thanks, not quite sure how to implement it... I assume where I have UIPopoverControllerDelegate, UISplitViewControllerDelegate I add UITextViewDelegate but then how do I hook it up to the method.Appreciate the help :)
GregD
If your using nibs, you make a connection between the text view's delegate property and the controller which will be the file owner for the nib. You do have to declare the UITextViewDelegate and save the header before it will show up in Interface Builder. If you set up the view programmatically, you just use `self.textView.delegate=self;`
TechZen
This is quite confusing! I set a delegate linked it on the xib. But I really don't understand what im supposed to be doing now... the method still doesn't appear to be linked to anything, is there a really basic example of this functionality somewhere?
GregD
The methods aren't linked to anything. Instead you link the textView's delegate outlet to the object that implements the delegate protocol. In this case, it is your controller. You have to implement the protocol's methods in the delegate object. Then when the textView needs to query its delegate, it send the appropriate message i.e. the method's selector, to the controller which then calls the method.
TechZen
Don't get it im afraid, is there an example somewhere ? Can't seem to find any sample code for this type of action which is strange as it should be a simple common piece of code.
GregD
Got it working I had to place it in RootViewController.m and DetailViewController.m :)Now wondering how I can insert the date instead of "Hello"
GregD
See Test and Web Programming Guide: http://developer.apple.com/iphone/library/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/Introduction/Introduction.html
TechZen
A: 

Replace your method with the below method

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

   BOOL shouldChangeText = YES;   

   if ([text isEqualToString:@"\n"] && textView == detailText) 
   {   
     // Find the next entry field   

       txtDetail.text = [txtDetail.text stringByAppendingString:@"\nhey\n"];          
       textView.editable = YES; 

       shouldChangeText = NO;  
       [textView becomeFirstResponder]; 
   } 
   return shouldChangeText;   
} 
RVN
A: 

The mechanics of delegate calls is a very important part of iOS programming. There is a doc on it in the Apple libraries, and it is also discussed in many good books. The Apress book "Beginning iPhone Programming" is very good and covers delegate methods.

fogelbaby