swift -NavigationController,代理傳值

來源:互聯網
上載者:User

標籤:select   iba   zed   color   animate   selected   import   ack   addition   

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



swift -NavigationController,代理傳值

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.