Skip to content

Instantly share code, notes, and snippets.

@jinqian
Last active December 11, 2015 20:48
Show Gist options
  • Save jinqian/4657702 to your computer and use it in GitHub Desktop.
Save jinqian/4657702 to your computer and use it in GitHub Desktop.
# Example of delegation in RubyMotion
class MsActivityView < UIView
attr_accessor :delegate
def initWithFrame(frame)
super
#...
@cancelButton = UIButton.alloc.init
@cancelButton.addTarget(
self,
action: :cancel,
forControlEvents:UIControlEventTouchUpInside
)
end
def cancel
self.delegate.public_send(:activityViewDidCancel, self)
end
end
class MsScannerViewController < UIViewController
# ...
def setActivityView(show)
activityIndicator = MsActivityView.alloc.initWithFrame(frame)
activityIndicator.text = "Searching"
activityIndicator.isAnimating = true
# Define delegation
activityIndicator.delegate = self
# Place activity view at the navigation controller level to make sure it ignores tap gestures
self.navigationController.view.addSubview(activityIndicator)
end
def activityViewDidCancel(view)
self.scannerSession.cancel
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment