0

I am about to set up some constraints in my app. When i try to make an NSDictionary i get the error that tells me that my UITextView is nil. As you can see i have set some preferences for my UITextView, so i would presume that it isn't nil. But, if it is nil, how to i make it not nil?

I have also tried the NSDictionaryOfVariableBindings, without any luck.

Thanks.

    UIView *infoView = [[UIView alloc] viewWithTag:1];
    infoView.backgroundColor = [UIColor blackColor];
    infoView.translatesAutoresizingMaskIntoConstraints = NO;
    [displayView addSubview:infoView];

    NSDictionary *views = @{@"infoView" : infoView};

    [displayView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[infoView]|"
                                                                    options:0
                                                                    metrics:nil
                                                                    views:views]];
    [displayView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[infoView]|"
                                                                    options:0
                                                                    metrics:nil
                                                                    views:views]];
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Henrik LP
  • 159
  • 9

1 Answers1

1

The mistake you're making is that viewWithTag: doesn't instantiate a new UIView, it just searches an existing UIView's heirarchy for another UIView that has that as its tag value. More at the documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instm/UIView/viewWithTag:

What you need to do is instatiate your UIView first, then set its tag:

UIView *infoView = [[UIView alloc] init];
infoView.tag = 1;

Also as a side point, as surprising as it might be, setting properties (and calling methods, or sending messages) on a nil is perfectly valid in objective-c, and you can't trust it as a test that the object isn't nil. More discussion here: Sending a message to nil?

J2K
  • 1,593
  • 13
  • 26