IOS-6-UI知识总结-1(代码添加控件、视图切换、多种传值方式)
2015-08-24 17:45
573 查看
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];
}
相关文章推荐
- iOS下UITableView的单元格重用逻辑
- 总结Android UI 自动化测试
- TableViewCell添加2个和多个侧滑菜单按钮
- leetcode 187: Repeated DNA Sequences
- AS3 URLRequest相关,浏览器请求头MIME类型引发的问题
- 类似与fiddler的抓包工具 burp suite free edition
- iOS开发-Day29-UI UIScrollView&多视图
- POJ3210 a stack or a queue
- MII、GMII、RMII、SGMII、XGMII、XAUI、Interlaken
- DuiLib : 使用CListUI显示海量数据
- JAVA UUID 生成
- easyiui textbox 光标 聚焦
- 程序检测系统是bios引导还是uefi引导
- UITabbarViewController+Navigation框架下的子视图的跳转示例之一
- UIActionSheet的常见用法
- poj 2051 priority_queue应用
- CF_313B_IlyaAndQueries
- 08-排序3. Talent and Virtue (25)
- 调用[[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad判断设备
- UE4 WorldPositionOffset