- 浏览: 2964402 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (893)
- android (110)
- iphone (198)
- java (79)
- JavaScript手册-目录 (9)
- JavaScript手册-Array (19)
- JavaScript手册-Boolean (5)
- JavaScript手册-Date (50)
- JavaScript手册-Math (30)
- JavaScript手册-Number (14)
- JavaScript手册-RegExp (7)
- JavaScript手册-String (38)
- JavaScript手册-全局函数 (8)
- JavaScript实用脚本 (7)
- Others (21)
- java-jpcap (7)
- java-thread (1)
- ibm文章 (3)
- classloader (2)
- java-filter (2)
- 运行环境 (33)
- java-正则 (2)
- oracle (1)
- linux-shell (26)
- wap (1)
- sqlite (3)
- wow (1)
- jvm (1)
- git (5)
- unity3d (29)
- iap (2)
- mysql (23)
- nginx (14)
- tomcat (9)
- apache (2)
- php (1)
- ubuntu (40)
- rsa (1)
- golang (21)
- appstore (5)
- sftp (2)
- log4j (2)
- netty (18)
- 测试工具 (6)
- memcache (5)
- 设计模式 (1)
- centos (8)
- google_iab (5)
- iOS专题 (4)
- mac (10)
- 安装配置帮助手册 (2)
- im4java_graphicsmagick (5)
- inotify-tools (1)
- erlang (6)
- 微信支付 (1)
- redis (8)
- RabbitMQ (5)
最新评论
-
heng123:
Netty视频教程https://www.douban.com ...
netty4.0.23 初学的demo -
maotou1988:
使用Netty进行Android与Server端通信实现文字发 ...
netty4.0.23 初学的demo -
码革裹尸:
非常感谢,正好用上
android 呼入电话的监听(来电监听) -
rigou:
提示的/222.177.4.242 无法链接到ip地址,是什 ...
通过 itms:services://? 在线安装ipa ,跨过app-store -
duwanbo:
GridView与数据绑定
(转载)xcode4.5(iphone6 sdk) - Storyboard的简单使用
转载自:http://my.oschina.net/plumsoft/blog/53886
之前做的例子,我们经常会用到.xib文件,在其中我们可以进行界面的设计。不过如果想切换视图,我们就得自己写很多代码。自从苹果推出了Storyboard,我们可以在一个编辑区域设计多个视图,并通过可视化的方法进行各个视图之间的切换。如下图:
上图中有两种箭头:左边的箭头表示程序刚开始运行时加载的控制器;右边的称为Segue,这个表示视图之间的切换,或者表示连接Navigation Controller的Root View Controller。
Storyboard功能强大,通过它不仅可以单独设计每一个视图,还能很简单地实现各个视图之间的切换。
接下来的例子主要显示Storyboard的功能,顺便用Storyboard实现了静态表格等功能。为了显示Storyboard的功能,我们从Empty Application开始我们的例子。
1、运行Xcode 4.5,新建一个Empty Application,名称为Storyboard Test:
2、打开AppDelegate.m,找到didFinishLaunchingWithOptions方法,删除其中代码,使得只有return YES;语句。
3、创建一个Storyboard:
在菜单栏依次选择File — New — New File,在弹出的窗口,左边选择iOS组中的User Interface,右边选择Storyboard:
之后单击Next,选择Device Family为iPhone,单击Next,输入名称MainStoryboard,并设好Group:
单击Create,这样就创建了一个Storyboard。
4、配置程序,使得从MainStoryboard启动:
先单击左边带蓝色图标的Storyboard Test,然后选择Summary,接下来在Main Storyboard中选择MainStoryboard:
这样,当运行程序时,就从MainStoryboard加载内容了。
5、单击MainStoryboard.storyboard,会发现编辑区域是空的。拖一个Navigation Controller到编辑区域:
6、选中右边的View Controller,然后按Delete键删除它。之后拖一个Table View Controller到编辑区域:
7、我们将在这个Table View Controller中创建静态表格,不过先要将其设置为左边Navigation Controller的Root Controller:
选中Navigation Controller,按住Control键,向Table View Controller拉线:
松开鼠标后,在弹出菜单选择Relationship - rootViewController:
之后,两个框之间会出现一个连接线,这个就可以称作是Segue。
8、选中Table View Controller中的Table View,之后打开Attribute Inspector,设置其Content属性为Static Cells:
这样你会发现Table View中出现了三行Cell。在上图你可以设置很多熟悉,比如Style、Section数量等。
9、设置行数:
选中Table View Section,在Attribute Inspector中设置其行数为2:
然后选中每一行,设置其Style为Basic:
设置第一行中Label的值为:Date and Time,第二行中的Label为List;之后选中下方的Navigation Item,在Attribute Inspector设置Title为Root View,Back Button为Root:
10、我们实现单击表格中的Date and Time这一行实现页面转换,在新页面显示切换时的时间。
在菜单栏依次选择File — New — New File,在弹出的窗口左边选择iOS中的Cocoa Touch,右边选择Objective-C class:
单击Next,Class中输入名称DateAndTimeViewController,Subclass中选择UIViewController,但是不要选XIB:
之后,选好位置和Group,完成创建。
11、再次打开MainStoryboard.storyboard,拖一个View Controller到编辑区域,然后选中这个View Controller,打开Identity Inspector,设置class属性为DateAndTimeViewController:
这样,我们就可以向DateAndTimeViewController创建映射了。
12、向新拖入的View Controller添加控件,如下图:
然后将显示为Label的两个标签向DateAndTimeViewController.h中创建映射,名称分别是dateLabel、timeLabel:
13、打开DateAndTimeViewController.m,在ViewDidUnload方法之后添加代码:
//每次切换到这个试图,显示切换时的日期和时间 - (void)viewWillAppear:(BOOL)animated { NSDate *now = [NSDate date]; dateLabel.text = [NSDateFormatter localizedStringFromDate:now dateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterNoStyle]; timeLabel.text = [NSDateFormatter localizedStringFromDate:now dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterLongStyle]; }
在@implementation DateAndTimeViewController 之后,添加:
@synthesize dateLabel,timeLabel;
14、打开MainStoryboard.storyboard,选中表格的行Date and Time,按住Contrl,向View Controller拉线:
在弹出的菜单选择Push:
这样,Root View Controller与DateAndTimeViewController之间就出现了箭头,运行时当点击表格中的那一行,视图就会切换到DateAndTimeViewController。
15、选中DateAndTimeViewController中的Navigation Item,在Attribute Inspector中设置其Title为Date and Time:
16、运行一下,首先程序将加载静态表格,表格中有两行:Date and Time以及List,单击Date and Time,视图切换到相应视图,单击左上角的Root按钮,视图回到Root View。每次进入Date and Time视图,显示的时间都会不同:
17、接下来,我们将要实现,单击List行,显示一个表格,并且单击表格中的某一行,我们可以在新弹出的视图中编辑该行内容。首先创建ViewController文件:ListViewController、ListEditViewController,前者继承UITableViewController,后者继承UIViewController,参照第10步。都不要创建XIB文件。
然后打开MainStoryboard.storyboard,拖一个Table View Controller和View Controller到编辑区域,调整所有视图在编辑区域的位置,如下图:
设置新拖入的Table View Controller和View Controller的class属性分别是ListViewController和ListEditViewController,参考第11步。
18、参照第14步,从Root View Controller中的List那一行向List View Controller拉线,并在弹出菜单也选择Push。然后选中List View Controller的Navigation Item,设置Title及Back Button都为List,可以参照第9步。
19、向List View Controller中的表格区域拖入一个Table View Cell,这样其中就有两个Cell了。设置第一个Cell的Identifier属性为GreenIdentifier。向第一个Cell中拖入一个Label,设置其字体颜色为绿色。同样对第二个Cell进行设置,Identifier属性为RedIdentifier,往其中拖入Label,设置字体颜色为红色。两个Label的Tag属性都设为1。
20、打开ListViewController.m,向其中添加代码:
20.1 在#import的下一行添加代码:
@interface ListViewController () @property (strong, nonatomic) NSMutableArray *listArray; @property (copy, nonatomic) NSDictionary *editedSelection; @end
20.2 在@implementation之后添加代码:
@synthesize listArray; @synthesize editedSelection;
20.3 找到viewDidLoad方法,向其中添加代码:
- (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"Horse", @"Sheep", @"Pig", @"Dog", @"Cat", @"Chicken", @"Duck", @"Goose", @"Tree", @"Flower", @"Grass", @"Fence", @"House", @"Table", @"Chair", @"Book", @"Swing" ,nil]; self.listArray = array; }
20.4 找到numberOfSectionsInTableView方法,使其返回1,并删掉#warning。
20.5 找到numberOfRowsInSection方法,删掉#warning,使其返回[listArray count]:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [listArray count]; }
20.6 找到cellForRowAtIndexPath方法,修改其中代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; NSString *identifier = nil; if (row%2 == 0) { identifier = @"GreenIdentifier"; }else identifier = @"RedIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; UILabel *cellLabel = (UILabel *)[cell viewWithTag:1]; cellLabel.text = [listArray objectAtIndex:row]; return cell; }
21、运行一下,当单击List行时,页面切换到我们List视图:
22、下面实现单击List表格中的某一行,视图切换,并且视图中的内容与之前选中的行相关,然后可以对其进行编辑,返回后,原来的数据也会发生改变。
打开MainStoryboard.storyboard,仿照第14步,从List View Controller中的两行向List Edit View Controller拉线,在弹出菜单选择Push。这样List Edit View Controller视图中就出现了Navigation Item,选中它,设置Title为Edit。这样,单击List表格中的某一行,视图都会切换到List Edit View Controller。
23、打开ListViewController.m,在@end之前添加代码:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { //获取目的ViewController UIViewController *destination = [segue destinationViewController]; if ([destination respondsToSelector:@selector(setPreViewController:)]) { //将自身传递给目的ViewController [destination setValue:self forKey:@"preViewController"]; } if ([destination respondsToSelector:@selector(setSelection:)]) { //目的ViewController中的selection属性存储的就是需要编辑的内容及其在表格中的位置 NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; id object = [self.listArray objectAtIndex:indexPath.row]; NSDictionary *selection = [NSDictionary dictionaryWithObjectsAndKeys: indexPath, @"indexPath", object, @"object", nil]; [destination setValue:selection forKey:@"selection"]; } }
在ViewDidUnload方法之后添加代码:
- (void)setEditedSelection:(NSDictionary *)dict { if (![dict isEqual:editedSelection]) { editedSelection = dict; NSIndexPath *indexPath = [dict objectForKey:@"indexPath"]; id newValue = [dict objectForKey:@"object"]; [listArray replaceObjectAtIndex:indexPath.row withObject:newValue]; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } }
24、打开MainStoryboard.storyboard,找到Edit视图,向其中拖一个Text Field:
然后为这个Text Field向ListEditViewController.h中创建Outlet映射,名称为editText。
25、打开ListEditViewController.h,向其中添加属性:
@property (copy, nonatomic) NSDictionary *selection; @property (weak, nonatomic) id preViewController;
26、打开ListEditViewController.m,添加代码:
26.1 在@implementation的下一行添加代码:
@synthesize preViewController; @synthesize selection;
26.2 找到ViewDidLoad方法,它默认是被注释掉的,去掉其周围注释符,添加代码如下:
- (void)viewDidLoad { [super viewDidLoad]; editText.text = [selection objectForKey:@"object"]; [editText becomeFirstResponder]; }
26.3 在ViewDidUnload方法之后添加代码:
- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if ([preViewController respondsToSelector:@selector(setEditedSelection:)]) { //结束编辑 [editText endEditing:YES]; NSIndexPath *indexPath = [selection objectForKey:@"indexPath"]; id object = editText.text; NSDictionary *editedSelection = [NSDictionary dictionaryWithObjectsAndKeys: indexPath, @"indexPath", object, @"object", nil]; //设置上一个ViewController中的editedSelection,由于设置editedSelection方法 //已经重写,从而对应在表格中的数据会发生变化 [preViewController setValue:editedSelection forKey:@"editedSelection"]; } }
27、运行:
单击List行时,视图切换到上面右图所示。然后单击Pig那一行,视图切换到Edit视图,然后编辑内容,之后返回,这样,原来表格中的内容就会发生改变:
发表评论
-
iOS程序运行生命周期
2015-11-10 09:05 963iOS程序运行生命周期 在文件AppDele ... -
iOS开发系列--IOS程序开发概览
2015-11-10 07:32 1178iOS开发系列--IOS程序开发概览 概览 ... -
2015年11月Xcode7.1(7B91b)打包发布苹果iOS应用指南
2015-11-09 18:29 108782015年11月Xcode7.1(7B91b) ... -
self.navigationController pushViewController执行不成功
2015-11-06 08:28 1610self.navigationControlle ... -
ios NSString format 保留小数点 float double
2015-11-05 17:37 2932ios NSString format 保留小数点 f ... -
自定义 URL Scheme 完全指南
2015-11-04 16:21 900自定义 URL Scheme 完全指南 转载 htt ... -
UIViewController生命周期方法viewDidLoad、viewWillAppear和viewDidAppear
2015-11-01 12:29 2950UIViewController生命周期 ... -
关于self.view.window与viewDidLoad、viewWillAppear、viewDidAppear
2015-11-01 09:36 2537关于self.view.window与viewD ... -
UIScreen学习记录
2015-10-31 08:18 1056UIScreen学习记录 转载自 ... -
使用NSTimer和CGAffineTransformMakeRotation实现旋转动画
2015-10-29 11:53 1824使用NSTimer和CGAffineTransform ... -
【原】iOSCoreAnimation动画系列教程(一):CABasicAnimation【包会】
2015-10-29 08:59 1022【原】iOSCoreAnimation动 ... -
iOS 在UILabel显示不同的字体和颜色
2015-10-27 08:07 1555在项目开发中,我们经常会遇到在这样一种情形:在一个UI ... -
UISlider滑动条的属性介绍以及于标签联合使用实时显示变动值
2015-10-27 08:06 1251UISlider滑动条的属性 ... -
关于使用DSLTableView下拉刷新数据遇到的问题
2015-10-23 21:17 939关于使用DSLTableView下 ... -
使用AdSupport.framework生成IDFA唯一标识符
2015-10-23 17:29 4706使用AdSupport.framework生成IDFA ... -
AppDelegate的详解
2015-10-22 17:51 676AppDelegate的详解 ... -
iOS开发问题集锦
2015-10-22 13:06 5241. Xcode开发连真机运行报错Please ver ... -
iOS 对象属性参数名定义的注意事项不能以alloc,new,copy,mutableCopy 作为开头命名
2015-10-21 15:41 1324property's synthesized g ... -
iOS-自定义的画圆或弧的UIView
2015-10-21 14:20 2652iOS-自定义的画圆或弧的UIView Cu ... -
iOS自定义的模态提示对话框
2015-10-20 14:27 6472iOS自定义的模态提示对话框 基本思路: 1.创建 ...
相关推荐
ios xcode10-xcode14各版本sdk,主要包括7 8 9 10 11 12 13 14个版本以及各小版本,博客中有截图。
xcode SDK ios 打包配置文件 8.0--12.2 全版本的 有需要的可以瞎子啊
vuforia-sdk-ios-6-5-25.zip vuforia sdk for iOS Use the Vuforia SDK to build Android, iOS, and UWP(win10+) applications for mobile devices and digital eyewear. Apps can be built with Android Studio, ...
macos-lion-dp4-10.8+ios6+xcode4.5.zip 含wifi,ipad,iphone等等全套bt种子
IOS xcode 模拟器 iphone 10.0 sdk
Xcode 快速注释插件VVDocumenter-Xcode-master 三个///快速注释 下载解压后 用Xcode打开工程文件,Command + B进行编译,再重启Xcode即可
Xcode4.5缺少的DevToolCore.framework,老版本Xcode4.5中拷贝出来的。考虑到有朋友没有备份老版本Xcode,单独考出这个文件,解决PBXCP报错。 将这个文件解压放到报错提示的位置即可(/Contents/Developer/Library/...
xib2Storyboard - 将Xcode .xib转换为.storyboard文件的工具
facebook-sdk-swift, 在与Facebook平台的Swift 中,集成你的iOS应用程序 在 Swift ( 测试版) 中的Facebook 将你的应用程序与Facebook集成的快速taylored体验。 包括:登录登录通过他们的Facebook凭据验证用户身份。...
xcode7 空工程Empty-Application
不想更新Xcode的,还想在Xcode 上运行的可以将SDK移入指定路径/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
XCode-cocos2dx-集成protobuf的demo工程. 详见 http://blog.csdn.net/yang3wei/article/details/9369735
下拉刷新 ios开发常用代码 已经封装好,更新到xcode4.5 有例子
官方Xcode及模拟器SDK下载-附件资源
这几天安装好了IOS开发软件,所以分享下安装过程。 win7+虚拟机9+mac lion 10.8+XCODE 4.5 安装说明 非常详细 里面包含所有安装软件的下载地址和说明。
从xcode8中拷贝出来的sdk,方便低版本的xcode使用真机调试。 具体存放位置/Applications/Xcode.app[请确认你的xcode名称]/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/ 另:找到在“程序”中...
xcode8调试IOS11 Shift+Command+G进入 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 把解压后的文件复制进去。
Command_Line_Tools_for_Xcode_12.3_beta-2
此文件为xcode的空模板文件,需要向xcode中增加空目标的人可下载,关于添加位置在我本网站博客中有说明,谢谢
vuforia-sdk-uwp-6-5-25.zip vuforia sdk for UWP 6-5-25 Use the Vuforia SDK to build Android, iOS, and UWP(win10+) applications for mobile devices and digital eyewear. Apps can be built with Android ...