Swift中利用AppDelegate实现调用指定ViewController中的函数

Swift中利用AppDelegate实现调用指定ViewController中的函数,第1张

概述接着上一篇的Blog讲,在我们自定义了TableViewCell之后,我们可能需要点击cell里面的button等 *** 作,比如点击了以后跳转到别的页面,这个时候,因为跳转动作是在tableview所在的viewcontroller(假设为A类)实现的,所以,我们需要在tablewViewCell类里面调用A类的一个实例,这个实例一般是通过AppDelegate类实现的。 具体来看一下实现过程。 我们

接着上一篇的Blog讲,在我们自定义了tableVIEwCell之后,我们可能需要点击cell里面的button等 *** 作,比如点击了以后跳转到别的页面,这个时候,因为跳转动作是在tablevIEw所在的vIEwcontroller(假设为A类)实现的,所以,我们需要在tablewVIEwCell类里面调用A类的一个实例,这个实例一般是通过AppDelegate类实现的。

具体来看一下实现过程。

我们先来看一下整体的需求:

在“基站列表”这个VIEwController里面,我们的tableVIEwCell 是自定义的,然后在我们自定义的cell里面,有按钮,我们点击按钮以后,跳转到下一个界面,Segue的IDentifIEr是“showInfoForStation”。

很明显,我们的需求是:在cell的类中button的action里面,获取到“基站列表”VIEwContrller的一个实例,这个实例有个方法,可以实现界面的跳转。

好了,上代码:

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            }


首先声明我们想要的vIEwController的实例,这里我命名为projectDetail

这里在dIDFinishLaunchingWithOptions函数里面其实得到的是,因为VIEwController只有在它被调用的时候,才被实例化,也就是vIEwDIDLoad只有在首次调用该界面的时候,才会实例化改类。

所以接下来我们需要在ProjectDetailVIEwControler类的VIEwDIDLoad函数中真正把这个vIEwcontroller的实例赋予AppDelegate

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 be more than 0        self.tableVIEw.tableheaderVIEw = UIVIEw(frame: CGRectMake(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")    }

这里的代码和上一篇blog的代码一样,就不多介绍,关键性的代码就是那两行:

        let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate        appdelegate.projectDetail = self

同时实现一个methode,用于别的类里面调用改方法可以实现页面的跳转:
    // Methord to show the other VC For the AppDelegate to call    func showStationDetail()->(){        self.performSegueWithIDentifIEr("showInfoForStation",sender: self)    }
这里的跳转动作和之前在storyBoard里面的segue的indentifIEr一样,是showInfoForStation


最后,在自定义的cell里面完成button的点击action函数就ok了

StationtableVIEwCell.swift
    @IBAction func buttonpressed(sender: AnyObject) {        let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate        appdelegate.projectDetail.showStationDetail()    }
总结

以上是内存溢出为你收集整理的Swift中利用AppDelegate实现调用指定ViewController中的函数全部内容,希望文章能够帮你解决Swift中利用AppDelegate实现调用指定ViewController中的函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1088015.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存