用此方法傳值可以替代委託了。具體例子 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
{
[superdidReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[superviewDidLoad];
}
-(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
{
[superviewDidUnload];
}
- (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.navigationControllerpopViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
}
-(void)dealloc{
Block_release(my);
[super dealloc];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[superviewDidLoad];
}
- (void)viewDidUnload
{
[superviewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end