太阳集团所有网址16877属性传值、代理传值、Bl

属性传值、代理传值、Block传值,block传

sfhvhkdvjdol

sfhvhkdvjdol...

*对比总结了页面传值六种方式,以便更好地记忆和应用:
1、属性传值
2、单例传值
3、NSUserDefaults传值
4、代理传值
5、block传值
6、通知传值

*注:本文中的传值,均以下图工程中ViewController和NextViewController页面之间的跳转为例。

太阳集团所有网址16877 1

iOS页面传值

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end

ViewController.m

#import "ViewController.h"
#import "NextViewController.h"
#import "DefaultInstance.h"

@interface ViewController ()<valuePassDelegate>
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *button;

@end

@implementation ViewController

- (UILabel *)label {
    if(!_label){
        _label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
        _label.backgroundColor = [UIColor blackColor];
        _label.textColor = [UIColor whiteColor];
        _label.font = [UIFont systemFontOfSize:20];
    }
    return _label;
}
- (UIButton *)button {
    if(!_button){
        _button = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 200, 40)];
        _button.backgroundColor = [UIColor redColor];
        [_button setTitle:@"跳转到页面二" forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}

- (void) buttonClick {
    //此处待页面传值
    [self presentViewController:vc animated:YES completion:nil];
}

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

    [self.view addSubview:self.label];
    [self.view addSubview:self.button];
}

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

NextViewController.h

#import <UIKit/UIKit.h>

@interface NextViewController : UIViewController

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

本文由太阳集团所有网址16877发布于www.16877.com,转载请注明出处:太阳集团所有网址16877属性传值、代理传值、Bl

您可能还会对下面的文章感兴趣: