let vc = ViewController.init()
letnav =UINavigationController.init(rootViewController: vc)
self.window?.rootViewController = nav
ViewController.swift
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
// 存放数据的字典
varcells :NSDictionary?
// 表格
vartableView:UITableView?
// UITableViewDataSource
functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
return(cells?.allKeys.count)!
}
functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
letstr ="cell"
varcell = tableView.dequeueReusableCell(withIdentifier: str)
ifcell ==nil{
cell =UITableViewCell.init(style: .default, reuseIdentifier: str)
}
cell?.textLabel?.text=cells?.allKeys[indexPath.row]as?String;
returncell!
}
// 点击按钮跳转
functableView(_tableView:UITableView, didSelectRowAt indexPath:IndexPath) {
ifindexPath.row==0{
letsec =SecendViewController.init()
// 将值传到下一个界面
sec.data=cells?["国内"]as?NSDictionary
self.navigationController?.pushViewController(sec, animated:true)
}else{
letsec =SecendViewController.init()
// 将值传到下一个界面
sec.data=cells?["国外"]as?NSDictionary
self.navigationController?.pushViewController(sec, animated:true)
}
}
overridefuncviewDidLoad() {
super.viewDidLoad()
// 初始化表格
tableView=UITableView.init(frame:self.view.frame, style: .plain)
// 表格的协议跟代理
tableView?.delegate=self
tableView?.dataSource=self
// 添加到视图上
self.view.addSubview(tableView!)
// 获得plist文件到地址
letpath =Bundle.main.bundlePath
letplistName:NSString="Property List.plist"
letfinalPath:NSString= (pathasNSString).appendingPathComponent(plistNameasString)asNSString
cells=NSDictionary(contentsOfFile:finalPathasString)
}
SecendViewController.swift
import UIKit
class SecendViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
// 存放数据的字典
vardata :NSDictionary?
// 表格
vartableView:UITableView?
// UITableViewDataSource
functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
return(data?.allKeys.count)!
}
functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
letstr ="cell"
varcell = tableView.dequeueReusableCell(withIdentifier: str)
ifcell ==nil{
cell =UITableViewCell.init(style: .default, reuseIdentifier: str)
}
cell?.textLabel?.text=data?.allKeys[indexPath.row]as?String;
returncell!
}
functableView(_tableView:UITableView, didSelectRowAt indexPath:IndexPath) {
ifindexPath.row==0{
letsec =ThirdViewController.init()
sec.data=data?["国内"]as?NSDictionary
self.navigationController?.pushViewController(sec, animated:true)
}else{
letsec =ThirdViewController.init()
sec.data=data?["国外"]as?NSDictionary
self.navigationController?.pushViewController(sec, animated:true)
}
}
overridefuncviewDidLoad() {
super.viewDidLoad()
// 初始化表格
tableView=UITableView.init(frame:self.view.frame, style: .plain)
// 表格的协议跟代理
tableView?.delegate=self
tableView?.dataSource=self
// 添加到视图上
self.view.addSubview(tableView!)
// Do any additional setup after loading the view.
}
ThirdViewController.swift
import UIKit
class ThirdViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
// 存放数据的字典
vardata :NSDictionary?
// 表格
vartableView:UITableView?
// UITableViewDataSource
functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
return3
}
functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
letstr ="cell"
varcell = tableView.dequeueReusableCell(withIdentifier: str)
ifcell ==nil{
cell =UITableViewCell.init(style: .default, reuseIdentifier: str)
}
letdata = ["谢霆锋","周杰伦","刘德华"];
cell?.textLabel?.text= data[indexPath.row];
returncell!
}
functableView(_tableView:UITableView, didSelectRowAt indexPath:IndexPath) {
letsec =FoutViewController.init()
// sec.data = data?["国外"] as? NSDictionary
self.navigationController?.pushViewController(sec, animated:true)
}
overridefuncviewDidLoad() {
super.viewDidLoad()
// 初始化表格
tableView=UITableView.init(frame:self.view.frame, style: .plain)
// 表格的协议跟代理
tableView?.delegate=self
tableView?.dataSource=self
// 添加到视图上
self.view.addSubview(tableView!)
// Do any additional setup after loading the view.
}
FourViewController.swift
import UIKit
class FoutViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate {
// 存放数据的字典
vardata :NSDictionary?
// 表格
vartableView:UITableView?
// UITableViewDataSource
functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
return2
}
functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
letstr ="cell"
varcell = tableView.dequeueReusableCell(withIdentifier: str)
ifcell ==nil{
cell =UITableViewCell.init(style: .default, reuseIdentifier: str)
}
letdata = ["因为爱所以爱","只要为你活一天"];
cell?.textLabel?.text= data[indexPath.row];
returncell!
}
functableView(_tableView:UITableView, didSelectRowAt indexPath:IndexPath) {
let sec = MusicViewController.init()
// sec.data = data?["国外"] as? NSDictionary
self.navigationController?.pushViewController(sec, animated:true)
}
overridefuncviewDidLoad() {
super.viewDidLoad()
// 初始化表格
tableView=UITableView.init(frame:self.view.frame, style: .plain)
// 表格的协议跟代理
tableView?.delegate=self
tableView?.dataSource=self
// 添加到视图上
self.view.addSubview(tableView!)
// Do any additional setup after loading the view.
}
MusicViewController.Swift
import UIKit
importAVFoundation
classMusicViewController:UIViewController{
varaudioPlayer:AVAudioPlayer?
overridefuncviewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
// 设置音乐名称的标签
letlab =UILabel.init(frame:CGRect(x:0, y:100, width:self.view.frame.size.width, height:30))
lab.text="因为爱所以爱"
lab.backgroundColor = .red
lab.textColor= .black;
lab.textAlignment= .center
self.view.addSubview(lab)
// 设置音乐
let path = Bundle.main.path(forResource: "%E5%BC%A0%E7%B4%AB%E8%B1%AA+-+%E5%8F%AF%E4%B8%8D%E5%8F%AF%E4%BB%A5", ofType: "flac")
letpathURL=NSURL(fileURLWithPath: path!)
do{
audioPlayer=tryAVAudioPlayer(contentsOf: pathURLasURL)
}catch{
audioPlayer=nil
}
audioPlayer?.prepareToPlay()
// 播放按钮
letplaybtn =UIButton(frame:CGRect(x:60, y:200, width:100, height:40))
playbtn.backgroundColor=UIColor.cyan
playbtn.setTitle("play", for: .normal)
playbtn.setTitleColor(UIColor.white, for: .normal)
// 暂停按钮
letpausebtn =UIButton(frame:CGRect(x:180, y:200, width:100, height:40))
pausebtn.setTitle("pause", for: .normal)
pausebtn.setTitleColor(UIColor.white, for: .normal)
pausebtn.backgroundColor=UIColor.cyan
// 添加到视图上
self.view.addSubview(playbtn)
self.view.addSubview(pausebtn)
// 按钮方法
playbtn.addTarget(self, action:#selector(play), for: .touchUpInside)
pausebtn.addTarget(self, action:#selector(pause), for: .touchUpInside)
self.view.addSubview(playbtn)
self.view.addSubview(pausebtn)
// Do any additional setup after loading the view.
}
@objcfuncplay(){
audioPlayer?.play()
}
@objcfuncpause(){
audioPlayer?.pause()
}