美文网首页
使用R.swift来管理资源

使用R.swift来管理资源

作者: ISwiftUI | 来源:发表于2017-12-09 19:41 被阅读16次

    基础用法

    常见的操作:
    1 => let icon = UIImage(named: "settings-icon")
    2 => let font = UIFont(name: "San Francisco", size: 42)
    3 => performSegueWithIdentifier("openSettings")

    替代方案:
    1 => let icon = R.image.settingsIcon
    2 => let font = R.font.sanFrancisco(size:42)
    3 => performSegueWithIdentifier(R.segue.openSettings)

    1.UIImage

    使用原生swift

    let settingsIcon = UIImage(named: "settings-icon")
    let gradientBackground = UIImage(named: "gradient.jpg")
    

    R.swift

    let settingsIcon = R.image.settingsIcon
    let gradientBackground = R.image.gradientJpg
    

    2.Storyboards

    使用原生swift

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let initialTabBarController = storyboard.instantiateInitialViewController() as? UITabBarController
    let settingsController = self.instantiateViewControllerWithIdentifier("settingsController") as? SettingsController
    

    R.swift

    let storyboard = R.storyboard.main.instance
    let initialTabBarController = R.storyboard.main.initialViewController
    let settingsController = R.storyboard.main.settingsController
    
    //通过这个代码来校验运行时storyboard的图片是否都能被加载
    // 只在debug模式下有效,会通过断言来提示
    R.storyboard.main.validateImages()
    
    //在运行时校验所有的viewController能够被正常加载
    mode.R.storyboard.main.validateViewControllers()
    
    

    3.Segues

    使用原生swift

    performSegue(withIdentifier: "openSettings", sender: self)
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let settingsController = segue.destination as? SettingsController,
           let segue = segue as? CustomSettingsSegue, segue.identifier == "openSettings" {
          segue.animationType = .LockAnimation
          settingsController.lockSettings = true
        }
      }
    

    R.swift

    performSegue(withIdentifier: R.segue.overviewController.openSettings, sender: self)
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if let typedInfo = R.segue.overviewController.openSettings(segue: segue) {
        typedInfo.segue.animationType = .LockAnimation
        typedInfo.destinationViewController.lockSettings = true
      }
    

    4.Nibs

    使用原生swift

    let nameOfNib = "CustomView"
    let customViewNib = UINib(nibName: "CustomView", bundle: nil)
    let rootViews = customViewNib.instantiateWithOwner(nil, options: nil)
    let customView = rootViews[0] as? CustomView
    let viewControllerWithNib = CustomViewController(nibName: "CustomView", bundle: nil)
    

    R.swift

    let nameOfNib = R.nib.customView.name
    let customViewNib = R.nib.customView
    let rootViews = R.nib.customView.instantiateWithOwner(nil, options: nil)
    let customView = R.nib.customView.firstView(nil, options: nil)
    let viewControllerWithNib = CustomViewController(nib: R.nib.customView)
    

    5.Reusable cells

    使用原生swift

    let textCellNib = UINib(nibName: "TextCell", bundle: nil)
    tableView.registerNib(textCellNib, forCellReuseIdentifier: "TextCellIdentifier")
    

    R.swift

    tableView.registerNib(R.nib.textCell)
    
    //cellForRowAtIndexPath中获取cell
    let textCell = tableView.dequeueReusableCellWithIdentifier(R.nib.textCell.reuseIdentifier, forIndexPath: indexPath)
    

    6.Custom fonts

    使用原生swift

    let lightFontTitle = UIFont(name: "Acme-Light", size: 22)
    

    R.swift

    let lightFontTitle = R.font.acmeLight(size: 22)
    

    7.Resource files

    使用原生swift

    let jsonURL = NSBundle.mainBundle().URLForResource("seed-data", withExtension: "son")
    

    R.swift

    let jsonURL = R.file.seedDataJson
    

    8.Localized Strings

    使用原生swift

    let welcomeMessage = NSLocalizedString("welcome.message", comment: "")
    let settingsTitle = NSLocalizedString("title", tableName: "Settings", comment: "")
    let welcomeName = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Alice")
    let progress = String(format: NSLocalizedString("copy.progress", comment: ""), locale: NSLocale.current, 4, 23)
    

    R.swift

    let welcomeMessage = R.string.localizable.welcomeMessage()
    let settingsTitle = R.string.settings.title()
    let welcomeName = R.string.localizable.welcomeWithName("Alice")
    let progress = R.string.localizable.copyProgress(completed: 4, total: 23)
    

    相关文章

      网友评论

          本文标题:使用R.swift来管理资源

          本文链接:https://www.haomeiwen.com/subject/psepixtx.html