1. 程式人生 > >兩個View之間互傳資料

兩個View之間互傳資料

1、利用delegate實現

View 1

//  ViewController.h

#import <UIKit/UIKit.h>
#import "AnotherViewController.h"

@interface ViewController : UIViewController<SendMessageDelegate>


@end
//  ViewController.m

#import "ViewController.h"
#import "AnotherViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
        
    UIButton * btn = [[UIButton alloc] init];
    [btn setFrame:CGRectMake(100.0, 100.0, 100.0, 30.0)];
    [btn setBackgroundColor:[UIColor blackColor]];
    [btn setTitle:@"View 1" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(showAnotherView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
	// 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) showAnotherView
{
    AnotherViewController * anotherViewController = [[AnotherViewController alloc] init];
    anotherViewController.str = @"Hello";
    anotherViewController.myDelegate = self;
    [self presentViewController:anotherViewController animated:YES completion:^{
        NSLog(@"View 2 present", nil);
    }];
}

- (void) receiveMessage:(NSString *)msg
{
    NSLog(msg, nil);
}
@end

View 2

//  AnotherViewController.h

#import <UIKit/UIKit.h>

@protocol SendMessageDelegate <NSObject>

- (void) receiveMessage:(NSString *)msg;

@end

@interface AnotherViewController : UIViewController{

    id<SendMessageDelegate> myDelegate;
    
    NSString * str;
    
}

@property (nonatomic, assign) id<SendMessageDelegate> myDelegate;

@property (nonatomic, assign) NSString * str;
@end

//  AnotherViewController.m

#import "AnotherViewController.h"

@interface AnotherViewController ()

@end

@implementation AnotherViewController

@synthesize myDelegate;
@synthesize str;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    NSLog(str,nil);
    
    UIButton * btn = [[UIButton alloc] init];
    [btn setFrame:CGRectMake(100.0, 200.0, 100.0, 30.0)];
    [btn setBackgroundColor:[UIColor blackColor]];
    [btn setTitle:@"View 2" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(sendMsg) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
	
}

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

- (void) sendMsg
{
    if(myDelegate != nil)
    {
        [myDelegate receiveMessage:@"World"];
    }
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"View 2 dimiss", nil);
    }];
}

@end


2、利用NSNotification Center實現