GithubHelp home page GithubHelp logo

psmenuitem's Introduction

PSMenuItem

A block based UIMenuItem subclass.

The inflexible @selector based approach in UIMenuItem was driving me crazy. I searched quite a while for a block-based UIMenuItem, but couldn't find one. So finally, I sat down and wrote my own implementation for my iOS PDF framework PSPDFKit.

If you are as annoyed about the missing target/action pattern, as I am, you will love this. Also read the in-depth article on my website.

How to use

    // one time-call needed to prepare the block based PSMenuItem handler.
    [PSMenuItem installMenuHandlerForObject:button];

    PSMenuItem *actionItem = [[PSMenuItem alloc] initWithTitle:@"Action 1" block:^{
        [[[UIAlertView alloc] initWithTitle:@"Message" message:@"From a block!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    }];

    PSMenuItem *submenuItem = [[PSMenuItem alloc] initWithTitle:@"Submenu..." block:^{
        [UIMenuController sharedMenuController].menuItems = @[
        [[PSMenuItem alloc] initWithTitle:@"Back..." block:^{
            [self buttonPressed:button];
        }],
        [[PSMenuItem alloc] initWithTitle:@"Sub 1" block:^{
            NSLog(@"Sub 1 pressed");
        }]];
        [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
    }];

    [UIMenuController sharedMenuController].menuItems = @[actionItem, submenuItem];
    [[UIMenuController sharedMenuController] setTargetRect:button.bounds inView:button];
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];

PSMenuItem uses ARC and is tested with Xcode 4.4 and 4.5 (iOS 4.3+)

The code looks a bit scary and involves swizzling certain methods, but it's actually not that bad. No private API is used, and it's highly unlikely that Apple ever changes something as basic as the UIResponder chain.

Creator

Peter Steinberger @steipete

I'd love a thank you tweet if you find this useful.

License

PSMenuItem is available under the MIT license. See the LICENSE file for more info.

psmenuitem's People

Contributors

steipete avatar

Watchers

Matthias Tretter avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.