ios入门之界面基础
学习移动app开发,我们常常从讲解基本的控件开始,如UILabel、UISearchBar、UIButton、UITextField等等。在实现一个简单的ios 应用之前,我们首先来看ios开发中一些基本的概念。
视图控制器(View Controllers)
视图控制器是MVC(Modl-View-Controller)模式的逻辑部分。按照字面意思,这个控制器能够控制某个视图。
UIViewController
苹果极力推崇MVC这种开发模式,并且帮我们实现了一个叫做UIViewController的控制器,它是UIKit的一部分。UIKit是众多能够制作交互界面元素的类,如果你在某个类的开头是UI,那么这个类属于UIkit。UIViewController视图属性被连接到一个视图文件,大多数情况下,是一个storyboard文件。
UIViewController提供一些需要的方法和属性,通常我们在使用的时候只需要将UIViewController子类化即可。如:
class mySubController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any addition setup after loading the view } }
在这个例子中,父类就是UIViewController。viewDidLoad()是UIViewcontroller默认的方法。
UINavigationController
我们在编写一个ios软件的时候,往往不只一个界面,界面之前跳转我们常常会用到navigation controller这么一个东西。一个UINavigationController可以在数组中支持多个UIViewController,导航控制器(navigation controller)按照先进后出的堆栈管理原则对我们创建的UIViewController进行管理。通过self.title属性来设置导航栏的标题。如:
self.title =@"登录";
Table View
Table views是用来显示滚动视图的控件,滚动视图是iOS Apps中最常见的用户界面。滚动视图中的每一行叫做cell,cell是用了展示table view中每行的内容。table view可以有很多个cell,多个cell组成section(组)。
在iPhone的设置界面,就是用不同的section把界面分开,像通知中心,控制中心,个人隐私,每个table view都有header和footer,header是在cell上面,footer在cell下面。
Delegation
在很多的OA软件中,往往都有定时提醒这么一个功能。在App内部发生某个事件时,就会发出提醒,为某个事件订阅或者接收提醒的过程叫做delegation(委托)。
例如,我们使用delegate创建table view,并告知要绘制10行。
override func tableView ( tableView: UITableView, numberOfRowsInSection section: Int ) -> Int { //Return the number of rows in the section return 10 }
UITableViewController
UITableViewController会自动创建一个table view,然后设置tableView属性,同时也需要委托自己获取所有需要的delegate方法。
UITableViewDataSource
UITableView的delegate协议有三个必须要写的方法,叫做UITableViewDataSource。这个协议包括组的数量,美组中行的数量,以及cell如何展现。
第一个方法是numberOfSectionsInTableView(_:),如:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { //#warning Potentially incomplete method implementation. //return the number of sections. return 0 }
注:注意到return那行目前是零,这意味着这个table view中没有组。苹果公司增加了一个警告注释,说如果组的个数是零,那么就不会显示行,组包含行cell,没有了组section,行cell也就不会被显示出来。
第二个方法是tableView(_:numberOfRowsInSection:),这个方法决定了某个组里具体有多少行,当然这里也不能为0:
override func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int { //#warning Incomplete method implementation // Return the number of rows in the section return 5 }
第三个方法是tableView(:cellForRowAtIndexPath:),这个方法里有个参数值叫indexPath,是一个NSIndexPath。
section组属性的索引是当前组,cell行属性的索引是当前行:
- 第一组第一行的索引NSIndexPath是0,0。
- 第一组第四行的索引NSIndexPath是0,3。
- 第三组第一行的索引NSIndexPath是2,0。
可以用点语法调用section和row属性:
var currentRow = indexPath.row var currentSection = indexPath.section
tableView代码:
override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell =tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell return cell }
ios跳转实例
为了方便大家的理解,我们先来一个简单的跳转的实例。
1)打开Xcode,点击顶部菜单栏的File -> New -> Project,从模板中选择Single View Application,点击Next。如图:
2)输入项目名称等属性,点击Next。
3)打开Main.storyboard,点击Inspector上工具栏中第一个图标File Inspector,鼠标移动到到中间部分,不勾选Use Auto Layout选项。这时会出现一个对话框,选择iPhone。
4)选中这个界面,然后点击顶部菜单栏的Editor -> Embed In -> Navigation Controller。一个新的scene会增加到Storyboard中,一个scene表示App一屏或者一个界面。Navigation Controller Scene和之前的View Controller Scene是连接在一起的,这连接说明View Controller Scene是Navigation Controller Scene里第一个出现视图,点击Storyboard Editor左下角的盒子按钮打开Document Outline,Document Outline显示了storyboard文件中所有的控件以及控件所处的层次等级。
5)接下来我们在ViewController.m中新建一个按钮,用来跳转到第二个界面。
先创建一个按钮,代码如下:
UIButton * button=[UIButton buttonWithType:UIButtonTypeSystem]; button.frame=CGRectMake(130, 220, 100, 30); [button addTarget:self action:@selector(toNext) forControlEvents:UIControlEventTouchUpInside]; [button setTitle:@"跳转登录" forState:UIControlStateNormal]; [self.view addSubview:button];
然后通过action添加跳转方法:
//跳转到登录界面 -(void)toNext{ UIBarButtonItem * back=[[UIBarButtonItem alloc]init]; back.title = @"返回"; self.navigationItem.backBarButtonItem = back; SecondViewController * second = [[SecondViewController alloc]init]; [self.navigationController pushViewController:second animated:YES]; }
整体的代码结构如下:

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
IOS平台TensorFlow实践:逻辑斯蒂回归(附源码)(一)
更多深度文章,请关注云计算频道:https://yq.aliyun.com/cloud 作者简介: MATTHIJS HOLLEMANS 荷兰人,独立开发者,专注于底层编码,GPU优化和算法研究。目前研究方向为IOS上的深度学习及其在APP上的应用。 推特地址:https://twitter.com/mhollemans 邮件地址:mailto:matt@machinethink.net github地址:https://github.com/hollance 个人博客:http://machinethink.net/ 在使用深度学习网络(deep learning network)进行预测任务之前,首先要训练它。目前有很多训练神经网络的工具,TensorFlow是大部分人的首选。 你可以使用TensorFlow训练你的机器学习模型,然后使用这些模型来进行预测。训练过程通常是在一台强大的机器或者云上进行,但是TensorFlow也可以运行在IOS上,虽然存在一些限制。 本文中,作者详细介绍了如何使用TensorFlow训练一个简单的分类器并应用在IOS app上。本文将会使用Gend...
- 下一篇
Android通过tcpdump抓包
1 手机要有root权限 2 下载tcpdump android模拟器上linux里面有的会自带。 3 adb push c:\wherever_you_put\tcpdump /data/local/tcpdump 4 adb shell chmod 6755 /data/local/tcpdump 5 adb shell, su获得root权限 6 cd /data/local 7 ./tcpdump -i any -p -s 0 -w /sdcard/capture.pcap 命令参数: # "-i any": listen on any network interface # "-p": disable promiscuous mode (doesn't work anyway) # "-s 0": capture the entire packet # "-w": write packets to a file (rather than printing to stdout) ... do whatever you want to capture, then ^C to st...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- MySQL8.0.19开启GTID主从同步CentOS8
- Hadoop3单机部署,实现最简伪集群
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS8编译安装MySQL8.0.19
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Linux系统CentOS6、CentOS7手动修改IP地址