UITextView member variable is always nil in my UIViewController

冷暖自知 提交于 2019-12-31 05:42:12

问题


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

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/6554858/uitextview-member-variable-is-always-nil-in-my-uiviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!