• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift-NavigationController,代理传值

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
//
//  ViewController.swift
//  NavigationController
//


import UIKit
import Foundation

class 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 UIKit
import 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. } */ }




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift编程语言的相关资料发布时间:2022-07-13
下一篇:
iOS10.0 & Swift 3.0 对于升级项目的建议发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap