protocol 和 delegate

目標:
延續範例http://luckfox.iteye.com/blog/1826126,加上protocol/delegate示範


//
//  UIStoryboardSubViewController.h
//  StoryboardDemo0
//
//  Created by Administrator on 13/3/7.
//  Copyright (c) 2013年 Administrator. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
//step 1. 告知protocol StoryboardSubDelegate的存在
@protocol StoryboardSubDelegate;					 

@interface UIStoryboardSubViewController : UIViewController


@property (strong) NSString *dataString;
@property (weak, nonatomic) IBOutlet UITextField *inputField;
//step 2.宣告一個id<StoryboardSubDelegate>的變數delegate,用來存放物件位址(也就是使用此protocol的Object位址),
//相當於callback中的回Call handle.
@property (weak) id<StoryboardSubDelegate> delegate;


- (IBAction)DoDone:(id)sender;
@end
//step 3.表明此delegate繼承自<NSObject>,如果繼承多個則<N1,N2,...>
@protocol StoryboardSubDelegate<NSObject>
//沒有加上@optional表示必須要實作,反之則可有可無
-(void)second:(UIStoryboardSubViewController *)theSecond inputString:(NSString *)inputString;
@end



//
//  UIStoryboardSubViewController.m
//  StoryboardDemo0
//
//  Created by Administrator on 13/3/7.
//  Copyright (c) 2013年 Administrator. All rights reserved.
//

#import "UIStoryboardSubViewController.h"

@interface UIStoryboardSubViewController ()

@end

@implementation UIStoryboardSubViewController
@synthesize inputField;
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
	inputField.text=self.dataString;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)DoDone:(id)sender
{
	//檢查protocol是否有被實作
	if([self.delegate respondsToSelector:@selector(second:inputString:)])
	{
		//呼叫此protocol的實作函數
		[self.delegate second:self inputString:inputField.text];
	}
	//關掉此ViewController
	[self dismissViewControllerAnimated:YES completion:
	 ^{
		 NSLog(@"done");
	 }];
}
@end

//
//  UIStoryboardDemoViewController.m
//  StoryboardDemo0
//
//  Created by Administrator on 13/3/7.
//  Copyright (c) 2013年 Administrator. All rights reserved.
//

#import "UIStoryboardDemoViewController.h"
#import "UIStoryboardSubViewController.h"
//step 1.protocol StoryboardSubDelegate使用
@interface UIStoryboardDemoViewController ()<StoryboardSubDelegate>

@end

@implementation UIStoryboardDemoViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//過場傳送端
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
	//將資料送到destinationViewController
	id secondCon =segue.destinationViewController;
	[secondCon setValue:self forKey:@"delegate"];
	[secondCon setValue:@"From Hello" forKey:@"dataString"];
}
//step 2.protocol 裡面的method的實作
-(void)second:(UIStoryboardSubViewController *)theSecond inputString:(NSString *)inputString
{
	NSLog(@"received %@",inputString);
}
@end

你可能感兴趣的:(delegate)