又到了总结的时间了,突然间感觉时间过得好快啊, 总觉的时间不够用,但是这也没办法啊, 只有自己挤时间了,虽然是零基础,但是这并不能代表什么啦,只要努力,收获总还是有的, 同时我也相信广大的博友肯定也有同样的感触吧!
接下来就让我来为大家解读我们今天所学习的内容吧,嘿嘿. 首先在上课刚开始时间,我们做了短短的练习, 对以往的知识有了进一步的了解, 也提高了熟练度, 但是时间总是很快的, 马上我们就迎来了我们今天学习的新内容UINavigationControl!
首先让我来介绍下UINavigationControl的创建方法吧:
1.创建导航控制器, 并指定导航控制器的RootViewController
2.设置导航控制器为rootViewController
具体创建过程我就用代码来告诉大家吧,如下:
FirstViewController *firstVC = [[FirstViewController alloc] init]; NSLog(@"第一个页面初始化完成"); UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:firstVC]; [firstVC release]; NSLog(@"设置第一个页面为导航控制器的rootViewController"); //步骤2.设置导航控制器为window的rootViewController self.window.rootViewController = navC; NSLog(@"设置导航控制器为window的RootViewController"); [navC release];
UINavigationControl,当行控制器, 继承于UIViewController, 视图控制器的视图控制器, 用于管理伊利咧视图控制器,被管理的视图控制器以栈(先进后出, 后进先出)的形式储存
每一个导航控制器都自带一个navigationBar(导航条), UINavigationBar继承于UIView, navigationBar是用于管理导航条的展现(iOS7之前是不透明的,之后就变为透明的啦)
同时我们也可以对导航条以及上面的东西进行设置, 具体方法如下:
//设置导航条是否半透明, 设置背景色,半透明失效 naviVC.navigationBar.translucent = YES; //设置导航条上item的颜色 naviVC.navigationBar.tintColor = [UIColor purpleColor]; //设置导航条样式 naviVC.navigationBar.barStyle = UIBarStyleBlack;
在之后我们便进入了本节课最为重要的了 那就是传值问题, 在今天我们学习的方法中由属性传值和单例传值.
单例传值的创建方法为:
1.类方法
2.返回值类型是当前类
3.方法名:default + 类名
下面为大家带上今天的例子:
下面图示为1 2 3步
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor purpleColor]; // Do any additional setup after loading the view. self.navigationItem.title = @"主页"; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 60, 30)]; label.text = @"用户名"; label.textAlignment = NSTextAlignmentRight; [self.view addSubview:label]; [label release]; UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 60, 30)]; label1.text = @"性别"; label1.textAlignment = NSTextAlignmentRight; [self.view addSubview:label1]; [label1 release]; field = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 30)]; field.borderStyle = UITextBorderStyleRoundedRect; field.placeholder = @"请输入用户名"; field.autocorrectionType = UITextAutocorrectionTypeNo; field.spellCheckingType = UITextSpellCheckingTypeNo; [self.view addSubview:field]; [field release]; field1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 200, 30)]; field1.borderStyle = UITextBorderStyleRoundedRect; field1.placeholder = @"请输入性别"; [self.view addSubview:field1]; [field1 release]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(70, 200, 70, 30); [button addTarget:self action:@selector(changPage:) forControlEvents:UIControlEventTouchUpInside]; [button setTitle:@"登陆" forState:UIControlStateNormal]; [self.view addSubview:button]; UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem]; button1.frame = CGRectMake(180, 200, 70, 30); [button1 addTarget:self action:@selector(changPage:) forControlEvents:UIControlEventTouchUpInside]; [button1 setTitle:@"取消" forState:UIControlStateNormal]; [self.view addSubview:button1]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnThekeyBorld:)]; [self.view addGestureRecognizer:tap]; [tap release]; //创建单例 // Single *single = [Single defaultSingle]; // NSLog(@"%@", single); // Single *single1 = [Single defaultSingle]; // NSLog(@"%@", single1); // Single *single2 = [Single defaultSingle]; // NSLog(@"%@", single2); } - (void)returnThekeyBorld:(UITapGestureRecognizer *)tap { for (UIView *view in self.view.subviews) { if ([view class] == [UITextField class]) { [view resignFirstResponder]; } } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)changPage:(UIButton *)button { Single *single = [Single defaultSingle]; single.nameString = field.text; NSLog(@"%@", single.nameString); HomeDetailViewController *detail = [[HomeDetailViewController alloc] init]; detail.nameString = [NSString stringWithFormat:@"欢迎%@ 性别%@登陆成功", field.text, field1.text]; [self.navigationController pushViewController:detail animated:YES]; [detail release]; }
- (void)viewDidLoad { self.view.backgroundColor = [UIColor redColor]; [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.title = @"详情页"; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 100)]; label.numberOfLines = 0; label.center = self.view.center; // label.text = _nameString; Single *single = [Single defaultSingle]; label.text = single.nameString; label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:label]; [label release]; }
#import <Foundation/Foundation.h> @interface Single : NSObject @property (nonatomic, retain) NSString *nameString, *genderString; //单例的创建方法 //1.类方法 //2.返回值类型是当前类 //3.方法名字:default + 类名 + (Single *)defaultSingle; #import "Single.h" @implementation Single + (Single *)defaultSingle { static Single *single = nil; if (single == nil) { single = [[Single alloc] init]; } return single; } - (void)dealloc { self.nameString = nil; self.genderString = nil; [super dealloc]; }
效果图如下所示:
好了今天的总结就到这里了, 不为别的 只为每天进步一点点, 大家一起努力哈, 圆我们共同的IT梦!