Last active
December 17, 2015 05:19
-
-
Save bensheldon/5556762 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyController < UIViewController | |
def viewDidLoad | |
@button = UIButton.alloc.init | |
@button.when_tapped do | |
weak_self = WeakRef.new(self) # <--- create a weak reference to self | |
@alert = UIAlertView.alertViewWithTitle "Alert", | |
message: "Message", | |
cancelButtonTitle: "Dismiss", | |
otherButtonTitles: nil, | |
onDismiss: lambda { |index| | |
weak_self and weakself.do_something # <--- soak the weak reference | |
}, | |
onCancel: lambda { | |
weak_self and weakself.do_something_else | |
} | |
end | |
end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyController < UIViewController | |
def viewDidLoad | |
@button = UIButton.alloc.init | |
@button.when_tapped do | |
@action_sheet = UIActionSheet.alloc.initWithTitle("Title", | |
delegate: WeakRef.new(self), # <--- use weak reference here, because delegate is ViewController | |
cancelButtonTitle: "Cancel", | |
otherButtonTitles: "Do Something", | |
nil | |
) | |
end | |
end | |
# This is the UIActionSheet's delegate method defined on the UIViewController | |
def actionSheet(action_sheet, clickedButtonAtIndex: index) | |
case index | |
when 0 | |
# do something | |
else | |
#cancel | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment