IOS_UI_传值
来源:互联网 发布:九浅一深网络剧百度云 编辑:程序博客网 时间:2024/06/02 02:34
从前往后传值
需要定义两个界面,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 = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
[_window release];
//UIViewController的使用
//1. UIViewController的一个对象代表一个页面,负责处理一部分功能(登陆页面,注册页面,找回密码页面...分别就是不同的viewController)
//2. UIViewController是一个抽象类,即:不能直接使用,如果要使用需要创建它的子类才能使用
MainViewController *mainVC = [[MainViewControlleralloc]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 {
[superviewDidLoad];
// Do any additional setup after loading the view.
//以后的视图创建代码写在这里
//每个视图控制器都自带一个view,用来添加其他视图
self.view.backgroundColor = [UIColorwhiteColor];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
button.backgroundColor = [UIColoryellowColor];
button.frame = CGRectMake(20, 20, 335, 140);
[button setTitle:@"button"forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
self.textField = [[UITextFieldalloc]initWithFrame:CGRectMake(20,200 , 335, 20)];
_textField.backgroundColor = [UIColorwhiteColor];
[self.viewaddSubview:_textField];
_textField.borderStyle =UITextBorderStyleRoundedRect;
_textField.placeholder =@"请输入用户名";
_textField.textAlignment = NSTextAlignmentCenter;
[_textField release];
}
- (void)buttonAction:(UIButton *)button
{
NSLog(@"点击");
button.backgroundColor = [UIColorgreenColor];
//点击按钮弹出新的页面(viewController)
//1.创建新的页面对象
SecondViewController *secondVC = [[SecondViewControlleralloc]init];
//2.3把要传的值在第二个页面弹出之前给第二个页面
secondVC.str = _textField.text;
//2. 弹出(模态) 当前视图器调用
//参数1: 要弹出的新页面(viewControler)
//参数2: 是否需要动画效果
//参数3: 弹出完毕之后执行的代码
//页面2出现的动画效果
secondVC.modalTransitionStyle =UIModalTransitionStylePartialCurl;
[selfpresentViewController:secondVC animated:YEScompletion:^{
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 {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorblueColor];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
button.backgroundColor = [UIColoryellowColor];
button.frame = CGRectMake(20, 120, 335, 140);
[button setTitle:@"返回"forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
UITextField *textField = [[UITextFieldalloc]initWithFrame:CGRectMake(20,300 ,335, 20)];
textField.backgroundColor = [UIColorwhiteColor];
[self.viewaddSubview:textField];
textField.borderStyle =UITextBorderStyleRoundedRect;
textField.placeholder = @"请输入用户名";
//2.4 接收第一个页面传来的值
textField.text = self.str;
textField.textAlignment = NSTextAlignmentCenter;
[textField release];
}
- (void)buttonAction:(UIButton *)button
{
NSLog(@"点击");
//在第二个页面点击返回
[selfdismissViewControllerAnimated:YEScompletion:^{
//返回原页面 此地为空 不用写代码
}];
}
- IOS_UI_传值
- IOS_UI_数据库
- IOS_UI_数据解析
- IOS_UI_网络数据请求
- IOS_UI_本地存储
- iOS_UI_网络数据请求
- IOS_UI_本地存储-复杂的对象写入本地
- IOS_UI_控件总结+九宫格+仿QQ好友列表+小图片拉伸
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值
- 传值
- cocos2d中的屏幕坐标系和OPenGL坐标系转换
- ASP网页中的Base64加密、解密函数代码
- 为什么样本方差(sample variance)的分母是 n-1?
- OCiOS开发:绘图与曲线
- Unique Paths
- IOS_UI_传值
- 挑战自我
- iOS开发-webView添加头部与尾部控件
- 1001. 害死人不偿命的(3n+1)猜想 (15)
- 安装ionic
- 数据结构基础 算法实现
- 我学习编程的一个错误
- honeypots检测技术
- java_自定义异常、打包、自定义模块模式、