Last active
December 1, 2017 07:33
-
-
Save manupstairs/b8c58d581dd69e1a11bfe895a640a591 to your computer and use it in GitHub Desktop.
Seems shareFile method not work because UIButton addTarget is weak reference
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
import Foundation | |
protocol ContentDelegate: class { | |
func changeText(_ content: Content) | |
} | |
class Content { | |
var text: String = "" | |
//changeText will work if I delete 'weak' | |
weak var delegate: ContentDelegate? | |
} | |
class Presenter: ContentDelegate { | |
lazy var content: Content = { | |
let content = Content() | |
content.text = "111" | |
content.delegate = self | |
return content | |
}() | |
func changeText(_ content: Content) { | |
content.text = "222" | |
} | |
} | |
class TestClass { | |
lazy var testContent: Content = { | |
let presenter = Presenter() | |
return presenter.content | |
}() | |
} | |
let testClass = TestClass() | |
testClass.testContent.delegate?.changeText(testClass.testContent) | |
print(testClass.testContent.text) |
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 Content1 { | |
var text: String = "" | |
var action: ((Content1) -> Void)? | |
} | |
class Presenter1 { | |
lazy var content: Content1 = { | |
let content = Content1() | |
content.text = "333" | |
content.action = changeText1 | |
return content | |
}() | |
func changeText1(_ content: Content1) { | |
content.text = "444" | |
} | |
} | |
class TestClass { | |
lazy var testContent: Content1 = { | |
let presenter = Presenter1() | |
return presenter.content | |
}() | |
} | |
let testClass = TestClass() | |
if let action = testClass.testContent.action { | |
action(testClass.testContent) | |
} | |
print(testClass.testContent.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CustomButton
如果不存在对ButtonPresenter
的strong reference,点击button触发touchUpInside
,并不能执行shareFile
方法。Presenter
对象被释放,Content
的delegate
将为nilweak
,导致强引用从而隐藏了生命周期的问题