Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-03-2011, 12:34 PM   #1 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default How to add a line to a UITextView

Hi everybody!

I am currently programming on something like a peer-to-peer Chat-App. I have a textfield and a textview.

Code:
-(IBAction) sendData:(id)sender{
    
	NSString *str=mTextField.text;
	[mSession sendData:[str dataUsingEncoding: NSASCIIStringEncoding] toPeers:mPeers withDataMode:GKSendDataReliable error:nil];

}

- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{    
	NSString* aStr;
	aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
	NSLog(@"Received Data from %@",peer);
	mTextView.text=aStr;
	
}
When I type in something in, the text of the textview appears. When I repeat that the text CHANGES. But I want to add a line. Could somebody please tell me how to do that?

vincefried
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Old 04-03-2011, 01:23 PM   #2 (permalink)
You win again, gravity!
 
reficul's Avatar
 
Join Date: Feb 2011
Location: Alta, Norway
Age: 38
Posts: 166
reficul is on a distinguished road
Default

You can try to use stringByAppendingString.
Code:
mTextview.text = [mTextview.text stringByAppendingString:aStr];
Remember to release your alloced string.
__________________
If you really want something in this life you have to work for it. Now quiet, they're about to announce the lottery numbers.

Homer J Simpson
reficul is offline   Reply With Quote
Old 04-03-2011, 04:11 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by vincefried View Post
Hi everybody!

I am currently programming on something like a peer-to-peer Chat-App. I have a textfield and a textview.

Code:
-(IBAction) sendData:(id)sender{
    
	NSString *str=mTextField.text;
	[mSession sendData:[str dataUsingEncoding: NSASCIIStringEncoding] toPeers:mPeers withDataMode:GKSendDataReliable error:nil];

}

- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{    
	NSString* aStr;
	aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
	NSLog(@"Received Data from %@",peer);
	mTextView.text=aStr;
	
}
When I type in something in, the text of the textview appears. When I repeat that the text CHANGES. But I want to add a line. Could somebody please tell me how to do that?

vincefried

Several things:

In one part of your code you call it mTextField, and in another part you call t mTextView. Do you have two different text objects?

Are you using UITextView or UITextField objects? A UITextView is a multi-line, scrollable text object.

If you are creating UITextFields, though, I don't think they are set up to display multiple lines of text.

If you're using a UILabel (read-only) they default to a single line, but can be set up to display multiple lines. To do that set the numberOfLines property to 0.

Next, assuming you're using a UITextView, you will need to append new text to the end of the old. To get the new text to begin on a new line, you'll need to add a carriage return at the end of the old text. Something like this:


Code:
myTextView.text = [myTextView.text stringByAppendingFormat: @"\n%@", newText];
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 04-03-2011, 04:59 PM   #4 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default Sorry

... I didn't describe it right... first you connect over peer-to-peer to another device. Then, you have a textfield and a textview. in the textfield, you type in something, and then it displays the typed text in the textview of your peer. the textview is not editable. there's a "send"-button, which is connected to the sendData-action. everytime, I press the send button, the text in the textview of my peer will be replaced by the new text, I typed in, but it should be added in a new line, just like a messenger. I hope that helped
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Old 04-03-2011, 06:52 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by vincefried View Post
... I didn't describe it right... first you connect over peer-to-peer to another device. Then, you have a textfield and a textview. in the textfield, you type in something, and then it displays the typed text in the textview of your peer. the textview is not editable. there's a "send"-button, which is connected to the sendData-action. everytime, I press the send button, the text in the textview of my peer will be replaced by the new text, I typed in, but it should be added in a new line, just like a messenger. I hope that helped
Ok, so take the contents of your text field and/or the response from the server and append it to the contents of the text view as I outlined in my post.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 04-04-2011, 09:47 AM   #6 (permalink)
Registered Member
 
vincefried's Avatar
 
Join Date: Apr 2010
Location: Hamburg, Germany
Posts: 38
vincefried is on a distinguished road
Default Thanks!

Yeah! It works perfectly! Thanks a lot
__________________
My Apps:
Adalbert sagt.. (Germany only)
vincefried is offline   Reply With Quote
Reply

Bookmarks

Tags
add, chat, peer-to-peer, uitextfield, uitextview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 369
12 members and 357 guests
condor304, dansparrow, dre, ilmman, LezB44, michelle, Objective Zero, samdanielblr, Sami Gh, shagor012, thephotographer, tinamm64
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:40 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0