0

I need to add an undefined number of NSButton to an NSView in code. The problem is that I can' t use constraintsWithVisualFormat: because i don' t know the name of the NSButton and also the number of button that I have. Anyone have a solution? Thanks!

1
  • I think that I can create dinamically the string used in the constraintsWithVisualFormat: method but can I do that if I have an array of NSButton?
    – Luca
    Commented Jun 2, 2013 at 15:28

1 Answer 1

0

When using constraintsWithVisualFormat:, you need to know the names of the variables that point to your NSButtons only if you use NSDictionaryOfVariableBindings to create the dictionary of views. You could just as easily build your own dictionary using whatever keys you like.

If your buttons are stored in an array, you can iterate through them and create constraints between each of them:

for ( int i = 1 ; i < buttonArray.count ; i++ ) {
    NSDictionary* views = @{ @"buttonOne":buttonArray[i-1] , @"buttonTwo":buttonArray[i] } ;
    NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"[buttonOne]-[buttonTwo]"  options:0  metrics:nil  views:views] ;
    // Use the constraints.
}
1
  • Yes, I found the solution before your reply but this is exactly what I need. Thanks again!
    – Luca
    Commented Jun 3, 2013 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.