代理传值,纯代码
来源:互联网 发布: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
- 代理传值,纯代码
- JAVA动态代理 - JDK -- 纯JAVA代码
- 纯代码
- UIScrollView原理、属性、代理方法及有关功能实现(用纯代码方式)
- 纯接口的动态代理
- 【iOS】代理传值与块代码传值
- 代理传值与块代码传值
- 【iOS】代理传值与块代码传值
- UITextField的各种属性,代理方法,常见的使用,纯代码和可视化工具中的都有
- Ajax留言板(纯代码)
- 纯代码布局
- 纯代码设置UILabel
- 纯代码setTextColor无效
- 使用纯代码贴
- 纯代码实现TabHost
- IB VS 纯代码
- 纯代码自定义UITableViewCell
- 纯代码写iOS
- 【html+css】表单提交,允许文件提交
- java环境配置
- Mysql 插入数据库太大导致异常
- 一致性哈希算法及其在分布式系统中的应用
- AppStore加急审核流程
- 代理传值,纯代码
- HTML5本地存储之 indexedDB 完整实例及效果图展示
- http://blog.csdn.net/djxiaoyu_haha/article/details/38315341
- iOS简单画折线图
- Mac 下清除文件夹中的.svn文件
- 3-8
- 爬虫的javascript支持
- Android工程下单独运行java main程序的方法
- 高性能I/O设计模式Reactor和Proactor