写了个例子,简单介绍了下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