女神画廊App (Swift1.2)

这个App的是storyboard+code的结合,主要的重点是:

1.segue传递图片值。

2.Autolayout中可以使用右下角三角符号使用Add Missing Constraints进行大概约束,使之适应不同大小屏幕。

3.上边栏Editor -> Embed In -> Navigation Controller或者Tab Bar Controller。

4.实现分享功能。 

5.注意storyboard设计Navigation Controller界面时关于segue的modal类型要改为push类型。

6.按住Command点击某个Controller控件代码可显示其方法类。

storyboard设计界面:

目录结构:

ViewController.swift文件代码:

 1 import UIKit
 2 class ViewController: UIViewController {
 3 
 4     @IBOutlet weak var beautyPicker: UIPickerView!
 5     let beauties = ["范冰冰","李冰冰","王菲","杨幂","周迅"]
 6     override func viewDidLoad() {
 7         super.viewDidLoad()
 8         beautyPicker.dataSource = self
 9         beautyPicker.delegate = self
10     }
11     override func didReceiveMemoryWarning() {
12         super.didReceiveMemoryWarning()
13         // Dispose of any resources that can be recreated.
14     }
15     
16     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
17         if segue.identifier == "GoToGallery"{
18             let index = beautyPicker.selectedRowInComponent(0)
19             var imageName:String?
20             switch index{
21             case 0:
22                 imageName = "fanbingbing"
23             case 1:
24                 imageName = "libingbing"
25             case 2:
26                 imageName = "wangfei"
27             case 3:
28                 imageName = "yangmi"
29             case 4:
30                 imageName = "zhouxun"
31             default:
32                 imageName = nil
33                 
34             }
35             var vc = segue.destinationViewController as! GalleryController
36             vc.imageName = imageName
37         }
38     }
39 }

View.swift (ViewController.swift的额外扩展文件):

 1 import UIKit
 2 
 3 extension ViewController:UIPickerViewDataSource{
 4     // returns the number of 'columns' to display.
 5     func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
 6         return 1
 7     }
 8     // returns the # of rows in each component..
 9     func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
10         return beauties.count
11     }
12 }
13 extension ViewController:UIPickerViewDelegate{
14     func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
15         return beauties[row]
16     }
17 }

GalleryController.swift文件代码:

 1 import UIKit
 2 import Social
 3 
 4 class GalleryController: UIViewController {
 5     var imageName:String?
 6     
 7     @IBOutlet weak var beautyImage: UIImageView!
 8     override func viewDidLoad() {
 9         super.viewDidLoad()
10         /*if (imageName != nil){
11             beautyImage.image = UIImage(named: imageName!)//修改前
12         }*/
13         
14         if let name = imageName {
15             beautyImage.image = UIImage(named: name)
16             switch name{
17             case "fanbingbing":
18                 navigationItem.title = "范冰冰"
19             case "libingbing":
20                 navigationItem.title = "李冰冰"
21             case "wangfei":
22                 navigationItem.title = "王菲"
23             case "yangmi":
24                 navigationItem.title = "杨幂"
25             case "zhouxun":
26                 navigationItem.title = "周迅"
27             default:
28                 navigationItem.title = "女神画廊"
29             }
30         }
31     }
32     override func didReceiveMemoryWarning() {
33         super.didReceiveMemoryWarning()
34         // Dispose of any resources that can be recreated.
35     }
36     
37     //分享按钮
38     @IBAction func shareTapped(sender: AnyObject) {
39         var controller: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTencentWeibo)
40         controller.setInitialText("一起来玩女神画廊app吧,就在Github上")
41         controller.addImage(beautyImage.image)
42         self.presentViewController(controller, animated: true, completion: nil)
43     }
44 }

KoreanViewController文件代码:

 1 import UIKit
 2 import Social
 3 class KoreanViewController: UIViewController {
 4     @IBOutlet weak var beautyImage: UIImageView!
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7     }
 8     override func didReceiveMemoryWarning() {
 9         super.didReceiveMemoryWarning()
10         // Dispose of any resources that can be recreated.
11     }
12     //facebook分享
13     @IBAction func facebooTapped(sender: AnyObject) {
14         var controller: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
15         controller.setInitialText("一起来玩女神画廊app吧,就在Github上")
16         controller.addImage(beautyImage.image)
17         self.presentViewController(controller, animated: true, completion: nil)
18     }
19     //twitter分享
20     @IBAction func twitterTapped(sender: AnyObject) {
21         var controller: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
22         controller.setInitialText("一起来玩女神画廊app吧,就在Github上")
23         controller.addImage(beautyImage.image)
24         self.presentViewController(controller, animated: true, completion: nil)
25     }
26     //微博分享
27     @IBAction func WeiboTapped(sender: AnyObject) {
28         var controller: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeSinaWeibo)
29         controller.setInitialText("一起来玩女神画廊app吧,就在Github上")
30         controller.addImage(beautyImage.image)
31         self.presentViewController(controller, animated: true, completion: nil)
32     }
33 }

运行结果:

github地址:https://github.com/AbelSu131/BeautyGallery

原文地址:https://www.cnblogs.com/abelsu/p/4775756.html