第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何設(shè)置一個(gè)簡(jiǎn)單的委托,以便在兩個(gè)視圖控制器之間進(jìn)行通信?

如何設(shè)置一個(gè)簡(jiǎn)單的委托,以便在兩個(gè)視圖控制器之間進(jìn)行通信?

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

3 回答

?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

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

首先,您需要聲明一個(gè)協(xié)議:

讓我們稱(chēng)之為MyFirstControllerDelegate.h

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

進(jìn)口MyFirstControllerDelegate.h存檔并確認(rèn)第一控制器帶協(xié)議MyFirstControllerDelegate

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

在實(shí)現(xiàn)文件中,您需要實(shí)現(xiàn)協(xié)議的兩個(gè)功能:

@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

的實(shí)現(xiàn)文件中第二控制器.

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

這里是關(guān)于委托的wiki文章。


查看完整回答
反對(duì) 回復(fù) 2019-06-14
?
浮云間

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊

下面的解決方案是使用委托將數(shù)據(jù)從VC2發(fā)送到VC1的非?;竞秃?jiǎn)單的方法。

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

聲明了一個(gè)協(xié)議,并創(chuàng)建了一個(gè)代表變?nèi)?/trans>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確認(rèn)協(xié)議,并期望通過(guò)委托方法接收數(shù)據(jù)。發(fā)送數(shù)據(jù)

    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            }
        }
    }


查看完整回答
反對(duì) 回復(fù) 2019-06-14
  • 3 回答
  • 0 關(guān)注
  • 547 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)