UITextView problem

Discussion in 'iOS Programming' started by sagarshivam, Jul 11, 2011.

  1. macrumors member

    Joined:
    May 24, 2011
    #1
    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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You have not added the text view to any visible view or window
     
  3. sagarshivam, Jul 11, 2011
    Last edited by a moderator: Jul 11, 2011

    thread starter macrumors member

    Joined:
    May 24, 2011
    #3
    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.
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  5. thread starter macrumors member

    Joined:
    May 24, 2011
    #5
    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
     
  6. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6

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

    Joined:
    May 24, 2011
    #7
    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
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    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).
     
  9. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #9
    No. one is using the point to acces your object, other one is using the generated Setter. which does the same.
     
  10. thread starter macrumors member

    Joined:
    May 24, 2011
    #10
    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
     

Share This Page