1.打开xcode,然后选择ios--Application--Empty Application一个空项目。

项目目录:

2.输入项目名称以及选择保存路径即可。

3.创建文件夹Model、Controller。

4.Model文件夹创建User类:User.h User.m

代码:

User.h:

  1. #import <Foundation/Foundation.h>
  2. @interface User : NSObject
  3. @property  (nonatomic, retain) NSString *name;
  4. @property  (nonatomic, retain) NSString *pword;
  5. @end

User.m:

  1. #import "User.h"
  2. @implementation User
  3. @synthesize name;
  4. @synthesize pword;
  5. @end

5.创建controller文件里的4个文件。

TLViewController.h:

  1. #import <UIKit/UIKit.h>
  2. #import "UserDelegate.h"
  3. @interface TLViewController : UIViewController<UserDelegate>
  4. @end

UserDelegate协议类在后面。

TLViewController.m:

  1. #import "TLViewController.h"
  2. #import "User.h"
  3. #import "AddViewController.h"
  4. @interface TLViewController ()
  5. @end
  6. @implementation TLViewController{
  7. UILabel *labelname ;
  8. UILabel *labelpwd ;
  9. User *user;
  10. }
  11. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  12. {
  13. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  14. if (self) {
  15. }
  16. return self;
  17. }
  18. - (void)viewDidLoad
  19. {
  20. //显示用户名
  21. labelname = [[UILabel alloc]initWithFrame:CGRectMake(50, 20, 200, 50)];
  22. //设置显示文字
  23. labelname.text =[NSString stringWithFormat:@"用户名:%@",user.name];
  24. //设置字体:粗体,正常的是 SystemFontOfSize
  25. labelname.font = [UIFont boldSystemFontOfSize:20];
  26. //设置文字颜色
  27. labelname.textColor = [UIColor blackColor];
  28. [self.view addSubview:labelname];
  29. [labelname release];
  30. //显示密码
  31. labelpwd = [[UILabel alloc]initWithFrame:CGRectMake(50, 70., 200, 50)];
  32. //设置显示文字
  33. labelpwd.text = [NSString stringWithFormat:@"密   码:%@",user.pword];
  34. //设置字体:粗体,正常的是 SystemFontOfSize
  35. labelpwd.font = [UIFont boldSystemFontOfSize:20];
  36. //设置文字颜色
  37. labelpwd.textColor = [UIColor blackColor];
  38. [self.view addSubview:labelpwd];
  39. [labelpwd release];
  40. UIButton *btnAdd=[[UIButton alloc] initWithFrame:CGRectMake(10, 130, 300, 30)];
  41. [btnAdd setTitle:@"返    回" forState:UIControlStateNormal];
  42. [btnAdd setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  43. [btnAdd.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
  44. btnAdd.backgroundColor = [UIColor redColor];
  45. [btnAdd addTarget:self action:@selector(BackView) forControlEvents :UIControlEventTouchUpInside];
  46. [self.view addSubview:btnAdd];
  47. [btnAdd release];
  48. [super viewDidLoad];
  49. }
  50. -(void)BackView{
  51. //    AddViewController *ad=[[AddViewController alloc] init];
  52. [self dismissViewControllerAnimated:YES completion:nil];
  53. //    [ad release];
  54. }
  55. -(void)setValue:(User *)userValue{
  56. user=userValue;
  57. }
  58. - (void)didReceiveMemoryWarning
  59. {
  60. [super didReceiveMemoryWarning];
  61. // Dispose of any resources that can be recreated.
  62. }
  63. @end

AddViewController.h:

  1. #import <UIKit/UIKit.h>
  2. #import "UserDelegate.h"
  3. @interface AddViewController : UIViewController<UITextFieldDelegate>{
  4. id<UserDelegate> deleage;
  5. }
  6. @property(assign,nonatomic)id<UserDelegate> delegate;
  7. @end

AddViewController.m:

  1. #import "AddViewController.h"
  2. #import "User.h"
  3. #import "TLViewController.h"
  4. @interface AddViewController ()
  5. {
  6. UITextField *tfname;
  7. UITextField *tfpassword;
  8. }
  9. @end
  10. @implementation AddViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  11. {
  12. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  13. if (self) {
  14. // 下一个界面的返回按钮
  15. UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
  16. temporaryBarButtonItem.title = @"返回";
  17. self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
  18. [temporaryBarButtonItem release];
  19. }
  20. return self;
  21. }
  22. @synthesize delegate;
  23. - (void)viewDidLoad
  24. {
  25. [super viewDidLoad];
  26. UILabel *lname = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 100, 30)];
  27. //设置显示文字
  28. lname.text = @"用户名:";
  29. //设置字体:粗体,正常的是 SystemFontOfSize
  30. lname.font = [UIFont boldSystemFontOfSize:20];
  31. //设置文字颜色
  32. lname.textColor = [UIColor blackColor];
  33. lname.textAlignment=NSTextAlignmentRight;
  34. [self.view addSubview:lname];
  35. [lname release];
  36. UILabel *lpassword = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 100, 30)];
  37. //设置显示文字
  38. lpassword.text = @"密   码:";
  39. //设置字体:粗体,正常的是 SystemFontOfSize
  40. lpassword.font = [UIFont boldSystemFontOfSize:20];
  41. //设置文字颜色
  42. lpassword.textColor = [UIColor blackColor];
  43. lpassword.textAlignment=NSTextAlignmentRight;
  44. [self.view addSubview:lpassword];
  45. [lpassword release];
  46. tfname= [[UITextField alloc] initWithFrame:CGRectMake(110, 40, 200, 30)];
  47. [tfname setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型
  48. tfname.placeholder = @"请输入用户名"; //默认显示的字
  49. tfname.delegate = self;
  50. [self.view addSubview:tfname];
  51. [tfname release];
  52. tfpassword= [[UITextField alloc] initWithFrame:CGRectMake(110, 80, 200, 30)];
  53. [tfpassword setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型
  54. tfpassword.placeholder = @"请输入密码"; //默认显示的字
  55. tfpassword.delegate = self;
  56. tfpassword.secureTextEntry = YES; //密码
  57. [self.view addSubview:tfpassword];
  58. [tfpassword release];
  59. UIButton *btnAdd=[[UIButton alloc] initWithFrame:CGRectMake(10, 130, 300, 30)];
  60. [btnAdd setTitle:@"登       陆" forState:UIControlStateNormal];
  61. [btnAdd setTitle:@"登陆中......" forState:UIControlStateHighlighted];
  62. [btnAdd setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  63. [btnAdd setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
  64. [btnAdd.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
  65. btnAdd.backgroundColor = [UIColor redColor];
  66. [btnAdd addTarget:self action:@selector(LoginUser) forControlEvents :UIControlEventTouchUpInside];
  67. [self.view addSubview:btnAdd];
  68. [btnAdd release];
  69. // 创建自定义的触摸手势来实现对键盘的隐藏
  70. UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
  71. tapGr.cancelsTouchesInView = NO;
  72. [self.view addGestureRecognizer:tapGr];
  73. }
  74. //键盘的隐藏
  75. -(void)viewTapped:(UITapGestureRecognizer*)tapGr{
  76. [tfname resignFirstResponder];
  77. [tfpassword resignFirstResponder];
  78. }
  79. //登陆并跳转
  80. -(void)LoginUser{
  81. NSLog(@"%@",tfname.text);
  82. TLViewController *tv=[[TLViewController alloc] init];
  83. self.delegate=tv;
  84. User *user=[[User alloc] init];
  85. user.name=tfname.text;
  86. user.pword=tfpassword.text;
  87. [self.delegate setValue:user];
  88. tv.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
  89. [self presentModalViewController:tv  animated:YES];
  90. [user release];
  91. [tv release];
  92. }
  93. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  94. {
  95. [textField resignFirstResponder];
  96. return YES;
  97. }
  98. - (void)didReceiveMemoryWarning
  99. {
  100. [super didReceiveMemoryWarning];
  101. // Dispose of any resources that can be recreated.
  102. }
  103. @end

6.创建UserDelegate类:

UserDelegate.h:

  1. #import <Foundation/Foundation.h>
  2. #import "User.h"
  3. @protocol UserDelegate <NSObject>
  4. -(void)setValue:(User *)userValue;
  5. @end

7.AppDelegate文件:

AppDelegate.h

  1. #import <UIKit/UIKit.h>
  2. @class TLViewController;
  3. @class AddViewController;
  4. @interface AppDelegate : UIResponder <UIApplicationDelegate>
  5. @property (strong, nonatomic) UIWindow *window;
  6. @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
  7. @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
  8. @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
  9. //自定义控件
  10. //@property (strong, nonatomic) TLViewController *viewController;
  11. @property (strong, nonatomic) AddViewController *addviewController;
  12. - (void)saveContext;
  13. - (NSURL *)applicationDocumentsDirectory;
  14. @end

AppDelegate.m:

  1. #import "AppDelegate.h"
  2. #import "TLViewController.h"
  3. #import "AddViewController.h"
  4. @implementation AppDelegate
  5. @synthesize managedObjectContext = _managedObjectContext;
  6. @synthesize managedObjectModel = _managedObjectModel;
  7. @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
  8. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  9. {
  10. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  11. // Override point for customization after application launch.
  12. self.window.backgroundColor = [UIColor whiteColor];
  13. self.addviewController = [[AddViewController alloc] init];
  14. self.window.rootViewController = self.addviewController;
  15. [self.window makeKeyAndVisible];
  16. return YES;
  17. }
  18. - (void)applicationWillResignActive:(UIApplication *)application
  19. {
  20. // 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.
  21. // 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.
  22. }
  23. - (void)applicationDidEnterBackground:(UIApplication *)application
  24. {
  25. // 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.
  26. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  27. }
  28. - (void)applicationWillEnterForeground:(UIApplication *)application
  29. {
  30. // 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.
  31. }
  32. - (void)applicationDidBecomeActive:(UIApplication *)application
  33. {
  34. // 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.
  35. }
  36. - (void)applicationWillTerminate:(UIApplication *)application
  37. {
  38. // Saves changes in the application's managed object context before the application terminates.
  39. [self saveContext];
  40. }
  41. - (void)saveContext
  42. {
  43. NSError *error = nil;
  44. NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
  45. if (managedObjectContext != nil) {
  46. if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
  47. // Replace this implementation with code to handle the error appropriately.
  48. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
  49. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  50. abort();
  51. }
  52. }
  53. }
  54. #pragma mark - Core Data stack
  55. // Returns the managed object context for the application.
  56. // If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
  57. - (NSManagedObjectContext *)managedObjectContext
  58. {
  59. if (_managedObjectContext != nil) {
  60. return _managedObjectContext;
  61. }
  62. NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
  63. if (coordinator != nil) {
  64. _managedObjectContext = [[NSManagedObjectContext alloc] init];
  65. [_managedObjectContext setPersistentStoreCoordinator:coordinator];
  66. }
  67. return _managedObjectContext;
  68. }
  69. // Returns the managed object model for the application.
  70. // If the model doesn't already exist, it is created from the application's model.
  71. - (NSManagedObjectModel *)managedObjectModel
  72. {
  73. if (_managedObjectModel != nil) {
  74. return _managedObjectModel;
  75. }
  76. NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Login" withExtension:@"momd"];
  77. _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
  78. return _managedObjectModel;
  79. }
  80. // Returns the persistent store coordinator for the application.
  81. // If the coordinator doesn't already exist, it is created and the application's store added to it.
  82. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
  83. {
  84. if (_persistentStoreCoordinator != nil) {
  85. return _persistentStoreCoordinator;
  86. }
  87. NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Login.sqlite"];
  88. NSError *error = nil;
  89. _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  90. if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
  91. /*
  92. Replace this implementation with code to handle the error appropriately.
  93. abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
  94. Typical reasons for an error here include:
  95. * The persistent store is not accessible;
  96. * The schema for the persistent store is incompatible with current managed object model.
  97. Check the error message to determine what the actual problem was.
  98. If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
  99. If you encounter schema incompatibility errors during development, you can reduce their frequency by:
  100. * Simply deleting the existing store:
  101. [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
  102. * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
  103. @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
  104. Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
  105. */
  106. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  107. abort();
  108. }
  109. return _persistentStoreCoordinator;
  110. }
  111. #pragma mark - Application's Documents directory
  112. // Returns the URL to the application's Documents directory.
  113. - (NSURL *)applicationDocumentsDirectory
  114. {
  115. return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
  116. }
  117. @end

效果图:

本项目传值用到delegate委托,可以可以用更加单的方法传值,就是在B页面定义一个User类,在A页面初始化B时进行赋值:如b.user=user1;

点击空白区域隐藏键盘,基本都是简单的一个demo而已,仅供参考学习。

项目如果报:release不能使用。

ARC forbids explicit message send of'release'

'release' is unavailable: not available inautomatic reference counting mode

解决办法:

打开当前工程,打开"Build Settings",找到Objective-C Automatic Reference Counting项,将它的值设置为NO。

ios学习-delegate、传值、跳转页面的更多相关文章

  1. IOS微信中看文章跳转页面后点击返回无效

    经过查找原因发现,下面两种链接,链接1返回不了,链接2可以返回. 链接1:http://mp.weixin.qq.com/s?__biz=MzA5NDY5MzcyNA==&mid=265089 ...

  2. IOS微信禁用分享跳转页面返回BUG修复

    fresh(); function fresh() { let isPageHide = false; window.addEventListener('pageshow', function () ...

  3. iOS阶段学习第32天笔记(页面传值方法介绍)

    iOS学习(UI)知识点整理 一.界面传值方法 1.方法一  Block传值  通过SubView视图的Block向View视图传值改变View视图的背景色 实例代码: 1)SubViewContro ...

  4. iOS学习——页面的传值方式

    一.简述 在iOS开发过程中,页面跳转时在页面之间进行数据传递是很常见的事情,我们称这个过程为页面传值.页面跳转过程中,从主页面跳转到子页面的数据传递称之为正向传值:反之,从子页面返回主页面时的数据传 ...

  5. [转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682433 iOS学习之UINavigationController详解与使用(一)添加U ...

  6. iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController

    iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem是上篇,我们接着讲UINavigationController的重要作用,页面的管理和切换. ...

  7. [HTML]js实现页面跳转,页面A跳到另一个页面B.以及页面传值(中文)

    要实现从一个页面A跳到另一个页面B,js实现就在A的js代码加跳转代码 JS跳转大概有以下几种方式: 第一种:(跳转到b.html)<script language="javascri ...

  8. webform基础介绍及页面传值(session,cookie)、跳转页面

    一,IIS 1.首先知道IIS是个什么东西:它是web服务器软件,安装在服务器上,接受客户端发来的请求,并传送给服务器端,然后响应请求并送回给客户端.类似于饭店里的服务员. 2.会安装IIS——控制面 ...

  9. 【2017-05-21】WebForm跨页面传值取值、C#服务端跳转页面、 Button的OnClientClick属性、Js中getAttribute和超链接点击弹出警示框。

    一.跨页面传值和取值: 1.QueryString - url传值,地址传值 优缺点:不占用服务器内存:保密性差,传递长度有限. 通过跳转页面路径进行传值,方式: href="地址?key= ...

随机推荐

  1. MySQL删除重复记录的方法

    参考网上的方法,总结了产出重复记录的方法,欢迎交流. 参考:http://www.cnblogs.com/nzbbody/p/4470638.html 方法1:创建一个新表临时储存数据 假设我们有一个 ...

  2. java.lang.NoClassDefFoundError: com/ibatis/sqlmap/engine/mapping/result/BasicResultMap

    错误日志: java.lang.NoClassDefFoundError: com/ibatis/sqlmap/engine/mapping/result/BasicResultMap     at ...

  3. 排序(5)---------高速排序(C语言实现)

    继shell发明了shell排序过后呢,各位计算机界的大牛们又開始不爽了,为什么他能发明.我就不能发明呢.于是又有个哥们蹦出来了.哎...那么多排序,就木有一个排序是中国人发明的.顺便吐槽一下,一百年 ...

  4. iOS 独立开发记录(下)

    侧边菜单栏 查看Github上相关实现,一开始选择的是SlideMenuControllerSwift,后来决定更改为自定义,使用更简洁的方式. 分离 分离之前的SliderMeanControlle ...

  5. Android中实现跨app之间数据的暴露与接收

    例如一个小项目:实现单词本的添加单词等功能 功能:不同的方式实现跨app之间数据的暴露与接收 暴露端app:实现单词的添加(Word.Translate),增删改查: 接收端app:模糊查询,得到暴露 ...

  6. 从C到汇编:栈是计算机工作的基础

             作者:r1ce        原创作品转载请注明出处       <Linux内核分析> MOOC课程http://mooc.study.163.com/course/U ...

  7. 模板-->Matrix重载运算符:+,-,x

    如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 poj_2118_Firepersons,my_ac_code 简单的测试 INPUT: 1 2 3 1 3 4 3 -1 ...

  8. python瓦登尔湖词频统计

    #瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...

  9. Python之路,Day20 - 分布式监控系统开发

    Python之路,Day20 - 分布式监控系统开发   本节内容 为什么要做监控? 常用监控系统设计讨论 监控系统架构设计 监控表结构设计 为什么要做监控? –熟悉IT监控系统的设计原理 –开发一个 ...

  10. noip 2010 关押罪犯 (二分图染色 并茶几)

    /* 二分图染色版本 两个监狱对应二部图的两部分 在给定的怨气值里二分 对于每一个Ci 进行染色判断是否合法 染色的时候 如果这条边的ci > Ci 这两个人就带分开 即染成不同的颜色 如果染色 ...