IOS-6-UI知识总结-1(代码添加控件、视图切换、多种传值方式)

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];  

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IOT_Elon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值