UIApplicationのsendActionを見直そう
ݹ͖ྑ͖ sendActionॴ ༑ଠ @tokoromiOS App Programerpotatotips-26
View Slide
ॴ༑ଠ@tokorom (ϑϦʔλʔʣpotatotipsӡӦ૭ޱ / potatotipsΛओ࠵͍͚ͨͩΔձࣾ͞Μืूதʂ
UIApplication - sendAction
UIApplication - sendAction•࣮ࡍʹ͍ͬͯΔਓͲͷ͘Β͍ʁ•ͦͦଘࡏΛ͍ͬͯΔʁ•ʢUIControlͷ΄͏ͨ·ʹݟΔ͚Ͳ…ʣ•ʢ͍ͬͯΔͷΛ͋Μ·Γݟͨ͜ͱͳ͍ʣ
UIApplication - sendActionNEW!iOS 9Ͱྲྀͷ͝ͱ͘ݱΕͨ࠷ڧͷfunction !?
UIApplication - sendActioniPhone OS 2͔Β͘͝ී௨ʹଘࡏ͢Δ͋·ΓΘΕͳ͍ͭ
// hoge: ΛൃՐlet app = UIApplication.sharedApplication()app.sendAction("hoge:", to: xxx, from: sender, forEvent: nil)// hoge:func hoge(sender: AnyObject) {print("hoge!!")}αϯϓϧίʔυUIApplication - sendAction
UIApplication - sendAction•ग़ͨΑɺจࣈྻࢦఆʂ•Objective-C෩selectorࢦఆ͕ݹष͍•͍ॴ͋Δͷʁ•RxSwiftͱ͔͏΄͏͕Α͘ͳ͍ʁapp.sendAction("hoge:", to: xxx, from: sender, forEvent: nil)͓͓ΉͶਖ਼ղ
Responder Chain (in UIKit)
Responder Chain (in UIKit)•Ԟ·ͬͨ֊ͰൃՐ•֊ͷͲ͜Ͱड͚ΒΕΔ•DelegateͷΑ͏ʹreceiverΛड͚͢ඞཁ͕ͳ͍•ଞखஈͰಉ͡ߏΛ࣮ݱ͢Δͷ͍͠
Responder Chain (in UIKit)app.sendAction("hoge:", to: xxx, from: sender, forEvent: nil)app.sendAction("hoge:", to: , from: sender, forEvent: nil)app.sendAction("hoge:", to: nil, from: sender, forEvent: nil)
Responder Chain (in UIKit)UITabBarControllerUIViewControllerUITabBarControllerUIViewControllerUIViewControllerUIViewController
Responder Chain (in UIKit)UITabBarControllerUIViewControllerUITabBarControllerUIViewControllerUIViewControllerUIViewControllerUITableViewUITableViewCellUIViewUIButton
sendActionͷྑ͍ͱ͜Ζ•Responder Chainͳૄ݁߹ͳΈͰը໘ߏ͕มΘͬͯར༻Ͱ͖Δ•CocoaͷࠜװͱͳΔΈͷͨΊྲྀߦʹࠨӈ͞Εͳ͍
sendActionͷμϝͳͱ͜Ζ•ΠϯλʔϑΣʔε͕ݹ͍•จࣈྻࢦఆ•selector??•ҾʹಛఆͷObjectΛͮ͠Β͍
৽͍͠ΠϯλʔϑΣʔεͰϥοϐϯά// hoge: ΛൃՐHogeEvent(hoge: hoge).fireFrom(self.view)// hoge:func hoge(sender: AnyObject) {guard let hoge: Hoge =ɹContext.valueFor(sender) else {return}print("hoge: \(hoge)")}
·ͱΊ•UIApplicationͷsendAction߹ʹΑͬͯ༗༻•ݹ͖ྑ͖ػೳ͕ͩը໘ߏ͕ෳࡶͳݱͰͦ͜ར༻Ձ͕͋Δ•ੲ͔Β͋Δػೳʹҙ֎ͱ͑Δͷ͕͍ͬͯΔ͔ʁ
More InformationYuta ToKoRoiOS App ProgramerTwitter @tokoromhttp://www.tokoro.me/