Skip to content

Instantly share code, notes, and snippets.

@katsuyoshi
Last active December 20, 2015 07:39
Show Gist options
  • Save katsuyoshi/6094430 to your computer and use it in GitHub Desktop.
Save katsuyoshi/6094430 to your computer and use it in GitHub Desktop.
# -*- 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)
# 解放手続き
button = self.navigationItem.leftBarButtonItem
button.clear_action
}
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
# @sugarcube_actionをnilにしないと解放されない
class UIBarButtonItem
def clear_action
@sugarcube_action = nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment