返回指定变量中的空值,Swift 3.0

时间:2016-12-19 07:36:24

标签: ios swift class oop null

我目前正尝试在swift上进行基于OOP的编程,并希望用2个或更多的pickviewer创建一个视图,但我想为每个选择查看器创建类,当我这样做时我遇到麻烦,导致视图中的另一个变量由于使用OOP类型,控制器类的值已更改为nil,这里是视图控制器程序的代码

 import UIKit

 class ViewController: UIViewController{

 @IBOutlet weak var imageView: UIImageView!
 @IBOutlet weak var imageView2: UIImageView!
 @IBOutlet weak var ListPilihanMask: UIPickerView!
 @IBOutlet weak var ListPilihanBG: UIPickerView!


 var InstancePickerViewBG = PickerViewBG()
 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var image = UIImage(named: "natural_swampash_gloss")
    var maskingImage = UIImage(named: "modernbodymask")
    var maskingImage2 = UIImage(named: "mask6")
    imageView.image = maskImage(image: image!, mask: maskingImage!)
    imageView2.image = maskImage(image: image!, mask: maskingImage2!)

    self.ListPilihanBG.dataSource=InstancePickerViewBG
    self.ListPilihanBG.delegate=InstancePickerViewBG
    //self.ListPilihanMask.dataSource=self
    //self.ListPilihanMask.delegate=self
 }

 func maskImage(image:UIImage, mask:(UIImage))->UIImage{

    guard let imageReference = image.cgImage else {
        return UIImage()
    }
    guard let maskReference = mask.cgImage else {
        return UIImage()
    }

    let imageMask = CGImage(maskWidth: maskReference.width,
                            height: maskReference.height,
                            bitsPerComponent: maskReference.bitsPerComponent,
                            bitsPerPixel: maskReference.bitsPerPixel,
                            bytesPerRow: maskReference.bytesPerRow,
                            provider: maskReference.dataProvider!, decode: nil, shouldInterpolate: true)

    let maskedReference = imageReference.masking(imageMask!)
    let maskedImage = UIImage(cgImage:maskedReference!)

    return maskedImage
}

 //case 0:
 //chooseMask(Url1: "modernbodymask", Url2 : "mask6")
 //case 1:
 //chooseMask(Url1: "mask4",Url2: "mask2")

}'

以下是pickviewer类的代码

import Foundation
import UIKit
class PickerViewBG:NSObject, UIPickerViewDataSource,         UIPickerViewDelegate
{
   let image = UIImage(named: "natural_swampash_gloss")
   let maskingImage = UIImage(named: "modernbodymask")
   let maskingImage2 = UIImage(named: "mask6")

   var ItemBG:[String]=["Putih","Merah","Hijau","Biru"]

   func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
   }

   func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return ItemBG.count;
   }

   func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return ItemBG[row]
   }

   func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
   {
    let InstanceViewControll = ViewController()
    switch row {
    case 0:
        InstanceViewControll.view.backgroundColor = UIColor.white;
    case 1:
        InstanceViewControll.view.backgroundColor = UIColor.red;
    case 2:
        InstanceViewControll.view.backgroundColor = UIColor.green;
    case 3:
        InstanceViewControll.view.backgroundColor = UIColor.blue;
    default:
        InstanceViewControll.view.backgroundColor = UIColor.white;
    }
   }

}

对不起,如果我在这篇文章中有这么多拼写错误,我仍然是英国学院的一名学生:)

0 个答案:

没有答案