开发者

UITextView member variable is always nil in my UIViewController

开发者 https://www.devze.com 2023-03-16 18:38 出处:网络
In my nib, I have a UITextView component. In my UIViewController I have a UITextView member field and I have used IB to make sure that the two are connected (at least I think I did that by dragging f

In my nib, I have a UITextView component.

In my UIViewController I have a UITextView member field and I have used IB to make sure that the two are connected (at least I think I did that by dragging from "Files Owner" and dropping it on the UITextView in IB and selecting my member variable).

Now, when I update the text via setText:, the UITextView still remains blank.

When I break into my code, the member variable (called textView) is nil.

So I am guessing I have not used IB correctly. How can I make sure that this variable is connected to the UITextView graphic element?

Here is the code

// My interface looks like

#import <UIKit/UIKit.h>

@interface DetailsController : UIViewController 
{
    UITextView *textView;
}

@property (nonatomic, retain) IBOutlet UITextView *textView; 

@end;


// and the implementation

#import "DetailsController.h"

@implementation DetailsController

@synthesize textView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any开发者_运维问答 cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

// used pressed the "Done" button
- (IBAction)done
{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)setDetails: (NSString *)detail withQuote:(NSString *)quote
{
    NSLog(@"%@", textView);
    [textView setText:detail];
}

@end


save nib file after connecting outlet, build project and run, it should work if it's connected


I can't see agere you are calling setText, but I think that you try to do it in the initializer. GUI code should not be done before the viewDidLoad in the case of using XIB's or loadView if you make your GUI programatically, or the other viewWill/viewDid... methods.

The reason is because the views are not loaded before they are actually needed, it is called lazy loading. You should Google that for more info.

0

精彩评论

暂无评论...
验证码 换一张
取 消