IOS/Swift 学习 14-10-14

Swift 初学习函数


1、UIViewContorller的切换
学习

首先是手动添加导航栏上面的Button,今天使用的是文字,具体添加的代码以下动画

// style 是Button的类型,.Plain 就显示文字 而后点击事件写到自己类中的nextPage函数中
let nextView = UIBarButtonItem(title: "下一页", style: .Plain, target: self, action: "nextPage")  

self.navigationItem.rightBarButtonItem = nextView //添加右侧的按钮

下面是nextPage函数的内容,就是切换到SecondContorller第二个页面的代码spa

func nextPage() {
        let nextView = SecondContorller()
        
        nextView.delegate = self // 肯定代理就是本身自己,才会调用 代理中必须实现的函数
        
        // 推出下一个页面 animated 推出的动画
        self.navigationController?.pushViewController(nextView, animated: true)
}

上面的delegate接下来再说。而后就是从SecondContorller返回到第一个ViewContorller代理

self.navigationController?.popViewControllerAnimated(true) //返回上一层

如今就实现了两个页面的切换功能!code



2、protocol协议,实现相似Java中Interface的功能事件


首先,要先写出这个protocol的名称相似Java中Interface,具体的函数功能不要实现ci

protocol TestDelegate { //定义一个协议
    func done(done : Bool) // 使用时要实现的方法
}

而后就须要在SecondContorller中声明此协议,而后再在使用的时候调用里面的方法,这个跟Java中的Interface同样,就再也不多记了。get


值得注意的是,IOS中UIViewContorller跟Android中Acitivity的差别,虽然Activity能够通讯,可是是没有办法直接改变另外一个Activity中的UI的,可是UIViewContorller就能够作到...完it

相关文章
相关标签/搜索