swift -NavigationController,代理传值
来源:互联网 发布:5轴加工中心软件编程 编辑:程序博客网 时间:2024/05/12 07:19
//// ViewController.swift// NavigationController//import UIKitimport Foundationclass ViewController: UIViewController,FontSizeChangDelegate { var myLabel :UILabel?;//声明一个UILabel对象 全局的 override func viewDidLoad() { super.viewDidLoad() //self.title = "百度"; self.navigationItem.title = "百度"; let nextItem = UIBarButtonItem(title: "下一页", style: .Plain, target: self, action: "nextPage"); self.navigationItem.rightBarButtonItem = nextItem; //放一个Label可以显示文字 let rect = CGRect(x: 0, y: 100, width: 320, height: 44); myLabel = UILabel(frame: rect); myLabel!.text = "欢迎来到百度"; self.view.addSubview(myLabel!); // Do any additional setup after loading the view, typically from a nib. } func nextPage(){ NSLog("按钮被点击了"); let svc = SubViewController(); //设置这个协议 svc.delegate = self; self.navigationController?.pushViewController(svc, animated: true); }// 代理方法 func fontSizeDidChange(controller: SubViewController, fontSize: Int) { println("controller is\(controller) fontsize:\(fontSize)"); let font = UIFont.systemFontOfSize(Float(fontSize));//这里有错误 myLabel!.font = font; } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}
//// SubViewController.swift// NavigationController//import UIKitimport Foundation//定义一个协议protocol FontSizeChangDelegate:NSObjectProtocol{ //定义的一个协议函数/代理的一个函数 //参数1 当前controller本身 //参数2 字体大小 func fontSizeDidChange(controller:SubViewController,fontSize:Int);}class SubViewController: UIViewController { var fontSize:Int = 20; //定义一个delegate对象 var delegate:FontSizeChangDelegate?; override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor .whiteColor(); self.title = "Page2"; //self.backgroundColor = UIColor .whiteColor(); // Do any additional setup after loading the view. var btn = UIButton.buttonWithType(.System) as UIButton; var frame = CGRect(x: 100, y: 100, width: 100, height: 100); btn.frame = frame; btn.setTitle("增大字体大小", forState: .Normal); btn.addTarget(self, action: "clickMe:", forControlEvents:.TouchUpInside); self.view.addSubview(btn); } func clickMe(sender:UIButton){ //self.navigationController?.popToRootViewControllerAnimated(true); fontSize++ println("fontsize is\(fontSize)"); if((delegate) != nil) { //调用里面的协议方法 delegate?.fontSizeDidChange(self,fontSize:fontSize); } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */}
1 0
- swift -NavigationController,代理传值
- swift代理传值
- swift代理传值
- swift代码生成navigationController
- swift添加navigationcontroller
- Swift NavigationController的使用
- Swift 传值之代理传值
- Swift语言实现代理传值
- Swift语言实现代理传值
- swift代理传值的实现
- Swift 协议代理传值(十)
- Swift语言实现代理传值
- Swift代理方法反向传值
- swift 用协议实现代理传值功能
- swift 用协议实现代理传值功能
- swift 用协议实现代理传值功能
- Swift语言实现代理传值(RegisterViewController和LoginViewController)
- 【swift_4】swift之代理传值(delegate的用法)
- Hibernate学习笔记(八) — 懒加载与抓取策略
- 黑马程序员 Java基础 IO2
- swift -UIView的使用
- PL/SQL 如何导出INSERT语句
- Spring学习笔记之六(数据源的配置)
- swift -NavigationController,代理传值
- 程序逻辑抽取器-linux之内核源码逆向设计
- int long long __int64的位数
- 黑马程序员 Java基础 多线程
- 20150814Oracle学习笔记
- memcpy 详解
- Tachyon在Spark中的作用(Tachyon: Reliable, Memory Speed Storage for Cluster Computing Frameworks 论文阅读翻译)
- linux学习笔记------网络命令---
- C++ string 用法详解