viewcontroller.swift//navigationcontroller//import uikitimport foundationclass ViewController: Uiviewcontroller,fontsizechangdelegate {var mylabel:uilabel?;//declares an Uilabel object global override Func Viewdidload () {super.viewdidload ()//self.title = "Baidu"; Self.navigationItem.title = "Baidu"; Let Nextitem = Uibarbuttonitem (title: "Next Page", Style:.) Plain, target:self, Action: "NextPage"); Self.navigationItem.rightBarButtonItem = Nextitem; Put a label to be able to display text let rect = CGRect (x:0, y:100, width:320, height:44); MyLabel = UILabel (Frame:rect); mylabel!. Text = "Welcome to Baidu"; Self.view.addSubview (mylabel!); Additional setup after loading the view, typically from a nib. } func NextPage () {NSLog ("button was clicked"); Let svc = Subviewcontroller (); Set this protocol svc.delegate = self; Self.navigationcontroller?. Pushviewcontroller (SVC, animated:true); }//Proxy method Func Fontsizedidchange (Controller:subviewcontroller, Fontsize:int) {println ("Controller is\ (Control ler) fontsize:\ (fontsize) "); Let font = Uifont.systemfontofsize (Float (fontSize));//There is an error mylabel! here. Font = font; } override func Didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources t Hat can be recreated. }}
subviewcontroller.swift//Navigationcontroller//import Uikitimport foundation//define a protocol protocol fontsizechangdelegate:nsobjectprotocol{//Define a protocol function/agent for a function//parameter 1 current controller itself//Parameters 2 font size func fontsizedidch Ange (Controller:subviewcontroller,fontsize:int);} Class Subviewcontroller:uiviewcontroller {var fontsize:int = 20; Define a Delegate object 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 ("Increase font size", 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) {//Call inside the Protocol method delegate?. Fontsizedidchange (self,fontsize:fontsize); }} override Func Didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any Resour Ces that can be recreated. }/*//MARK:-Navigation//In a storyboard-based application, you'll often want to do a little Preparati On 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, Proxy pass value