Swift扩展文件:ViewContoller进入扩展文件功能而不调用

时间:2017-06-27 14:28:05

标签: ios swift xcode

有人可以帮我理解为什么会发生这个错误。

当我对某个视图控制器执行segue时,即使视图控制器文件没有对所述函数进行任何调用,程序也会调用扩展文件中的函数。

我在每个文件中都有print语句,所以我可以看到程序进入ViewControllerUniversity.swift然后进入loadImageUsingCacheWithUrlString(urlString:String)。

以下是我遇到问题的两个文件:

//
//  ViewControllerUniversity.swift
//  
//


import UIKit

class ViewControllerUniversity: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {


//var to hold university
var userUniversity = ""

override func viewDidLoad() {
    super.viewDidLoad()
    print("Gone into SELECT UNIVERSITY")
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/****
* CONTINUE
* continue with sign up
****/

@IBOutlet var selectUniversityWarningLabel: UILabel!


@IBAction func contButton(_ sender: UIButton) {

    if userUniversity != ""
    {
        if userUniversity != "-"
        {
            //set user university
            thisUser.setUniversity(uni: userUniversity)


            //go to signup
            self.performSegue(withIdentifier: "fromSignIn1ToSignIn2", sender: self)

        } else
        {
            print("Select university")
            selectUniversityWarningLabel.text = "Please select a university from list above. "
        }
    }else
    {
        print("Select university")
        selectUniversityWarningLabel.text = "Please select a university from list above."
    }

}



@IBOutlet var universityPickerView: UIPickerView!

/****
 * PICKER VIEW
 * Shows the universites available
 ****/


@IBOutlet var uniPickerView: UIPickerView!


let universityList = ["-", "University"]

//number of rows
func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}


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

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

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    userUniversity = universityList[row]
}

}

扩展文件

//
//  extensions.swift
//  


import UIKit

//stores images
let imageCache = NSCache<AnyObject, AnyObject>()

extension UIImageView {


func loadImageUsingCacheWithUrlString(urlString: String){
    print("Gone into LOADIMAGEUSINGCACHE")
    self.image = nil

    //check cache for image
    if let cachedImage = imageCache.object(forKey: urlString as AnyObject){
        self.image = cachedImage as! UIImage
        return
    }

    //otherwise download the image
    let url = URL(string: urlString)
    URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in

        if error != nil {
            print(error)
            return
        }

        //update user profile pciture to cell
        DispatchQueue.main.async {

            if let downloadedImage = UIImage(data: data!){
                imageCache.setObject(downloadedImage, forKey: urlString as AnyObject)
                self.image = downloadedImage
            }


        }



    }).resume()
}

}

1 个答案:

答案 0 :(得分:1)

感谢您的建议。

检查在ViewControllerUniversity之前加载的视图控制器上收到的操作时,我发现我已将两个提交按钮连接在一起。我认为在一个视图控制器上按下提交将加载ViewControllerUniversity,然后立即调用该按钮。