iOS中使用block传值
2015-05-06 11:46
357 查看
用此方法传值可以替代委托了。具体例子
![](https://img-blog.csdn.net/20150506114722579?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZnVzaHVuaW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20150506115008528?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZnVzaHVuaW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
MainView.h #import <UIKit/UIKit.h> @interface MainView : UIViewController { IBOutlet UIButton* btn; IBOutlet UILabel* labShow; } -(IBAction)push:(id)sender; @end
MainView.m #import "MainView.h" #import "SecondView.h" @implementation MainView - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; } -(IBAction)push:(id)sender { SecondView *s = [[SecondView alloc] initwithBlock:Block_copy(^(NSString *str){ NSLog(@"%@",str); labShow.text = str; })]; [self.navigationController pushViewController:s animated:YES]; [s release]; } - (void)viewDidUnload { [super viewDidUnload]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end
Second.h #import <UIKit/UIKit.h> typedef void (^MyBlock)(NSString *); @interface SecondView : UIViewController { IBOutlet UITextField* txtView; MyBlock my; } -(IBAction)back:(id)sender; -(id)initwithBlock:(MyBlock)str; @end
Second.m #import "SecondView.h" @implementation SecondView -(id)initwithBlock:(MyBlock)str { self = [super init]; if(self) { my = str; } return self; } -(IBAction)back:(id)sender { NSString* s = txtView.text; if(my) { my(s); } [self.navigationController popViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(void)dealloc{ Block_release(my); [super dealloc]; } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; } - (void)viewDidUnload { [super viewDidUnload]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end
相关文章推荐
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值) 推荐
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- iOS --- block的一些使用方法(反向传值)
- iOS中使用Block反响传值的用法
- IOS开发: 使用Block在两个界面之间传值(Block高级用法:Block传值)
- iOS中使用block传值
- iOS开发使用Block页面间传值
- iOS block传值的简单使用
- iOS 使用Block实现界面间传值
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- iOS开发:使用Block在两个界面之间传值
- iOS开发:使用Block在两个界面之间传值
- iOS开发:StoryBoard 使用Block在两个界面之间传值
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- iOS使用block从后往前传值
- iOS开发 使用Block实现两个页面互相传值
- iOS中使用block传值
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
- iOS--storyBoard使用Block传值
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)