IBOutlet instances are (null) after loading from NIB

前端 未结 5 1878
太阳男子
太阳男子 2021-02-18 22:30

I am working on an iPhone app and am getting (null) references to IBOutlet fields in my controller. I have a UIViewController subclass that is set as the File\'s Owner in my XIB

相关标签:
5条回答
  • 2021-02-18 22:42

    I guess asking the question after looking at the problem for over an hour led me to figure it out:

    I just changed my code to check the text box AFTER displaying the view... now everything is instantiated.

    Imagine that: the UI elements aren't instantiated until you display them!

    0 讨论(0)
  • 2021-02-18 22:43

    This is the solution.

    The IBOutlets aren't ready to use until the view controller finishes loading.

    For example, if you want to set a UILabels text property, you would need to set a NSString on your controller first, and then set the labels text property in somewhere like viewDidLoad.

    So in your firstViewController.m : (This is for storyboards, but same idea applies)

    - (void)buttonPress:(id)sender {
       [self performSegueWithIdentifier:@"mySegue" sender:self];
    }
    
    - (void)prepareForSegue(UIStoryboardSegue *)segue sender:(id)sender {
       SecondViewController *secondViewController = [segue destinationViewController];
       secondViewController.stringForLabel = @"My Label String";
    }
    

    Then in the .h of your secondViewController:

    @property (strong, nonatomic) IBOutlet UILabel *label;
    @property (strong, nonatomic) NSString *stringForLabel;
    

    Then we set the text property on the UILabel, in the viewDidLoad of secondViewController.m. By this stage, the IBOutlet has been created and is ready to go.

    - (void)viewDidLoad {
       [super viewDidLoad];
       self.label.text = self.stringForLabel;
    }
    
    0 讨论(0)
  • 2021-02-18 22:43

    Make sure the view property of your view controller (ie File's Owner in this case) is wired to the view in your xib. As your textField is almost certainly a subview of that, it's important to get that wired in too (and I don't think the nib will load properly without that being set).

    0 讨论(0)
  • 2021-02-18 22:55

    This is how I do it:

    //in your view controller
    -(void)setUp
    {
        [self.cardView layoutIfNeeded];
        LearnCardPlainText *cardNib = [[[UINib nibWithNibName:@"LearnCardPlainText" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0];
        cardNib.frame = self.cardView.frame;
        [cardNib setUpWithPart:learnModel];
        [self.view addSubview:cardNib];
    }
    
    0 讨论(0)
  • 2021-02-18 22:56

    Another potential cause of a null IBOutlet pointer is forgetting to save the xib file in Interface Builder after creating the outlet connection.

    0 讨论(0)
提交回复
热议问题