ios入门之界面基础

来源:互联网 发布:软件系统架构类型 编辑:程序博客网 时间:2024/06/03 18:48

学习移动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 sectionreturn 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.rowvar 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];}

整体的代码结构如下:
这里写图片描述

1 0
原创粉丝点击