古木小永

IOS 页面跳转

IOS页面跳转有三种方式,

第一种是直接拖拽式

第二种是代码实现

第三种是页面加代码实现


下面分别介绍

1.

第一种是在storyboard中,从控件中拖一个ViewControl作为第二个页面,这时,storyboard中

就存在2个ViewControl,

 然后在第一个页面中,选择按钮右键,Trigger Segues的action拖拽到第二个页面,选择model模式

这时点击第一个页面的button就可以顺利跳转到第二个页面了。


2.

新建一个swift文件,注意是IOS下面的swift文件,起名SecondViewController,然后将ViewControl.swift文件内容复制过来,把子类名称由ViewController变成SecondViewController

在ViewController这个类(也就是第一个页面)中相应其按键操作(跳转操作)

    @IBAction func nextClicked(sender: AnyObject) {


        var second = SecondViewController()


        self.presentViewController(second, animated: true, completion: nil)


    }



就可以跳转到第二个页面了,只是这时候第二个页面还什么内容都没有,因此为一个背景为黑色的空页面。


3. 通过storyboard设置第二个页面的样式,通过代码实现跳转

与1类似,拖拽一个ViewControl,然后拖拽控件构建第二个页面的样式

与2类似,创建一个新的swift文件,在第二个ViewControl的Class属性中选择新建的类SecondViewController,

同时为这个ViewControl设置identifier   这里起名为secondId

在第一个页面的按钮的按键响应中实现跳转

    @IBAction func nextClicked(sender: AnyObject) {


        var sb=UIStoryboard(name:"Main",bundle:nil)


        var vc = sb.instantiateViewControllerWithIdentifier("secondId") as SecondViewController


        self.presentViewController(vc, animated: true, completion: nil)


    }



评论

热度(1)