Skip to content

Instantly share code, notes, and snippets.

@bensheldon
Last active December 17, 2015 05:19
Show Gist options
  • Save bensheldon/5556762 to your computer and use it in GitHub Desktop.
Save bensheldon/5556762 to your computer and use it in GitHub Desktop.
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
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