iOS傳值

weixin_34195546發表於2017-10-25

A頁面跳轉到B頁面,B頁面向A頁面傳值。

Delegate

A 頁面


#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()<ReturnDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一頁" style:UIBarButtonItemStyleDone target:self action:@selector(nextViewController)];
    // 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)nextViewController{
    
    SecondViewController *svc = [[SecondViewController alloc]init];
    svc.delegate = self;
    [self.navigationController pushViewController:svc animated:YES];
}

- (void)returnString:(NSString *)string {
    
    NSLog(@"%@",string);
}

@end

B頁面

#import <UIKit/UIKit.h>

@protocol ReturnDelegate <NSObject>

- (void)returnString:(NSString *)string;

@end

@interface SecondViewController : UIViewController

@property(nonatomic, weak)id<ReturnDelegate>delegate;

@end


#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(goback)];
    // Do any additional setup after loading the view.
}

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


- (void)goback{
    
    if ([self.delegate respondsToSelector:@selector(returnString:)]) {
        [_delegate returnString:@"6666666"];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

Block

A頁面

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一頁" style:UIBarButtonItemStyleDone target:self action:@selector(nextViewController)];
    // 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)nextViewController{
    
    SecondViewController *svc = [[SecondViewController alloc]init];
    svc.block = ^(NSString *string) {
        NSLog(@"%@",string);
    };
    [self.navigationController pushViewController:svc animated:YES];
}



@end

B頁面

#import <UIKit/UIKit.h>

typedef void(^ReturnBlock)(NSString *string);

@interface SecondViewController : UIViewController

@property(nonatomic, copy)ReturnBlock block;

@end

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(goback)];
    // Do any additional setup after loading the view.
}

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


- (void)goback{
    
    if (self.block != nil) {
        self.block(@"66666666");
    }
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

Notification

A頁面

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一頁" style:UIBarButtonItemStyleDone target:self action:@selector(nextViewController)];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:@"notificationtest" object:nil];
    // 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)nextViewController{
    
    SecondViewController *svc = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:svc animated:YES];
}

- (void)receiveNotification:(NSNotification *)noti{
    // NSNotification 有三個屬性,name, object, userInfo,其中最關鍵的object就是從第三個介面傳來的資料。name就是通知事件的名字, userInfo一般是事件的資訊。
    NSLog(@"%@ === %@ === %@", noti.object, noti.userInfo, noti.name);
}

- (void)dealloc{
    // 移除當前物件監聽的事件
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

B頁面

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(goback)];
    // Do any additional setup after loading the view.
}

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


- (void)goback{
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"notificationtest" object:@"6666666" userInfo:nil];

    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

相關文章