ฉันมีสองUITableViewControllersและจำเป็นต้องส่งค่าจากตัวควบคุมมุมมองลูกไปยังผู้ปกครองโดยใช้ผู้รับมอบสิทธิ์ ฉันรู้ว่าผู้ได้รับมอบหมายคืออะไรและแค่ต้องการเห็นตัวอย่างง่ายๆในการติดตาม
ขอบคุณ
ฉันมีสองUITableViewControllersและจำเป็นต้องส่งค่าจากตัวควบคุมมุมมองลูกไปยังผู้ปกครองโดยใช้ผู้รับมอบสิทธิ์ ฉันรู้ว่าผู้ได้รับมอบหมายคืออะไรและแค่ต้องการเห็นตัวอย่างง่ายๆในการติดตาม
ขอบคุณ
คำตอบ:
ตัวอย่างง่ายๆ ...
สมมติว่าตัวควบคุมมุมมองลูกมีUISliderและเราต้องการส่งค่าของตัวเลื่อนกลับไปยังผู้ปกครองผ่านตัวแทน
ในไฟล์ส่วนหัวของตัวควบคุมมุมมองย่อยให้ประกาศประเภทผู้รับมอบสิทธิ์และวิธีการ:
ChildViewController.h
#import <UIKit/UIKit.h>
// 1. Forward declaration of ChildViewControllerDelegate - this just declares
// that a ChildViewControllerDelegate type exists so that we can use it
// later.
@protocol ChildViewControllerDelegate;
// 2. Declaration of the view controller class, as usual
@interface ChildViewController : UIViewController
// Delegate properties should always be weak references
// See http://stackoverflow.com/a/4796131/263871 for the rationale
// (Tip: If you're not using ARC, use `assign` instead of `weak`)
@property (nonatomic, weak) id<ChildViewControllerDelegate> delegate;
// A simple IBAction method that I'll associate with a close button in
// the UI. We'll call the delegate's childViewController:didChooseValue: 
// method inside this handler.
- (IBAction)handleCloseButton:(id)sender;
@end
// 3. Definition of the delegate's interface
@protocol ChildViewControllerDelegate <NSObject>
- (void)childViewController:(ChildViewController*)viewController 
             didChooseValue:(CGFloat)value;
@end
ในการใช้งานตัวควบคุมมุมมองลูกเรียกวิธีการผู้รับมอบสิทธิ์ตามต้องการ
ChildViewController.m
#import "ChildViewController.h"
@implementation ChildViewController
- (void)handleCloseButton:(id)sender {
    // Xcode will complain if we access a weak property more than 
    // once here, since it could in theory be nilled between accesses
    // leading to unpredictable results. So we'll start by taking
    // a local, strong reference to the delegate.
    id<ChildViewControllerDelegate> strongDelegate = self.delegate;
    // Our delegate method is optional, so we should 
    // check that the delegate implements it
    if ([strongDelegate respondsToSelector:@selector(childViewController:didChooseValue:)]) {
        [strongDelegate childViewController:self didChooseValue:self.slider.value];
    }
}
@end
ในไฟล์ส่วนหัวของมุมมองพาเรนต์ของคอนโทรลเลอร์ให้ประกาศว่าใช้ChildViewControllerDelegateโปรโตคอล
RootViewController.h
#import <UIKit/UIKit.h>
#import "ChildViewController.h"
@interface RootViewController : UITableViewController <ChildViewControllerDelegate>
@end
ในการใช้งานตัวควบคุมมุมมองพาเรนต์ให้ใช้วิธีการมอบหมายอย่างเหมาะสม
RootViewController.m
#import "RootViewController.h"
@implementation RootViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ChildViewController *detailViewController = [[ChildViewController alloc] init];
    // Assign self as the delegate for the child view controller
    detailViewController.delegate = self;
    [self.navigationController pushViewController:detailViewController animated:YES];
}
// Implement the delegate methods for ChildViewControllerDelegate
- (void)childViewController:(ChildViewController *)viewController didChooseValue:(CGFloat)value {
    // Do something with value...
    // ...then dismiss the child view controller
    [self.navigationController popViewControllerAnimated:YES];
}
@end
หวังว่านี่จะช่วยได้!
detailViewController.delegate = self;(อยู่-tableView:didSelectRowAtIndexPath:ในข้อมูลโค้ดด้านบน #
                    โค้ดด้านล่างนี้แสดงการใช้แนวคิดพื้นฐานของตัวแทนเท่านั้นคุณตั้งชื่อตัวแปรและคลาสตามความต้องการของคุณ
ก่อนอื่นคุณต้องประกาศโปรโตคอล:
มาเรียกมันว่าMyFirstControllerDelegate.h
@protocol MyFirstControllerDelegate
- (void) FunctionOne: (MyDataOne*) dataOne;
- (void) FunctionTwo: (MyDatatwo*) dataTwo;
@end
นำเข้าไฟล์MyFirstControllerDelegate.hและยืนยันFirstControllerของคุณด้วยโปรโตคอล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
ในSecondControllerของคุณ:
@interface SecondController:<UIViewController>
{
   id <MyFirstControllerDelegate> delegate;
}
@property (nonatomic,assign)  id <MyFirstControllerDelegate> delegate;
@end
ในแฟ้มการดำเนินงานของSecondController
@implementation SecondController
@synthesize delegate;
//Call below two function on self.
-(void) SendOneDataToFirstController
{
   [delegate FunctionOne:myDataOne];
}
-(void) SendSecondDataToFirstController
{
   [delegate FunctionTwo:myDataSecond];
}
@end
นี่คือบทความ wiki เกี่ยวกับผู้รับมอบสิทธิ์
วิธีการแก้ปัญหาต่อไปนี้เป็นวิธีพื้นฐานและเรียบง่ายในการส่งข้อมูลจาก VC2 ไปยัง VC1 โดยใช้ผู้รับมอบสิทธิ์
PS: โซลูชันนี้ทำในXcode 9.X และ Swift 4
ประกาศโปรโตคอลและสร้างผู้แทน var ลงใน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ยืนยันโปรโตคอลและคาดว่าจะรับข้อมูลผ่านวิธีมอบหมายsendData
    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
            }
        }
    }
คุณต้องใช้ผู้รับมอบสิทธิ์และโปรโตคอล นี่คือเว็บไซต์ที่มีตัวอย่างhttp://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html