Then the previous blog said, after we have customized the Tableviewcell, we may need to click inside the cell button and other operations, such as click on the later to jump to other pages, this time, Because the jump action is implemented in the Viewcontroller of the TableView (assumed to be Class A), we need to invoke an instance of Class A in the Tablewviewcell class, which is typically done by Appdelegate Class is implemented.
See the implementation process in detail.
Let's start by looking at the overall requirements:
In the " Base Station list " This viewcontroller, our Tableviewcell is custom, then in our custom cell inside, there are buttons, we click on the button, jump to the next interface, The identifier of segue is "showinfoforstation".
Obviously, our demand is: in the cell class in the action of the button, get to the "Base station list" Viewcontrller An instance, this instance has a method, can realize the interface jump.
Okay, on the code:
Appdelegate.swift
Import Uikit@uiapplicationmainclass Appdelegate:uiresponder, uiapplicationdelegate { var window:uiwindow? var projectdetail = Projectdetailviewcontroller () func application (application:uiapplication, Didfinishlaunchingwithoptions launchoptions: [Nsobject:anyobject]?) -Bool { //Override point for customization after application launch. Let MAINSB = Uistoryboard (name: "Main", bundle:nil) Self.projectdetail = Mainsb.instantiateviewcontrollerwithidentifier ("PROJECTDETAILVC") as! Projectdetailviewcontroller return True }
first of all we want to declare an instance of Viewcontroller, here I named Projectdetail
Here in the didfinishlaunchingwithoptions function actually get is empty , because Viewcontroller only when it is called, it is instantiated, That is, Viewdidload will instantiate the class only when the interface is first invoked.
So next we need to actually give this Viewcontroller instance to appdelegate in the viewdidload function of the Projectdetailviewcontroler class.
Projectdetailviewcontroller.swfit
Override Func Viewdidload () { super.viewdidload () //Do any additional setup after loading the view. Let appdelegate = Uiapplication.sharedapplication (). Delegate as! Appdelegate appdelegate.projectdetail = self self.tableView.delegate = self self.tableView.dataSource = Self //Remove the blank of the header of the table view, mind the height must is more than 0 self.tableView.table Headerview = UIView (frame:cgrectmake (0, 0, self.tableView.frame.size.width, 0.01)) //Register the custom TableView Cell var nib = uinib (nibname: "Stationtableviewcell", Bundle:nil) Self.tableView.registerNib (NIB, Forcellreuseidentifier: "Cell") }
The code here is the same as the code in the previous blog, not much, the key code is those two lines:
Let appdelegate = Uiapplication.sharedapplication (). Delegate as! Appdelegate appdelegate.projectdetail = self
at the same time implement a Methode, for other classes inside the call modification method can achieve the page jump:
Methord to show the other VCs for the appdelegate-call func Showstationdetail (), () { Self.performseguewith Identifier ("Showinfoforstation", Sender:self) }
The jump action here is the same as before in the storyboard inside of the Segue Indentifier, is Showinfoforstation
Finally, the Click Action function of the button in the custom cell is OK.
Stationtableviewcell.swift
@IBAction func buttonpressed (sender:anyobject) {let appdelegate = Uiapplication.sharedapplication (). Delegate as! Appdelegate appdelegate.projectDetail.showStationDetail () }
Using Appdelegate in Swift to invoke functions in the specified Viewcontroller