在多视图控制器中使用相同的变量

时间:2017-07-09 16:28:25

标签: ios swift uicolor

我有两个颜色变量:

var blue = UIColor(red:0.08, green:0.49, blue:0.49, alpha:1.0)
var red = UIColor(red:0.61, green:0.12, blue:0.20, alpha:1.0)

我已在EntryViewController中声明了这些变量,但是,我还需要在我的其他ViewControllers中使用它们。我想知道是否有可能在多个ViewControllers中使用相同的变量?我已经知道使用segue传递数据了,但是我想知道是否有一种方法可以在自己的类中声明颜色并使用此类继承我的ViewControllers。谢谢。

2 个答案:

答案 0 :(得分:2)

此处的一种方法是在public class ConvertWord { public static void ConvertToPDF(String docPath, String pdfPath) { try { InputStream doc = new FileInputStream(new File(docPath)); XWPFDocument document = new PdfOptions options = PdfOptions.create(); options.fontEncoding("UTF-8") OutputStream out = new FileOutputStream(new File(pdfPath)); PdfConverter.getInstance().convert(document,out,options); } catch (FileNotFoundException ex) { } catch (IOException ex) { } } public static void main(String[] args) { ConvertWord cwoWord=new ConvertWord(); cwoWord.ConvertToPDF("D://" + "usc.docx","D://test12.pdf"); } } 上创建extension。然后,您可以将它们设置为模块的全局。

UIColor

答案 1 :(得分:1)

你可以制作这些全局变量,但这是不赞成的,因为它会污染全局名称空间。相反,您可以使这些静态成员成为新类型。类或结构可以工作,但通常会使用enum来确保不能生成类型的实例。

enum Colors {
    static let blue = UIColor(red: 0.08, green: 0.49, blue: 0.49)
    static let red = UIColor(red: 0.61, green: 0.12, blue: 0.20)
}