swift视图与控制器之间的简单传值
2016-08-31 21:50
295 查看
第一种 代码块(oc)
1 新建一个swift文件(NSobject、view、controller等),代码如下
2.使用方式
第二种 协议(oc)
1 新建一个swift文件(NSobject、view、controller等),代码如下
2使用方法
第三种 通知(oc)
1 新建一个swift文件(NSobject、view、controller等),代码如下
class swiftNotification: UIView {
NSNotificationCenter.defaultCenter().removeObserver(self)//移除通知
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
}
2 使用方式
NSNotificationCenter.defaultCenter().postNotificationName(“notifid”, object: nil)//方式1
NSNotificationCenter.defaultCenter().postNotificationName(“notifiQu”, object: nil)//方式2
NSNotificationCenter.defaultCenter().postNotificationName(“nofiNmae”, object: nil)//方式3
1 新建一个swift文件(NSobject、view、controller等),代码如下
class switfBlock: UIView{ typealias testBlock = (String)->()//声明代码块 var blo: testBlock? //代码块属性 func nameBlock() { self.blo!("fdasf fhjkasdf fajkfh") } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func drawRect(rect: CGRect) { // Drawing code } */ }
2.使用方式
let switfBlocks = switfBlock() switfBlocks.blo = {str in print("fa:\(str)") }
第二种 协议(oc)
1 新建一个swift文件(NSobject、view、controller等),代码如下
protocol swiftDelegateProtocol {//声明协议 func testDelegate(str: String)//方法 } class swiftDelegate: UIView { var delegatessde : swiftDelegateProtocol?//属性协议 func namede() { delegatessde?.testDelegate("fdasf\(swiftShare.shareDlin.names)")//开始传值 } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func drawRect(rect: CGRect) { // Drawing code } */ }
2使用方法
class ViewController: UIViewController, swiftDelegateProtocol { override func viewDidLoad() { super.viewDidLoad() let swiftDelegates = swiftDelegate() swiftDelegates.delegatessde = self swiftDelegates .namede() } func testDelegate(str: String) {//调用协议 print("de:\(str)") } }
第三种 通知(oc)
1 新建一个swift文件(NSobject、view、controller等),代码如下
class swiftNotification: UIView {
func resigerNotifi() { let notifi = NSNotificationCenter.defaultCenter() notifi.addObserverForName("notifid", object: nil, queue: nil,usingBlock: { (notifis: NSNotification) in print("no:\(notifi)") })//方式一 let operationQueue = NSOperationQueue.mainQueue() notifi.addObserverForName("notifiQu", object: nil, queue: operationQueue) { (notofis: NSNotification) in print("noQ:\(notifi)") }//方式2️⃣ //方式3 notifi.addObserver(self, selector:#selector(self.notifiNmae(_:)), name: "nofiNmae", object: nil) notifi.addObserver(self, selector:#selector(notifiNmae(_:)), name: "nofiNmae", object: nil) } func notifiNmae(notifis: NSNotification) { let share = swiftShare.getdataShare2() print("noN:\(notifis)+_\(share.names)") } deist{//消除
NSNotificationCenter.defaultCenter().removeObserver(self)//移除通知
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
}
2 使用方式
NSNotificationCenter.defaultCenter().postNotificationName(“notifid”, object: nil)//方式1
NSNotificationCenter.defaultCenter().postNotificationName(“notifiQu”, object: nil)//方式2
NSNotificationCenter.defaultCenter().postNotificationName(“nofiNmae”, object: nil)//方式3
相关文章推荐
- iOS开发UIViewCOntroller视图控制器之间跳转切换举例,以及视图控制器生命周期的简单说明
- 【iOS开发-20】UIViewCOntroller视图控制器之间跳转切换举例,以及视图控制器生命周期的简单说明
- swift两个视图控制器之间的传值
- swift IOS 视图控制器 UIButton UILabel 控件使用
- 【iOS7的一些总结】3、视图控制器ViewControllers之间的数据传递(2)
- iOS开发利用协议代理实现导航控制器UINavigationController视图之间的正向传值和反向传值
- iOS开发导航控制器下不同视图控制器之间切换:利用CATrasition和view的layer层来实现自定义的动画效果
- 视图控制器之间的切换
- 【转】Ruby on Rails,创建最简单的视图/控制器单元Hello World
- 视图(view)、视图控制器(view controller)之间的数量关系
- 两个视图控制器之间通过delegate传真
- Ruby on Rails,创建最简单的视图/控制器单元Hello World
- IOS使用视图控制器和视图(五)使用 UISegmentedControl 做简单选项分组
- 导航视图控制器中如何定义按钮以及视图之间的切换
- iOS视图控制器之间delegate传值教程
- 主攻ASP.NET.3.5.MVC架构之重生: 控制器与视图之间的值传递(四)
- Ruby on Rails,创建最简单的视图/控制器单元Hello World
- Ruby on Rails,创建最简单的视图/控制器单元Hello World
- swift IOS 视图控制器 UIButton UILabel 控件使用