2017-01-16 67 views
-2

沒有要求我有兩個控制器UIAlertView中的委託方法在兒童視圖控制器

VC A - >ParentVC B - >Child

警報視圖的委託方法即

func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){} 

VC A聲明。

當我顯示來自VC B的警報委託方法不會在點擊警報按鈕時調用。

+1

將'AlertView'代碼添加到您的問題。 –

+0

添加整個代碼,你如何設置和調用委託方法? – iphonic

+0

FUNC showAlert(){ VAR createAccountErrorAlert:UIAlertView中= UIAlertView中() createAccountErrorAlert.delegate =自 createAccountErrorAlert.tag = sender.tag createAccountErrorAlert.accessibilityIdentifier = 「ReportAbuse」 createAccountErrorAlert.title = 「確認」 createAccountErrorAlert.message = 「測試警報」 createAccountErrorAlert.addButtonWithTitle( 「取消」) createAccountErrorAlert.addButtonWithTitle( 「OK」) createAccountErrorAlert.show() } –

回答

0
protocol alertViewDelegate:class { 
    func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) 
} 

在父類VC一個

weak var delegate:alertViewDelegate() ?= nil 

實現VC B中的委託創建警報視圖的委託對象,並設置委託對象在VC乙

let alertview = alertView() 
alertview.delegate = self 
0

要設置AlertView代表自我,Self意爲Child,將代表改爲VC A.

alertView.delegate = self.parent?.parent 
0

按照以下分步:

  1. 在VC-B創建協議爲:

    protocol AlertViewProtocol:class { 
        func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) 
    } 
    
  2. 添加VAR在VC-B類爲:

    var delegate: AlertViewProtocol? 
    
  3. 使用de使用VC-B的任何分類方法將數據發送到接收方法(即,任何VC-A的方法),這是任何採用該協議的方法。按照這種模式來使用委託:

    delegate?.alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) 
    // Above statement is like a method calling (on delegate var), use your parameters accordingly 
    
  4. 採用協議在recieving類(在這裏,VC-A):

    class ViewControllerA: UIViewController, AlertViewProtocol { 
    ... 
    ... 
    } 
    
  5. 實現VC-A的委託方法:

    func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) { 
        // Do your stuff 
        // when you click on the designated button in your VC-B, this method will be invoked 
    } 
    
  6. 在VC-A中設置實例化VC-B對象的委託。在我而言,這是這樣的:

這裏,vcb?.delegate = self是非常重要的。如果您忘記使用self設置對象的委託屬性,委派過程將不起作用。

@IBAction func showVCB(sender: AnyObject) { 
     let vcb: ViewControllerB? = self.storyboard?.instantiateViewControllerWithIdentifier("viewcontrollerB") as? ViewControllerB 
     vcb?.delegate = self 
     self.presentViewController(vcb!, animated: true, completion: nil) 
    } 

希望,這有助於你理解的代表是如何工作的過程。

相關問題