我想把A级传递给B级,然后从A级向后调用方法。我不确定你是否理解我。
A类:
public func showAlertFor()
{
let secondController = SecondController()
var alert : UIAlertController = secondController.showAlertForTranslate( **????** )
self.presentViewController(alert, animated: true, completion: nil)
}
public func doSomething
{
println("doSomething")
}
B组:(第二控制器)
public func showAlertForTranslate( **????** )
{
**????**.doSomething()
// do other stuff
}
答案 0 :(得分:0)
答案 1 :(得分:0)
这就是你如何将函数从A类调用到B然后从B调用到A:
class A{
func foo(){
println("A foo");
}
func tellBToCallFoo(){
var myB = B()
myB.fooTheA(self);
}
}
class B{
func fooTheA(myA : A){
myA.foo();
}
}
var myA = A();
myA.tellBToCallFoo()
您也可以使用protocols
protocol myCustomProtocol {
func myCallBackFunction();
}
class A : myCustomProtocol{
func myCallBackFunction(){
println("I AM A")
}
}
class B : myCustomProtocol{
func myCallBackFunction(){
println("I AM B")
}
}
class C{
func doSomethingWithMyCustomProtocol(mcp : myCustomProtocol){
mcp.myCallBackFunction();
}
}
var myA = A();
var myB = B();
var myC = C();
myC.doSomethingWithMyCustomProtocol(myA);
myC.doSomethingWithMyCustomProtocol(myB);
//output:
//I AM A
//I AM B