Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sagarshivam

macrumors member
Original poster
May 24, 2011
51
0
Dear All!

I am trying to read text file from local system as well as using url (using NSURLConnection). And I am able to view the content of file on console.

I want them to be displayed on TextView using UITextView or by any other mean.

I created a button on clicking of which viewer with file content should be seen.

Code:
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
							     initWithTitle:@"View" 
							    style:UIBarButtonItemStylePlain 
							    target:self 
							    action:@selector(textviewtest)]
                                                            autorelease];

Further I am trying to use the UITextView as:

Code:
- (void) textviewtest {
    textview = [[UITextView alloc] init];	 
    NSString *data = @"this is textview";	
    [self.textview  setText: data];
    [self.textview setFont:[UIFont fontWithName:@"Helvetica" size:16.0]];
    [data release];		
	
}

in above "data" is just for testing purpose and later I will display the NSMutabledata object on this textview.

Kindly help me out where the mistake I am doing.
Or provide me some reference / tutorial where I can learn how to use UITextView.

Thanks and regards
Sagar Priyadarshi
 

sagarshivam

macrumors member
Original poster
May 24, 2011
51
0
Dear robbieduncan

In the same .m file I am already defining object of UIView as below:

Code:
- (void)loadView {
	UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	self.view = contentView;
	contentView.backgroundColor = [UIColor whiteColor];
	contentView.multipleTouchEnabled= TRUE;
       [contentView release];


Can I add UITextView object as subview to this contentview??
I tried doing as:

Code:
- (void) textviewtest {
    textview = [[UITextView alloc] init];	 
    NSString *data = @"this is textview";	
    [self.textview  setText: data];
    [self.textview setFont:[UIFont fontWithName:@"Helvetica" size:16.0]];
    [[self view] addSubview : textview ];
    [data release];	
	
}
There is no error on building but on clinking the "view button" nothing happens..even there is no error on console too.

Plz help me out.I am a newbie for app development.
 
Last edited by a moderator:

sagarshivam

macrumors member
Original poster
May 24, 2011
51
0
Sorry , i dint add the view as well as frame was required. So my modified code looks like

Code:
       - (void) textviewtest {

       CGRect frame1 = CGRectMake(10, 10, 200, 200); 
       textview = [[[UITextView alloc] initWithFrame:frame1 ] autorelease];
	textview.backgroundColor =[UIColor redColor];
	textview.clipsToBounds = YES;
	NSString *data = @"this is textview";
	[textview  setText: data];
        [textview setFont:[UIFont fontWithName:@"Helvetica" size:16.0]];
	[[self view] addSubview : textview ];
	[textview release];

}



I can see the red colored view being added on click of "view" button. It means that textview is added now. But the problem now is that the text ("this is textview") is not seen.

I tried
textview.text = @"this is textview";

Am I missing something? or what the changes should I make to let the string visible on textview. Plz guide

Thanks
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
Code:
- (void) textviewtest {
       CGRect frame1 = CGRectMake(10, 10, 200, 200); 
       textview = [[[UITextView alloc] initWithFrame:frame1 ] [COLOR=red]autorelease[/COLOR]];
	textview.backgroundColor =[UIColor redColor];
	textview.clipsToBounds = YES;
	NSString *data = @"this is textview";
	[textview  setText: data];
        [textview setFont:[UIFont fontWithName:@"Helvetica" size:16.0]];
	[[self view] addSubview : textview ];
	[COLOR=red][textview release];[/COLOR]
}


I don't know if it's related but you are over-releasing textview. Either send autorelease or release, don't send both.
 

sagarshivam

macrumors member
Original poster
May 24, 2011
51
0
Thanks a lot!!

Sending both autorelease and release was the problem of no display.
Its working fine now.

is there any diff between:

[textview setText: data];
and
textview.text=@"some data"

Both of them are working well in my case

Thanks
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
is there any diff between:

[textview setText: data];
and
textview.text=@"some data"

No. The dot (.) notation for accessing properties is syntactic sugar. The compiler replaces
Code:
textview.text=value;
with
Code:
[text setText:value]
(assuming you have not used a directive to rename the accessor).
 

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
Thanks a lot!!

Sending both autorelease and release was the problem of no display.
Its working fine now.

is there any diff between:

[textview setText: data];
and
textview.text=@"some data"

Both of them are working well in my case

Thanks

No. one is using the point to acces your object, other one is using the generated Setter. which does the same.
 

sagarshivam

macrumors member
Original poster
May 24, 2011
51
0
Plz look at below code (which already I have mentioned above too)

Code:
- (void) textviewtest {
	CGRect frame1 = CGRectMake(10, 10, 300, 400); 
        textview = [[[UITextView alloc] initWithFrame:frame1 ] autorelease];
	textview.backgroundColor =[UIColor yellowColor];
	textview.autocorrectionType =YES;
	textview.editable = YES;
	textview.clipsToBounds = YES;
	NSString *data = @"this is text view";
	[textview  setText: data ];
        [textview setFont:[UIFont fontWithName:@"Helvetica" size:16.0]];
	[[self view] addSubview : textview ];
}

In above I am initializing variable 'data' and textview is showing it well.
Even if I am replacing 'data' value with larger size of text , its being shown in textview.

Now I am trying to get the content of a file (txt) in a string and that string I am passing to 'setText' method above..but nothing happens, not even yellow coloured textview is seen.

I am saving the content of text file in a string as :

Code:
NSFileHandle *infile;
infile = [NSFileHandle fileHandleForReadingAtPath: @"/Users/sagar/testing.txt"];
if (infile == nil) {
NSLog (@"reading operation failed");		
}
 else {		
	fileString = [NSString stringWithContentsOfFile: @"/Users/sagar/testing.txt"];
		
	NSLog(@"%@", fileString);		
	NSLog (@"reading of the file was successful");
	}
	[infile closeFile];


Though the content of file is seen on console.

In textview I am writing as:

Code:
[textview  setText: fileString ];

I am not getting what is the issue about.
Plz help me. Thanks
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.