2017-08-22 158 views
1

我已經在兩個視圖控制器內做了委託協議。但委託方法不會調用我的代碼片段。這是什麼原因。我無法找到問題,請發佈您的建議來重溫此問題。Swift 3.0委託協議不起作用

主視圖控制器

class ViewController: UIViewController, testDelegateMethod { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let vw = testViewController() 
    vw.delegateTest = self 

    let push = self.storyboard?.instantiateViewController(withIdentifier: "testViewController") 
    self.navigationController?.pushViewController(push!, animated: true) 
} 

func testMethod(value:String) { 
    print("Hai", value) 
} 
} 

副視點控制器

protocol testDelegateMethod { 
func testMethod(value:String) 
} 

class testViewController: UIViewController { 
var delegateTest : testDelegateMethod? 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

@IBAction func actSubmit(_ sender: Any) { 
    delegateTest?.testMethod(value: "Hello how are you!") 
} 

} 
+6

不是'vw.delegateTest = self'是'push.delegateTest = self',不需要這個'self.navigationController?.pushViewController(push!,animated:true)' –

+1

你正在製作testViewController()的對象。爲什麼需要它。不需要這行只是讓push = self.storyboard?.instantiateViewController(withIdentifier:「testViewController」)as! testViewController push.delegateTest = self self.navigationController?.pushViewController(push !, animated:true) –

+0

@ Anbu.Karthik感謝您的支持。我現在改了它。它運作良好。 – Raja

回答

1

更新這些變化viewdidLoad()方法

override func viewDidLoad() { 
    super.viewDidLoad() 

    if let push = self.storyboard?.instantiateViewController(withIdentifier: "testViewController") as? SelectionScreen 
    { 
    push.delegateTest = self 
    } 

} 
+0

這個'self.navigationController的使用是什麼?.pushViewController(push!,animated:true)' –

+0

sry剛剛編輯答案 –

+0

其確定我的兄弟,...答案更新爲更安全的句柄 –

3

你AR問題Ë面對由於這一行

let vw = testViewController() 
    vw.delegateTest = self 

您已經創建的實例testViewController的大衆,並分配大衆實例

的代表在下一行

let push = self.storyboard?.instantiateViewController(withIdentifier: "testViewController") 
self.navigationController?.pushViewController(push!, animated: true) 

您正在創建的不同實例testviewcontroller push

還有無需此代碼,

let vw = testViewController() 
     vw.delegateTest = self 

而是做

let push testViewController = self.storyboard.instantiateViewController(withIdentifier: "testViewController") as! testViewController 
push.delegateTest = self 
self.navigationController?.pushViewController(push, animated: true) 
+1

沒有類的類型它不允許你設置'delegateTest'所以你需要寫let let = self.storyboard ?.instantiateViewController(withIdentifier:「testViewController」)as! testViewController –

+0

@NimishaRanipa - 謝謝。我正在更新答案 –

+0

@Dev_Tandel非常感謝。它的工作現在。 – Raja

2
let vw = testViewController() // Your are doing wrong code 
vw.delegateTest = self 

只能使用此

我希望它會爲你工作

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let push = storyboard.instantiateViewController(withIdentifier:"testViewController")as! testViewController 
push.delegateTest = self 
self.navigationController?.pushViewController(push, animated: true) 
+0

非常感謝。它的工作現在。 – Raja

2

這種合作de snippet沒有意義。您只需創建一個對象,但不會進一步使用它。所以刪除這段代碼片段。

let vw = testViewController() 
     vw.delegateTest = self 

而且這樣做:

override func viewDidLoad() { 
    super.viewDidLoad() 
    let pushVC = self.storyboard?.instantiateViewController(withIdentifier: "testViewController") as! testViewController 
pushVC.delegateTest = self 

    self.navigationController?.pushViewController(pushVC, animated: true) 
} 
+0

非常感謝。它的工作現在。 – Raja

2

我覺得,你把代碼func testMethod(value:String) { print("Hai", value) } 到viewDidLoad中和上述let push = self.storyboard?.instantiateViewController(withIdentifier: "testViewController") self.navigationController?.pushViewController(push!, animated: true)

+0

感謝您的支持。我的錯只是代表的實例 「delegateTest」 vw.delegateTest = self 現在我更新了請檢查它。 selectionVc.delegateTest = self – Raja