Miriam Roberts
Miriam Roberts

Reputation:

Trying to copy a string into a textfield in objective-C

I am having trouble copying the value of an NSString into a Textfield value. I've done this before, but for some reason it's not working in the function below. Can someone tell me if I'm doing something wrong?

pictureComment is the Textfield and comment is an NSString. When I look at the log file, only 'comment' prints. Any suggestions?

-(void) userPhotoComment:(PhotoComment*)userPhotoComment
       didAddComment:(NSString*)comment
{
NSLog(@"PhotoPicker-DidAddComment: Below is the comment");
NSLog(comment);     // this prints the comment correctly
pictureComment.text = [comment copy];

NSLog(@"PhotoPicker-DidAddComment: Below is the picture comment");
NSLog(pictureComment.text);     // doesn't print

[self dismissModalViewControllerAnimated:YES];
    /// ... more stuff is done below
}

Upvotes: 0

Views: 741

Answers (2)

iKenndac
iKenndac

Reputation: 18776

There's no need to copy the comment into the pictureComment field - in fact, your code as it stands has a memory leak.

The NSLogs might not be working because you're using NSLog incorrectly - you should never directly NSLog a variable. Try this version of the method (making sure pictureComment isn't nil):

-(void) userPhotoComment:(PhotoComment*)userPhotoComment
           didAddComment:(NSString*)comment
{
NSLog(@"PhotoPicker-DidAddComment: Below is the comment");
NSLog(@"%@", comment);     // this prints the comment correctly
pictureComment.text = comment;

NSLog(@"PhotoPicker-DidAddComment: Below is the picture comment");
NSLog(@"%@", pictureComment.text);     // doesn't print

[self dismissModalViewControllerAnimated:YES];
/// ... more stuff is done below

}

Upvotes: 1

Justin Gallagher
Justin Gallagher

Reputation: 3232

Perhaps this is too simple, but have you made sure that pictureComment is not nil? Should it be userPhotoComment instead?

Upvotes: 1

Related Questions