PDA

View Full Version : UITextView problem




sagarshivam
Jul 11, 2011, 03:47 AM
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.


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



Further I am trying to use the UITextView as:


- (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



robbieduncan
Jul 11, 2011, 05:20 AM
You have not added the text view to any visible view or window

sagarshivam
Jul 11, 2011, 06:39 AM
Dear robbieduncan

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


- (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:


- (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.

robbieduncan
Jul 11, 2011, 06:51 AM
What is the frame of textview?

sagarshivam
Jul 12, 2011, 06:20 AM
Sorry , i dint add the view as well as frame was required. So my modified code looks like


- (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
Jul 12, 2011, 06:31 AM
- (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 don't know if it's related but you are over-releasing textview. Either send autorelease or release, don't send both.

sagarshivam
Jul 12, 2011, 07:25 AM
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
Jul 12, 2011, 07:28 AM
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
textview.text=value;
with
[text setText:value] (assuming you have not used a directive to rename the accessor).

jnoxx
Jul 12, 2011, 07:29 AM
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
Jul 12, 2011, 11:54 PM
Plz look at below code (which already I have mentioned above too)


- (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 :


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:


[textview setText: fileString ];


I am not getting what is the issue about.
Plz help me. Thanks