从前往后传值
需要定义两个界面,MainViewController和Secondcontroller
1.为了方便要传值的对象需要用属性书写
2.在接受传值的.h文件里设置属性字符串接受第一个页面传来的值
3.在第一个页面把要穿的值在第二个页面弹出之前给第二个页面设定好的属性字符串
4.在接受传值的页面按钮接收第一个页面传来的值
详细代码如下:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (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];
[_window release];
//UIViewController的使用
//1. UIViewController的一个对象代表一个页面,负责处理一部分功能(登陆页面,注册页面,找回密码页面...分别就是不同的viewController)
//2. UIViewController是一个抽象类,即:不能直接使用,如果要使用需要创建它的子类才能使用
MainViewController *mainVC = [[MainViewController alloc]init];
//给window设置一个根视图控制器,作为启动之后默认显示的界面
self.window.rootViewController = mainVC;
[mainVC release];
return YES;
}
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@end
#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()
//2.1. 传值需要用属性写
@property (nonatomic,retain) UITextField *textField;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//以后的视图创建代码写在这里
//每个视图控制器都自带一个view,用来添加其他视图
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.backgroundColor = [UIColor yellowColor];
button.frame = CGRectMake(20, 20, 335, 140);
[button setTitle:@"button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(20,200 , 335, 20)];
_textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_textField];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.placeholder = @"请输入用户名";
_textField.textAlignment = NSTextAlignmentCenter;
[_textField release];
}
- (void)buttonAction:(UIButton *)button
{
NSLog(@"点击");
button.backgroundColor = [UIColor greenColor];
//点击按钮弹出新的页面(viewController)
//1.创建新的页面对象
SecondViewController *secondVC = [[SecondViewController alloc]init];
//2.3把要传的值在第二个页面弹出之前给第二个页面
secondVC.str = _textField.text;
//2. 弹出(模态) 当前视图器调用
//参数1: 要弹出的新页面(viewControler)
//参数2: 是否需要动画效果
//参数3: 弹出完毕之后执行的代码
//页面2出现的动画效果
secondVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:secondVC animated:YES completion:^{
NSLog(@"弹出完毕%@",self.textField.text);
}];
//3. 内存管理
}
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
//2.2 传值用属性可写在延展里 但是以后要用需要写在.h文件里
@property (nonatomic,retain) NSString *str;
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blueColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.backgroundColor = [UIColor yellowColor];
button.frame = CGRectMake(20, 120, 335, 140);
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20,300 , 335, 20)];
textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:textField];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.placeholder = @"请输入用户名";
//2.4 接收第一个页面传来的值
textField.text = self.str;
textField.textAlignment = NSTextAlignmentCenter;
[textField release];
}
- (void)buttonAction:(UIButton *)button
{
NSLog(@"点击");
//在第二个页面点击返回
[self dismissViewControllerAnimated:YES completion:^{
//返回原页面 此地为空 不用写代码
}];
}