steipete / psmenuitem Goto Github PK
View Code? Open in Web Editor NEWA block based UIMenuItem subclass.
License: Other
A block based UIMenuItem subclass.
License: Other
no need to swizzle any func, very easy
struct Info {
var id = ""
var title: String?
var callback:(() -> Swift.Void)?
}
func menuItems(infos: [Info], targetClasses: [AnyClass]) -> [UIMenuItem] {
var items = [UIMenuItem]()
infos.forEach { (info) in
// dynamic add selector for custom selector
let aSelector = NSSelectorFromString("custom_prefix" + info.id)
let block = { () -> Swift.Void in
info.callback?()
}
let castedBlock: AnyObject = unsafeBitCast(block as @convention(block) () -> Swift.Void, to: AnyObject.self)
let imp = imp_implementationWithBlock(castedBlock)
targetClasses.forEach({ (targetClass) in
if class_addMethod(targetClass,
aSelector,
imp,
UnsafeMutablePointer(mutating: "v")) {
} else {
class_replaceMethod(targetClass,
aSelector,
imp,
UnsafeMutablePointer(mutating: "v"))
}
})
// create item
let item = UIMenuItem(title: info.title ?? "", action: aSelector)
items.append(item)
}
return items
}
i run your demo but have some mistake
I get the following warnings in iOS 8:
~/src/PSMenuItem/PSMenuItem.m:80:40: Undeclared selector 'pspdf_canPerformAction:withSender:'
~/src/PSMenuItem/PSMenuItem.m:85:43: Undeclared selector 'pspdf_canBecomeFirstResponder'
~/src/PSMenuItem/PSMenuItem.m:100:43: Undeclared selector 'pspdf_methodSignatureForSelector:'
~/src/PSMenuItem/PSMenuItem.m:111:45: Undeclared selector 'pspdf_forwardInvocation:'
Hello! The project no longer compiles by default no iOS8.
Here are some changes I did to make it compile:
((void(*)(id, SEL, NSInvocation*))objc_msgSend)(_self, forwardInvocationSEL, invocation);
return ((NSMethodSignature *(*)(id, SEL, SEL))objc_msgSend)(_self, methodSignatureSEL, selector);
After getting it to compile, it still doesn't seem to work. The UIMenuController would still ask the active UIViewController for canPerformAction:sender: and canBecomeFirstResponder:.
(I did remember to install the handler)
If you're using this library while also using PSPDFKit you'll get duplicate symbol errors regarding kMenuItemTrailer - renaming to kMenuItemTrailerPSMenuItem fixed the linking errors.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.