0

我有一个名为的文件UIElements.swift,其中包含我想在整个应用程序中使用的一些扩展名。

到目前为止,他们工作得很好。在我创建了一个新的 viewController 之前,我无法让它们中的任何一个在那个或我制作的任何其他 viewController 中工作。它们只在我的第一个 viewController 中工作?

这是扩展文件的代码:

import UIKit

extension UIImage {
    //create image from UIColor, to use for buttons
    class func imageWithColor(color:UIColor?) -> UIImage! {
        let rect = CGRectMake(0.0, 0.0, 1.0, 1.0);
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        let context = UIGraphicsGetCurrentContext();
        if let color = color {
            color.setFill()
        }
        else {
            UIColor.whiteColor().setFill()
        }
        CGContextFillRect(context, rect);
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
}

显然我在文件中有更多扩展名,但这是我在发现它不起作用时首先尝试调用的函数。

这是不起作用的代码,在一个名为的文件中Login_VC.swift

import UIKit

class LoginVC: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        var signInButton = UIButton()
        signInButton.setTranslatesAutoresizingMaskIntoConstraints(false)
        signInButton.setTitle("SIGN IN", forState: .Normal)
        signInButton.titleLabel!.font =  UIFont(name: "BebasNeue-Bold", size: 50)
        signInButton.setBackgroundImage(UIImage.imageWithColor(UIColor.colorWithHex("#12D99E")), forState: .Normal)
        self.view.addSubview(signInButton)

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

上面的代码返回以下错误:

'UIImage.Type' does not have a member named 'imageWithColor'

这是我调用的第一个视图控制器中的一个类似按钮LandingPageVC.swift(此代码工作正常)

import UIKit

class LandingPageVC: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()


    var signUpButton: UIButton = UIButton()
    signUpButton.setTranslatesAutoresizingMaskIntoConstraints(false)
    signUpButton.setTitle("Sign Up Today", forState: .Normal)
    signUpButton.titleLabel!.font =  UIFont(name: "BebasNeue-Bold", size: 50)
    signUpButton.setBackgroundImage(UIImage.imageWithColor(UIColor.colorWithHex("#12D99E")), forState: .Normal)
    signUpButton.setBackgroundImage(UIImage.imageWithColor(UIColor.colorWithHex("#0DB07F")), forState: .Highlighted)
    signUpButton.layer.shadowColor = formulaShadowColor.CGColor
    signUpButton.layer.shadowOffset = CGSizeMake(3, 3)
    signUpButton.layer.shadowRadius = 0
    signUpButton.layer.shadowOpacity = 1.0
    signUpButton.addTarget(self, action: "signUp:", forControlEvents: .TouchUpInside)
    signUpButton.addTarget(self, action: "signUpHighlighted:", forControlEvents: .TouchDown)
    signUpButton.addTarget(self, action: "signUpReset:", forControlEvents: .TouchDragExit)
    self.view.addSubview(signUpButton)

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

}

我在任何一个文件中都看不到我所做的任何不同的事情。

然而,当我开始写作时UIImage.i-> 它imageWithColor在第一个视图控制器中提出了建议,但它没有出现在任何其他文件中。

我没有做任何事情来在这两个文件中导入这个类。由于它只是 的扩展UIImage,因此应使用UIKit.

我还尝试添加public到课程中,但没有做任何事情。

谁能解释为什么此功能在一个文件中有效,而在另一个文件中无效?

4

2 回答 2

1

您是否尝试过清理您的项目(Command-Option-Shift-K)?清理项目会清空 Xcode 缓存。更好的是退出您的项目并~/Library/Developer/Xcode/DerivedData手动清理。确保删除此文件夹中的所有内容,因为该错误可能会缓存在任何文件中。

于 2015-03-03T12:13:03.007 回答
0

I sent a bug-report to apple about this

Since this question has been untouched for a while not. I ended up creating a new project and copying my files over. It's working now. Still have no clue as to why it failed to work. The files are the same, same structure, same names, same targets.

于 2015-03-10T15:28:49.640 回答