界面的跳转和传值
2016-07-14 10:09
330 查看
//在APPDelegate.h中,即项目的入口部分
//didFinishLaunchingWithOptions该方法表示APP启动完毕,接下来进入自定义界面
//如下MyViewController是自定义的类,即一个界面实现UIViewController接口
//页面跳转,并传递参数
//MainViewController.h
//效果图
//didFinishLaunchingWithOptions该方法表示APP启动完毕,接下来进入自定义界面
//如下MyViewController是自定义的类,即一个界面实现UIViewController接口
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; MyViewController *myController = [[MyViewController alloc] init]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myController]; self.window.rootViewController = navController; [self.window makeKeyAndVisible]; return YES; }
//MyViewController.h //定义四个控件 @interface MyViewController : UIViewController @property (strong, nonatomic) UILabel *labelNum; @property (strong, nonatomic) UILabel *labelPwd; @property (strong, nonatomic) UITextField *fieldNum; @property (strong, nonatomic) UITextField *fieldPwd; @end //MyViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.labelNum = [[UILabel alloc] init]; self.labelNum.frame = CGRectMake(100, 100, 50, 20); self.labelNum.text = @"Num: "; self.fieldNum = [[UITextField alloc] init]; self.fieldNum.frame = CGRectMake(150, 100, 100, 20); self.fieldNum.borderStyle = UITextBorderStyleRoundedRect; self.labelPwd = [[UILabel alloc] init]; self.labelPwd.frame = CGRectMake(100, 150, 50, 20); self.labelPwd.text = @"Pwd: "; self.fieldPwd = [[UITextField alloc] init]; self.fieldPwd.frame = CGRectMake(150, 150, 100, 20); self.fieldPwd.borderStyle = UITextBorderStyleRoundedRect; UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(100, 200, 80, 20); btn.backgroundColor = [UIColor grayColor]; [btn setTitle:@"login" forState:UIControlStateNormal]; //设置按钮监听事件,监听方法为login [btn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:self.labelNum]; [self.view addSubview:self.fieldNum]; [self.view addSubview:self.labelPwd]; [self.view addSubview:self.fieldPwd]; [self.view addSubview:btn]; }
//页面跳转,并传递参数
-(void) login{ NSLog(@"login"); NSString *num = self.fieldNum.text; NSString *pwd = self.fieldPwd.text; if([num isEqualToString:@"123"] == TRUE){ NSLog(@"yes"); //初始化要跳转的界面 MainViewController *mm = [[MainViewController alloc] init]; //把账号密码传递到下个界面 [mm initData:num two:pwd]; //开始跳转 [self.navigationController pushViewController:mm animated:FALSE]; }else{ NSLog(@"error"); } }
//MainViewController.h
@interface MainViewController : UIViewController //@property( nonatomic) NSString //传递两个参数a、b NSString 类型 -(void) initData:(NSString *) a two:(NSString *) b; @end
//接收参数,并保存 -(void) initData:(NSString *) a two:(NSString *) b{ self.num = a; self.pwd = b; NSLog(@"%@,%@", self.num, self.pwd); }
//效果图
相关文章推荐
- c#窗体传值用法实例详解
- C#简单实现子窗体向父窗体传值的方法
- ASP.NET页面间的传值的几种方法
- 关于C语言中参数的传值问题
- C# Winform窗口之间传值的多种方法浅析
- php下通过POST还是GET来传值
- JS中Iframe之间传值的方法
- JavaScript实现同一页面内两个表单互相传值的方法
- js传值后台中文出现乱码的解决方法
- asp.net 页面间传值方法小结
- json传值以及ajax接收详解
- ASP.NET页面传递值的方式介绍
- JSP页面间传值问题实例简析
- Servlet与JSP间的两种传值情况
- javascript中的变量是传值还是传址的?
- Javascript showModalDialog两个窗体之间传值
- Android学习笔记--使用剪切板在Activity中传值示例代码
- 如何传值在2个页面之间 要求不刷新父页面,并且不能用Querystring传值
- JavaScript新窗口与子窗口传值详解
- 浅析Java方法传值和传引用问题