代理传值,纯代码

来源:互联网 发布:java编写管理系统 编辑:程序博客网 时间:2024/06/09 22:21

声明:全篇文章运行环境xcode6,没有使用storymain。

以下的类名依次是AppDelegate.m   ViewController.m   SecondViewController.h  SecondViewController.m

输出在控制台  PS:就是为了简洁

#import "AppDelegate.h"#import "ViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];[self.window setBackgroundColor:[UIColor whiteColor]]; [self.window makeKeyAndVisible];ViewController * rootview = [[ViewController alloc]init];UINavigationController * rootnavigation = [[UINavigationController alloc]initWithRootViewController:rootview];self.window.rootViewController = rootnavigation;// Override point for customization after application launch.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
#import "ViewController.h"#import "SecondViewController.h"@interface ViewController ()<SecondViewControllerDelegate>@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];[self.view setBackgroundColor:[UIColor yellowColor]];UIButton * buttonok = [UIButton buttonWithType:UIButtonTypeRoundedRect];buttonok.frame = CGRectMake(120, 400, 80, 40);[buttonok.layer setCornerRadius:8];[buttonok setTitle:@"开始传值" forState:UIControlStateNormal];buttonok.showsTouchWhenHighlighted = YES;[self.view addSubview:buttonok];[buttonok addTarget:self action:@selector(begain) forControlEvents:UIControlEventTouchUpInside];}-(void)setvalue:(NSString *)str{NSLog(@"%@",str);}-(void)begain{SecondViewController * second = [[SecondViewController alloc]init];second.delegate = self;// 这句话就表示 第二个视图的代理是第一个视图 即第二个视图的方法由第一个视图来实现 (其中的参数是从第二个视图传过来的)实际上这句语法是和上面的引入相关联的,引入代理,完成代理方法[self.navigationController pushViewController:second animated:YES];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}@end

#import<span style="font-family: Arial, Helvetica, sans-serif;"><UIKit/UIKit.h></span>@protocol SecondViewControllerDelegate <NSObject>-(void)setvalue :(NSString *)str;@end@interface SecondViewController : UIViewController@property id<SecondViewControllerDelegate>delegate;@property(strong,nonatomic)UITextField *textfield;@end#import "SecondViewController.h"@implementation SecondViewController- (void)viewDidLoad {[super viewDidLoad];[self.view setBackgroundColor:[UIColor greenColor]];UIButton * buttonok = [UIButton buttonWithType:UIButtonTypeRoundedRect];buttonok.frame = CGRectMake(120, 400, 80, 40);[buttonok.layer setCornerRadius:8];[buttonok setTitle:@"back" forState:UIControlStateNormal];buttonok.showsTouchWhenHighlighted = YES;[self.view addSubview:buttonok];[buttonok addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];_textfield = [[UITextField alloc]init];[_textfield setFrame:CGRectMake(120, 200, 80, 40)];[_textfield setTextAlignment:NSTextAlignmentCenter];[_textfield setBackgroundColor:[UIColor whiteColor]];[self.view addSubview:_textfield];}-(void)back{if (![self respondsToSelector:@selector(setvalue:)]) {[self.delegate setvalue:_textfield.text];}[self.navigationController popToRootViewControllerAnimated:YES];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}@end




以上的类名依次是AppDelegate.m   ViewController.m   SecondViewController.h  SecondViewController.m
0 0
原创粉丝点击