1、事先准备若干图片。
2、效果图:
3、控件:UIlabel,UIImageView,UIButton
4、建好的pic.plist 文件如下:
5、代码如下:
import UIKit class ViewController: UIViewController { ///---资源数组 var pic:NSArray!; ///--索引 var index:Int = 0; @IBOutlet weak var backBtn: UIButton! @IBOutlet weak var imageview: UIImageView! @IBOutlet weak var label: UILabel! @IBOutlet weak var topLabel: UILabel! @IBOutlet weak var nextBtn: UIButton! @IBAction func backBtnClick(sender: AnyObject) { ///---索引-- index--; if(index >= 0) { if (index == 0) { backBtn.enabled = false; nextBtn.enabled = true; } ///---1、从数组中获取当前索引对应的图片数据 let dic = pic[index]; ///---2、将数据设置到控件上 topLabel.text = "\(index+1)/\(pic.count)"; let i = dic["icon"]!; imageview.image = UIImage(named:"\(i!)"); let s = dic["label"]!; label.text = "\(s!)"; } else { index = 0; backBtn.enabled = false; } } override func viewDidLoad() { super.viewDidLoad() ///---获取.plist文件路径 let path:String! = (NSBundle.mainBundle()).pathForResource("pic.plist",ofType: nil); if (path.isEmpty) { print("路径为空,没找到文件"); } else { ///---读取文件,先获取数组(plist文件中,root级的类型为ARRAY) let array = NSArray(contentsOfFile:path!); // print("path=\(path!)"); // print("array.count = \(array!.count)"); pic = array; } ///-----设置启动图像 ///---1、从数组中获取当前索引对应的图片数据 let dic = pic[0]; ///---2、将数据设置到控件上 topLabel.text = "\(1)/\(pic.count)"; let i = dic["icon"]!; imageview.image = UIImage(named:"\(i!)"); let s = dic["label"]!; label.text = "\(s!)"; ///---禁用上一个按钮 backBtn.enabled = false; } ///---下一张 @IBAction func nextBtnClick(sender: UIButton) { ///---索引++ index++; ///---1、从数组中获取当前索引对应的图片数据 let dic = pic[index-1]; ///---2、将数据设置到控件上 topLabel.text = "\(index)/\(pic.count)"; let i = dic["icon"]!; imageview.image = UIImage(named:"\(i!)"); let s = dic["label"]!; label.text = "\(s!)"; ///---设置按钮是否可用 nextBtn.enabled = index != pic.count; backBtn.enabled = index == pic.count; // // if (index == pic.count) // { // nextBtn.enabled = false; // } // else // { // nextBtn.enabled = true; // } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }