How to show the share button in Mountain Lion?

南楼画角 提交于 2019-12-20 10:09:06

问题


Mountain Lion offers a built-in sharing button that reveals a menu of sharing services appropriate for the app:

How can I insert it in my app?


回答1:


To add the share button on Mountain Lion:

1) Add a NSButton called, for example, shareButton.

2) Add the standard image for this button:

[shareButton setImage:[NSImage imageNamed:NSImageNameShareTemplate]];
[shareButton sendActionOn:NSLeftMouseDownMask];

3) Into the "on click action", present the NSSharingServicePicker:

NSSharingServicePicker *sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:urls];
sharingServicePicker.delegate = self;

[sharingServicePicker showRelativeToRect:[sender bounds]
                                          ofView:sender
                                   preferredEdge:NSMinYEdge];

4) Eventually, implement the NSSharingServicePickerDelegate methods to customize the picker’s available services.




回答2:


In Swift, I've used this:

extension NSSharingService {
    class func shareContent ( content: [AnyObject], button: NSButton ) {
        let sharingServicePicker = NSSharingServicePicker (items: content )

        sharingServicePicker.showRelativeToRect(button.bounds, ofView: button, preferredEdge: NSRectEdge.MaxY)
    }
}



回答3:


Note that if you're trying to add this button via Interface Builder:

  1. Select the button
  2. Switch to Attributes inspector
  3. Delete the button Title
  4. Insert: NSShareTemplate as the Image name.

It doesn't look right to me in XCode, but works fine when run.

PS - This appears to be a case where you need to use the System Icon string value (NSShareTemplate) instead of the constant (NSImageNameShareTemplate).



来源:https://stackoverflow.com/questions/11815077/how-to-show-the-share-button-in-mountain-lion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!