Swift中利用AppDelegate实现调用指定ViewController中的函数
2015-05-03 18:43
495 查看
接着上一篇的Blog讲,在我们自定义了TableViewCell之后,我们可能需要点击cell里面的button等操作,比如点击了以后跳转到别的页面,这个时候,因为跳转动作是在tableview所在的viewcontroller(假设为A类)实现的,所以,我们需要在tablewViewCell类里面调用A类的一个实例,这个实例一般是通过AppDelegate类实现的。
具体来看一下实现过程。
我们先来看一下整体的需求:
在“基站列表”这个ViewController里面,我们的TableViewCell
是自定义的,然后在我们自定义的cell里面,有按钮,我们点击按钮以后,跳转到下一个界面,Segue的identifier是“showInfoForStation”。
很明显,我们的需求是:在cell的类中button的action里面,获取到“基站列表”ViewContrller的一个实例,这个实例有个方法,可以实现界面的跳转。
好了,上代码:
首先声明我们想要的viewController的实例,这里我命名为projectDetail
这里在didFinishLaunchingWithOptions函数里面其实得到的是空,因为ViewController只有在它被调用的时候,才被实例化,也就是viewDidLoad只有在首次调用该界面的时候,才会实例化改类。
所以接下来我们需要在ProjectDetailViewControler类的ViewDidLoad函数中真正把这个viewcontroller的实例赋予AppDelegate
这里的代码和上一篇blog的代码一样,就不多介绍,关键性的代码就是那两行:
同时实现一个methode,用于别的类里面调用改方法可以实现页面的跳转:
最后,在自定义的cell里面完成button的点击action函数就ok了
具体来看一下实现过程。
我们先来看一下整体的需求:
在“基站列表”这个ViewController里面,我们的TableViewCell
是自定义的,然后在我们自定义的cell里面,有按钮,我们点击按钮以后,跳转到下一个界面,Segue的identifier是“showInfoForStation”。
很明显,我们的需求是:在cell的类中button的action里面,获取到“基站列表”ViewContrller的一个实例,这个实例有个方法,可以实现界面的跳转。
好了,上代码:
AppDelegate.Swift
import UIKit @UIApplicationMain class 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, 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() }
相关文章推荐
- 如何在ViewController里调用appdelegate里的方法
- AppDelegate单例模式、通过StoryBoardId加载ViewController Swift
- 用UIButton实现页面跳转(AppDelegate +NavigationViewController)
- TabBar实现页面跳转(AppDelegate +NavigationViewController + TabBarViewController)
- 用UIButton实现页面跳转(AppDelegate +NavigationViewController)
- Swift项目中利用SWRevealViewController实现侧滑菜单
- 如何在ViewController里调用app delegate里自己写的的方法或者系统的方法
- 在 Swift 项目中实现侧滑菜单-利用 SWRevealViewController
- C++学习 【4.2】 利用函数实现指定的功能---函数的调用
- [绍棠_Swift] Swift3.0中别的类获取APPDelegate及UITabbarViewController的自定义
- Swift - 集成app启动页广告,切换rootViewController,支持LaunchImage和LaunchScreen.storyboard,支持GIF图片显示,支持视图过渡动画
- iOS实现在webview页面内点击链接,跳转指定App
- 利用ida python 实现复原函数调用的参数 (仅对数据被简单硬编码有效)
- WPF利用委托与事件实现VM对View中控件的调用
- Swift - 使用SWRevealViewController实现侧滑菜单
- iOS圆角view的Swift实现(利用Core Graphics绘制)
- Swift开发教程--如何在别的类中调用AppDelegate中的方法
- PB 利用timer()函数实现定时将数据窗口以excel文档保存至指定地方
- 利用ViewPager实现app的启动画面
- C语言创建一个数组,利用函数调用实现数组的初始化,逆置和清零