Created
July 27, 2013 08:47
-
-
Save naoya/6094280 to your computer and use it in GitHub Desktop.
This file contains 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
# -*- coding: utf-8 -*- | |
class AppDelegate | |
def application(application, didFinishLaunchingWithOptions:launchOptions) | |
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) | |
controller = MainViewController.alloc.init | |
@window.rootViewController = UINavigationController.alloc.initWithRootViewController(controller) | |
@window.makeKeyAndVisible | |
true | |
end | |
end | |
class MainViewController < UIViewController | |
def viewDidLoad | |
super | |
button = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemBookmarks, target:self, action:'open_modal') | |
self.view.backgroundColor = UIColor.whiteColor | |
self.navigationItem.rightBarButtonItem = button | |
end | |
def open_modal | |
modal = ModalViewController.alloc.init | |
nav = UINavigationController.alloc.initWithRootViewController(modal) | |
self.presentModalViewController(nav, animated:true) | |
end | |
end | |
class ModalViewController < UITableViewController | |
def viewDidLoad | |
super | |
@obj = MyObject.new | |
## パターン1: 普通の呼び方 => dealloc は呼ばれる | |
# button = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemCancel, target:self, action:'dismiss_modal') | |
# self.navigationItem.leftBarButtonItem = button | |
## パターン2: sugarcube + closure => dealloc が呼ばれないっぽい、ウゲー | |
# self.navigationItem.leftBarButtonItem = | |
# UIBarButtonItem.cancel { self.dismissViewControllerAnimated(true, completion:nil) } | |
## パターン3: => これも暗黙の self があるせいか、dealloc 呼ばれない | |
# self.navigationItem.leftBarButtonItem = UIBarButtonItem.cancel { dismiss_modal } | |
## パターン4: これはもちろん OK | |
# btn = UIBarButtonItem.cancel | |
# btn.action = 'dismiss_modal' | |
# btn.target = self | |
# self.navigationItem.leftBarButtonItem = btn | |
## dismissViewControllerAnimated とは関係内文脈でクロージャで self を持つと・・・ | |
# => これをするのも NG | |
# ただし、sugarcube が悪いのかどうなのかまでは検証できておらず | |
# self.navigationItem.rightBarButtonItem = UIBarButtonItem.titled('hoge') do | |
# self | |
# end | |
## パターン5: weakref | |
this = WeakRef.new(self) | |
self.navigationItem.leftBarButtonItem = | |
UIBarButtonItem.cancel { this.dismissViewControllerAnimated(true, completion:nil) } | |
end | |
def dismiss_modal | |
self.dismissViewControllerAnimated(true, completion:nil) | |
end | |
def dealloc | |
NSLog('dealloc ModalViewController') | |
super | |
end | |
end | |
class MyObject | |
def dealloc | |
NSLog("dealloc MyObject") | |
super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment