Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Four Little Problems

Four Little Problems

Andy Lee talks about four Cocoa programming obstacles he encountered, and how resolved them.

CocoaHeadsNYC

March 14, 2013
Tweet

More Decks by CocoaHeadsNYC

Other Decks in Programming

Transcript

  1. Four Little Problems
    Andy Lee
    for CocoaHeadsNYC, 2013-03-14

    View full-size slide

  2. Better solutions?

    View full-size slide

  3. Problem
    Make 3 NSMatrix controls behave as one.

    View full-size slide

  4. Problem
    Make 3 NSMatrix controls behave as one.

    View full-size slide

  5. Solution
    Make 3 NSMatrix controls behave as one.
    AKMultiRadioView
    delegate
    target
    Custom superview,
    plus delegation.

    View full-size slide

  6. Solution
    Make 3 NSMatrix controls behave as one.
    AKMultiRadioView
    delegate
    doRadioAction:
    multiRadioViewDidMakeSelection:
    Your action code
    target

    View full-size slide

  7. API
    Make 3 NSMatrix controls behave as one.
    @interface AKMultiRadioView : NSView
    @protocol AKMultiRadioViewDelegate

    View full-size slide

  8. API
    Make 3 NSMatrix controls behave as one.
    @interface AKMultiRadioView : NSView
    - id delegate;
    - (NSInteger)selectedTag;
    - (BOOL)selectCellWithTag:(NSInteger)tag;
    - (IBAction)doRadioAction:(id)sender;
    @protocol AKMultiRadioViewDelegate
    - multiRadioViewDidMakeSelection:
    (AKMultiRadioView *)mrv

    View full-size slide

  9. How to use
    Make 3 NSMatrix controls behave as one.
    • Set the superview class in IB.
    • Drop in the NSMatrix instances.
    • Use unique cell tags.
    • Connect targets and delegate.
    • Put action code in the delegate method.

    View full-size slide

  10. Problem
    Set text color for disabled buttons.
    Apple’s colors
    The colors I want
    (Those triangles are actually characters.)
    (Yeah, I know the difference is subtle.)

    View full-size slide

  11. Solution
    Set text color for disabled buttons.
    @interface AKButtonCell : NSButtonCell
    // AppKit passes in an all-gray string.
    // Override to change the string color.
    // Then call super.
    - (NSRect)drawTitle:(NSAttributedString *)title
    withFrame:(NSRect)frame
    inView:(NSView *)controlView;

    View full-size slide

  12. How to use
    Set text color for disabled buttons.
    • Select your button’s cell in IB.
    • Set its class to AKButtonCell.
    • Set custom colors if you want.

    View full-size slide

  13. Credit
    Set text color for disabled buttons.
    http://stackoverflow.com/questions/
    6370500/nsbutton-set-text-color-in-
    disabled-mode/10632311#10632311

    View full-size slide

  14. Problem
    Focus rings where and when I want them.
    Standard focus ring bleeds.
    WebViews have no focus ring.
    I don’t like standard look.

    View full-size slide

  15. Solution
    Focus rings where and when I want them.
    AKFocusView
    Wrap views in custom superview.
    AKFocusView
    table view web view

    View full-size slide

  16. How to use
    Focus rings where and when I want them.
    • Embed any view in an AKFocusView.
    • No code required.
    • Redraws automatically as needed.
    • Draws focus ring as solid border.
    • Suppresses normal AppKit focus ring.

    View full-size slide

  17. Problem
    Control over key view loop.

    View full-size slide

  18. Problem
    Control over key view loop.
    • NSBrowser not included by default.
    • Views in drawers not included.
    • Sometimes trapped in toolbar.

    View full-size slide

  19. Solution
    Control over key view loop.
    @interface AKWindow : NSWindow
    - (void)setTabChain:(NSArray *)views;
    - (BOOL)handlePossibleTabChainEvent:(NSEvent *)e;
    //In NSApplication subclass, override:
    - (void)sendEvent:(NSEvent *)anEvent;

    View full-size slide

  20. Solution
    Control over key view loop.
    • I currently hack toolbar buttons in.
    • Should go into a separate class.
    • Note that [self window] may be a drawer.
    • Note Full Keyboard Access.

    View full-size slide

  21. All code is on github:
    aglee/appkido

    View full-size slide

  22. That’s all, thanks!

    View full-size slide