UI - SendValue
2015-10-14 20:49
435 查看
<AppDelegate.m>
<FirstViewController.m>
<SecondViewController.h>
<SecondViewController.m>
#import "AppDelegate.h" #import "FirstViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; //创建一个视图控制器 FirstViewController *firstVC = [[FirstViewController alloc]init]; //创建导航视图控制器 并设置 firstVC 为其根视图控制器 UINavigationController *navigationVC = [[UINavigationController alloc]initWithRootViewController:firstVC]; self.window.rootViewController = navigationVC; 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<FirstViewController.h>
#import <UIKit/UIKit.h> @interface FirstViewController : UIViewController @end
<FirstViewController.m>
#import "FirstViewController.h" #import "SecondViewController.h" @interface FirstViewController ()<UITextFieldDelegate,SecondViewControllerDelegate> @property (nonatomic,retain)UILabel *label; @property (nonatomic,retain)UITextField *tf; @end @implementation FirstViewController -(void)dealloc { self.label = nil; self.tf = nil; [super dealloc]; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor orangeColor]; self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 150, 220, 50) ]; _label.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:_label]; [_label release]; self.tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 220, 220, 50)]; _tf.placeholder = @"请输入"; _tf.borderStyle = UITextBorderStyleRoundedRect; //设置代理 _tf.delegate = self; [self.view addSubview:_tf]; [_tf release]; //自定义 navigationbar [self customNavigationBar]; } -(void)customNavigationBar { //设置 title self.navigationItem.title = @"FirstVC"; //定制 rightitem UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(push:)]; self.navigationItem.rightBarButtonItem = rightItem; [rightItem release]; } -(void)push:(UIBarButtonItem *)buttonItem { SecondViewController *secVC = [[SecondViewController alloc] init]; //将输入框中的值传给下一个控制器 secVC.text = _tf.text; secVC.delegate = self; [self.navigationController pushViewController:secVC animated:YES]; } -(void)sendText:(NSString *)text { _label.text = text; } -(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
<SecondViewController.h>
#import <UIKit/UIKit.h> @class SecondViewController; @protocol SecondViewControllerDelegate <NSObject> @optional //向前一页传值的协议方法 -(void)sendText:(NSString *)text; @end @interface SecondViewController : UIViewController @property (nonatomic,retain)NSString *text; //用于接收第一个界面传入进来的字符串 @property (nonatomic,assign)id<SecondViewControllerDelegate>delegate;//服从协议的代理 @end
<SecondViewController.m>
#import "SecondViewController.h" @interface SecondViewController ()<UITextFieldDelegate> @property (nonatomic,retain)UILabel *label; @property (nonatomic,retain)UITextField *tf; @end @implementation SecondViewController -(void)dealloc { self.label = nil; self.tf = nil; self.text = nil; [super dealloc]; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor orangeColor]; self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 150, 220, 50) ]; _label.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:_label]; [_label release]; self.tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 220, 220, 50)]; _tf.placeholder = @"请输入"; _tf.borderStyle = UITextBorderStyleRoundedRect; //设置代理 _tf.delegate = self; [self.view addSubview:_tf]; [_tf release]; [self customNavigationBar]; //将传入的值赋给 label _label.text = _text; } -(void)customNavigationBar { self.navigationItem.title = @"SecVC"; //自定义navigationbar UIButton *leftBt = [UIButton buttonWithType:UIButtonTypeSystem]; leftBt.frame = CGRectMake(0, 0, 80, 40); [leftBt setTitle:@"pop" forState:UIControlStateNormal]; [leftBt addTarget:self action:@selector(pop:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBt]; self.navigationItem.leftBarButtonItem = leftItem; [leftItem release]; } -(void)pop:(UIButton *)button { [self.navigationController popViewControllerAnimated:YES]; //判断代理是否去实现了协议中的方法 sendText: ,防止没写实现时发生 crash if ([_delegate respondsToSelector:@selector(sendText:)]) { [_delegate sendText:_tf.text]; } } -(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- Android之UI布局
- UI - UINavigationController
- [PAT (Advanced Level) ]1007. Maximum Subsequence Sum 解题文档
- java复习:”Sting类、StringBuffer类、StringBuilder类的应用
- iOS UIView autoresizingMask
- PostgreSQL security usage guide
- PostgreSQL function's SECURITY DEFINER | INVOKER, SET configuration_parameter { TO value | = value |
- UITableView / UITableViewDataSource / UITableViewDelegate
- 《iOS Human Interface Guidelines》——Navigation
- CALayer的position,anchorPoint属性 与UIView的frame 属性
- UICollectionView
- ajax ExecuteNonQuery
- Buildbot初探
- 1007. Maximum Subsequence Sum
- 60 Permutation Sequence
- iOS之UIView简单动画
- 黑马程序员——GUI
- iOS中 DataBase SQL数据库 UI_高级
- iOS中 DataBase SQL数据库 UI_高级
- 关于UITableViewCell的自适应高度使用后的心得记录