1. 代码添加控件
以UILabel为例:
UILabel *LB = [[UILabel alloc]initWithFrame:CGRectMake(30, 70, 200,30)];
UIFont *ft = [UIFont fontWithName:@"您好!" size:10];
LB.font = ft;
LB.text = @"哦哦";
LB.textColor = [UIColor grayColor];
LB.backgroundColor = [UIColor redColor];
[self.view addSubview:LB];
2. 视图切换
视图Aà视图B:
[A present..:B];//B覆盖A
视图Bà视图A:
[B dismiss...];//移除B
3. 视图跳转:navigationController 导航视图控制器
RootViewController 根视图
推入一个视图firstVC :[self.navigationController pushViewController:self.firstVC animated:YES];
推出一个视图 :[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
注意:可以推出前一个的、指定的、根视图。
4. 以“IB……”开头的,均有映射关系。
如:“IBOutlet”代表控件类型;“IBAction”代表动作类型。
5.两个界面的上、下传值方法:
(1)、向下传值:AàB
方法1:自定义方法
点击跳转第一个界面按钮时,向下一个界面传值:
- (IBAction)goOne:(id)sender {
UIStoryboard *board =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
self.firstVC = [board instantiateViewControllerWithIdentifier:@"firstVC"]; self.firstVC.nameStr= @"下一个界面的文本框";
[self.navigationController pushViewController:self.firstVC animated:YES];
}
方法2:运用系统自带方法(用于界面间直接连线)
给出的代码默认是被屏蔽的,需要自己打开
#pragma mark - Navigation
// In a storyboard-based application, you will often wantto do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using
SecondVC *vc2 = [segue destinationViewController];//segue目的控制器
vc2.namestr2 = @"下一个界面的文本框";
// Pass the selected object to the new viewcontroller.
}
(2)、向上传值: B -> A
方法1:运用代理委托模式(1对1传值,委托者必须持有被委托者)
第一步:先建立一个协议,里面写一个传值方法:
-(void)fuzhi:(NSString *)str;
第二步:在第2个界面设置一个代理:@property(nonatomic,assign)id<Chuanzhi> aDelegate;
if (self.aDelegate&&[self.aDelegate respondsToSelector:@selector(fuzhi:)]) {
[self.aDelegate fuzhi:self.name2.text];
第三步:在第1个界面遵循协议设置传过来的值;
-(void)fuzhi:(NSString *)str
{
self.textLB.text = str;
}
方法2:运用通知(NSNotification)(1对多传值,不需要持有)
第一步:在第2个界面建立并发送一个通知:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:self.name2.text,@"Name", nil];
NSNotification *nontion= [NSNotification notificationWithName:@"Zhi" object:self userInfo:dict];
[[NSNotificationCenter defaultCenter]postNotification:nontion];
第二步:在第1个界面注册接收一个通知:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(fuzhi:) name:@"Zhi" object:nil];
-(void)fuzhi:(NSNotification *)notificat
{
NSDictionary *dict =[notificat userInfo];
NSString *str = [dict objectForKey:@"Name"];
self.textLB.text = str;
}
方法3:运用代码块
第一步:在视图1的.h文件声明一个代码块:
#import <UIKit/UIKit.h>
typedef void(^aCzBlock)(NSString *);
@interfaceFirstVCViewController : UIViewController
@property(nonatomic,strong)aCzBlock aBlock;
@end
第二步:在实现文件点击返回按钮时为代码块赋值并执行
- (IBAction)fanHui:(id)sender {
self.aBlock(self.firstaLB.text);
[self.navigationController popViewControllerAnimated:YES];
}
第三步:为视图1的文本框添加传过来的数据:
- (IBAction)goOne:(id)sender {
UIStoryboard *board =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
self.firstVC = [board instantiateViewControllerWithIdentifier:@"firstID"];
self.firstVC.aBlock = ^(NSString *str){
self.rootLB.text = str;
};
[self.navigationController pushViewController:self.firstVC animated:YES];
}