亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何設置一個簡單的委托,以便在兩個視圖控制器之間進行通信?

如何設置一個簡單的委托,以便在兩個視圖控制器之間進行通信?

iOS
一只名叫tom的貓 2019-06-14 10:20:59
如何設置一個簡單的委托,以便在兩個視圖控制器之間進行通信?我有兩個UITableViewControllers并需要使用委托將子視圖控制器的值傳遞給父控件。我知道什么是代表,我只想看到一個簡單的榜樣。謝謝
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

下面的代碼只顯示了委托概念的基本用法。您可以根據需要命名變量和類。

首先,您需要聲明一個協議:

讓我們稱之為MyFirstControllerDelegate.h

@protocol MyFirstControllerDelegate- (void) FunctionOne: (MyDataOne*) dataOne;- (void) FunctionTwo: (MyDatatwo*) dataTwo;@end

進口MyFirstControllerDelegate.h存檔并確認第一控制器帶協議MyFirstControllerDelegate

#import "MyFirstControllerDelegate.h"@interface FirstController : UIViewController<MyFirstControllerDelegate>{}@end

在實現文件中,您需要實現協議的兩個功能:

@implementation FirstController 


    - (void) FunctionOne: (MyDataOne*) dataOne      {
          //Put your finction code here
      }
    - (void) FunctionTwo: (MyDatatwo*) dataTwo      {
          //Put your finction code here
      }

     //Call below function from your code
    -(void) CreateSecondController
     {
             SecondController *mySecondController = [SecondController alloc] initWithSomeData:.];
           //..... push second controller into navigation stack 
            mySecondController.delegate = self ;
            [mySecondController release];
     }@end

在你的第二控制器:

@interface SecondController:<UIViewController>{
   id <MyFirstControllerDelegate> delegate;}@property (nonatomic,assign)  id <MyFirstControllerDelegate> delegate;@end

的實現文件中第二控制器.

@implementation SecondController@synthesize delegate;//Call below two function on self.-(void) SendOneDataToFirstController{
   [delegate FunctionOne:myDataOne];}-(void) SendSecondDataToFirstController{
   [delegate FunctionTwo:myDataSecond];}@end

這里是關于委托的wiki文章。


查看完整回答
反對 回復 2019-06-14
?
浮云間

TA貢獻1829條經驗 獲得超4個贊

下面的解決方案是使用委托將數據從VC2發送到VC1的非?;竞秃唵蔚姆椒?。

PS:這個解決方案是在Xcode 9.x和SWIFT 4

聲明了一個協議,并創建了一個代表變入ViewControllerB

    import UIKit

    //Declare the Protocol into your SecondVC
    protocol DataDelegate {
        func sendData(data : String)
    }

    class ViewControllerB : UIViewController {

    //Declare the delegate property in your SecondVC
        var delegate : DataDelegate?
        var data : String = "Send data to ViewControllerA."
        override func viewDidLoad() {
            super.viewDidLoad()
        }

        @IBAction func btnSendDataPushed(_ sender: UIButton) {
                // Call the delegate method from SecondVC
                self.delegate?.sendData(data:self.data)
                dismiss(animated: true, completion: nil)
            }
        }

ViewControllerA確認協議,并期望通過委托方法接收數據。發送數據

    import UIKit
        // Conform the  DataDelegate protocol in ViewControllerA
        class ViewControllerA : UIViewController , DataDelegate {
        @IBOutlet weak var dataLabel: UILabel!

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

        @IBAction func presentToChild(_ sender: UIButton) {
            let childVC =  UIStoryboard(name: "Main", bundle: nil).
            instantiateViewController(withIdentifier:"ViewControllerB") as! ViewControllerB
            //Registered delegate
            childVC.delegate = self
            self.present(childVC, animated: true, completion: nil)
        }

        // Implement the delegate method in ViewControllerA
        func sendData(data : String) {
            if data != "" {
                self.dataLabel.text = data            }
        }
    }


查看完整回答
反對 回復 2019-06-14
  • 3 回答
  • 0 關注
  • 548 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號