您的位置:首页 > 其它

自定义协议protocol(delegate)传值的步骤

2012-05-03 11:31 302 查看
1、在需要传值给其他类的类头文件中定义一个协议。

@protocol stringDelegate<NSObject>

-(void)textField:(NSString *)sender;

@end

2、在该类中声明一个代理属性:

@property (assign,nonatomic)id<stringDelegate>delegate;

3、在.m中实现:

@synthesize delegate ;

4、在需要触发传值的方法中调用协议中的方法:

[delegate textField:aTextField.text];

5、在传值给的类中的.h文件中引用该协议:

@interface rootViewController : UIViewController<stringDelegate>

6、在.m中:

secondVC = [[SecondViewController alloc]init];

secondVC.delegate = self;

7、然后实现该方法:

-(void)textField:(NSString *)sender

{

aLabel.text = sender;

}

所有源代码如下:

Appdelegate

#import <UIKit/UIKit.h>

@interface testProjectAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

#import "testProjectAppDelegate.h"

#import "rootViewController.h"

@implementation testProjectAppDelegate

@synthesize window = _window;

- (void)dealloc

{

[_window release];

[super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// Override point for customization after application launch.

self.window.backgroundColor = [UIColor whiteColor];

rootViewController *rootVC = [[rootViewController alloc]init];

UINavigationController *aNav = [[UINavigationController alloc]initWithRootViewController:rootVC];

self.window.rootViewController = aNav;

[aNav release];

[rootVC release];

[self.window makeKeyAndVisible];

return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application

{

/*

Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to
the background state.

Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

*/

}

- (void)applicationDidEnterBackground:(UIApplication *)application

{

/*

Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

*/

}

- (void)applicationWillEnterForeground:(UIApplication *)application

{

/*

Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

*/

}

- (void)applicationDidBecomeActive:(UIApplication *)application

{

/*

Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

*/

}

- (void)applicationWillTerminate:(UIApplication *)application

{

/*

Called when the application is about to terminate.

Save data if appropriate.

See also applicationDidEnterBackground:.

*/

}

@end

rootViewController类

#import <UIKit/UIKit.h>

#import "SecondViewController.h"

@interface rootViewController : UIViewController<stringDelegate>

{

SecondViewController *secondVC;

UILabel *aLabel;

}

@end

#import "rootViewController.h"

#import "SecondViewController.h"

@implementation rootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

}

return self;

}

- (void)didReceiveMemoryWarning

{

// Releases the view if it doesn't have a superview.

[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.

}

#pragma mark - View lifecycle

// Implement loadView to create a view hierarchy programmatically, without using a nib.

- (void)loadView

{

UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];

self.view = aView;

[aView release];

self.view.backgroundColor = [UIColor redColor];

UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[pushButton setFrame:CGRectMake(140, 200, 40, 40)];

[pushButton setTitle:@"go" forState:UIControlStateNormal];

[pushButton addTarget:self action:@selector(push ) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:pushButton];

aLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 120, 100, 50)];

aLabel.backgroundColor = [UIColor greenColor];

[self.view addSubview:aLabel];

[aLabel release];

secondVC = [[SecondViewController alloc]init];

secondVC.delegate = self;

//[secondVC release];

}

-(void)push

{

[self.navigationController pushViewController:secondVC animated:YES];

}

-(void)textField:(NSString *)sender

{

aLabel.text = sender;

}

/*

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad

{

[super viewDidLoad];

}

*/

- (void)viewDidUnload

{

[super viewDidUnload];

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

-(void)dealloc

{

[secondVC release];

[super dealloc];

}

@end

SecondViewController 类

#import <UIKit/UIKit.h>

@protocol stringDelegate<NSObject>

-(void)textField:(NSString *)sender;

@end

@interface SecondViewController : UIViewController

{

UITextField *aTextField;

id<stringDelegate>delegate; //为了后边更好的用vc2.delegate;

}

@property (assign,nonatomic)id<stringDelegate>delegate;

@end

#import "SecondViewController.h"

@implementation SecondViewController

@synthesize delegate ;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

}

return self;

}

- (void)didReceiveMemoryWarning

{

// Releases the view if it doesn't have a superview.

[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.

}

#pragma mark - View lifecycle

// Implement loadView to create a view hierarchy programmatically, without using a nib.

- (void)loadView

{

UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];

self.view = aView;

[aView release];

self.view.backgroundColor = [UIColor blackColor];

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[aButton setFrame:CGRectMake(140, 140, 40, 40)];

[aButton setTitle:@"back" forState:UIControlStateNormal];

[aButton addTarget:self action:@selector(back ) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:aButton];

aTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 90, 120, 40)];

[self.view addSubview:aTextField];

[aTextField setBorderStyle:UITextBorderStyleRoundedRect];

aTextField.backgroundColor = [UIColor blueColor];

[aTextField release];

}

-(void)back

{

[delegate textField:aTextField.text];

aTextField.text = NULL;

[self.navigationController popViewControllerAnimated:YES];

}

/*

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad

{

[super viewDidLoad];

}

*/

- (void)viewDidUnload

{

[super viewDidUnload];

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

@end

考到工程中可以直接运行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: