Frage

I'd like to set the identifier property on a few of my constraints from within interface builder. I've used the User Defined Runtime Attributes table in IB before with NSView instances, and have had no problems, but I'm getting nowhere doing exactly the same thing with constraint classes. When I try to access the identifier in code it just returns null, rather than the string I entered into the Value column of the table. I guess this may be something to do with the xib loading mechanism, but I'm hoping someone will be able to say with certainty what the issue is.

IB --> Identity Inspector --> User Defined Runtime Attributes:

  1. Key Path: identifier
  2. Type: String
  3. Value: TextViewWidthConstraint

later in an IBAction method:

NSLog(@"%@", self.widthCon.identifier);

// --> (null)
War es hilfreich?

Lösung

There seems to be something basically wrong with the NSLayoutConstraint implementation. Creating a Category on NSLayoutConstraint and overwriting some methods for debugging purposes revealed the following:

The runtime attributes must be stripped by Xcode during compile time, because of the following reasons:

  1. The overwritten KVC methods inside the NSLayoutCategory setValue:forKey:, setValue:forKeyPath: and setValue:undefinedKey: are never getting called.

  2. The overwritten method setIdentifier: gets called, but never receives the passed value from the runtime attributes section

  3. Providing invalid keys does not create errors like on any other object, for example a NSView:

    2014-03-19 08:25:52.806 LayoutTestApp[13733:303] [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key testKey.

Additionally, setting a custom class on NSLayoutConstraint gets also gracefully ignored by Xcode (the custom classes' init methods are never getting called). It looks like Apple disabled the runtime attributes and custom class feature for NSLayoutConstraints or it's just a bug. Either way: setting the identifier via runtime attributes will most likely not work atm.

Andere Tipps

Your problem could be, that the NSLayoutConstraint class has no property identifier, try this:

NSLog(@"%@", [self.widthCon valueForKey:@"identifier"]);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top