iOS编程——OC viewController 传值常用方法Delegate和Block的使用

本文通过实例详细讲解了Objective-C(OC)中UIViewController使用Delegate和Block进行数据传递的方法,包括如何设置、实现协议以及防止循环引用等关键点,旨在帮助iOS开发者更好地理解和应用这两种常见传值技术。
摘要由CSDN通过智能技术生成

写了个例子,简单介绍了下Delegate和Block的使用,注意点都在注释里,直接上代码了:

1.FirstViewController.h:

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController


@end

FirstViewController.m:

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController() <SecondViewDelegate>

@property (nonatomic, strong) UITextField   *textField;

@property (nonatomic, strong) UIButton      *button;

@end

@implementation FirstViewController

- (void) viewDidLoad {
    [super viewDidLoad];
    self.title = @"First";
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;

    //通过Delegate和Block来改变textField的text
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake((screenWidth - 300) / 2, 100, 300, 40)];
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    self.textField.text = @"oldValue";
    [self.view addSubview:self.textField];
    
    //跳转button
    self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.button.frame = CGRectMake((screenWidth - 300) / 2, 200, 300, 40);
    [self.button setTitle:@"PushSecondViewController" forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.button setBackgroundColor:[UIColor greenColor]];
    [self.button addTarget:self action:@se
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值