我的第一个控制器 - ViewController
My first controller - ViewController
class ViewController: UIViewController,testProtocol {
@IBAction func btInit(sender: AnyObject) {
println("Bt Init")
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as targetViewController
self.presentViewController(initViewController,animated: false, nil)
}
var targetController = targetViewController();
override func viewDidLoad() {
super.viewDidLoad()
self.targetController.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func testDelegate(){
println(" in my view controller delegate ")
}
}
在我的第二个视图控制器 - targetViewController
In my second view controller - targetViewController
protocol testProtocol {
func testDelegate() // this function the first controllers
}
class targetViewController: UIViewController {
@IBAction func BtTarget(sender: AnyObject) {
println("bt target pressed")
delegate?.testDelegate()
}
var delegate : testProtocol?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func testDelegate(){
println(" in my target view controller delegate ")
}
}
为什么从来没有在 ViewController 上调用 testDelegate()?我究竟做错了什么?谢谢.
Why is testDelegate() never called on ViewController? What am I doing wrong? Thanks.
我已经阅读了很多关于这个的帖子,但是所有的例子都是用 segue 转换给出的,我不想使用 segue.
I have read a lot of posts about this, but all of the examples are given with segue transition, and I don't want use a segue.
通常你在 prepareForSegue:
中设置一个新的视图控制器的委托属性.你说你没有使用 segue,所以你需要实例化第二个视图控制器并以某种方式呈现它.你可以这样做:
Typically you set a new view controller's delegate property in prepareForSegue:
. You said you're not using a segue, so you'll need to instantiate the second view controller and present it somehow. You can do this by doing something like:
let storyboard = UIStoryboard(name: "AStoryboardName", bundle: nil)
let secondVC = storyboard.instantiateViewControllerWithIdentifier(anIdentifier) as! targetViewController
secondVC.delegate = self
presentViewController(secondVC, animated: true, completion: nil)
您在 both 视图控制器中有一个 testDelegate()
方法,但您只希望在第一个视图控制器中使用它.然后你的第二个视图控制器可以在适当的时候调用 delegate?.testDelegate()
.
You have a testDelegate()
method in both view controllers, but you only want it in the first view controller. Then your second view controller can call delegate?.testDelegate()
at the appropriate time.
最后,您通常想让委托属性变弱,所以我建议将 var delegate : testProtocol?
更改为 weak var delegate: testProtocol?
Finally, you typically want to make delegate properties weak, so I would recommend changing var delegate : testProtocol?
to weak var delegate: testProtocol?
我会阅读代表团.这是一个相对简单的 5 步委派过程,可能会对您有所帮助:
I would read up on delegation. Here is a relatively simple 5 step process to delegation that may help you:
五步委派:
对象 A 是对象 B 的委托,对象 B 会发出消息:
object A is the delegate for object B, and object B will send out the messages:
这篇关于没有segue的两个视图控制器之间的快速委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!