导入项目

时间:2017-07-05 12:20:17

标签: ios xcode swift3

我有一个Scroll View教程,我自定义下载图像,而不是使用本地。

项目运作正常。当我将两个文件和两个相应的视图控制器导入我的主项目时,它崩溃了。我收到了错误

  

在解包可选值时意外地发现了nil。

我在整个项目中设置了断点,也设置了异常断点,但找不到问题。滚动视图显示第一张图像,然后是炸弹。

第一个VC:

import UIKit
import Alamofire
import AlamofireImage

class TutorialViewController: UIViewController {
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var pageControl: UIPageControl!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    var pages = [TutorialStepViewController]()
    var stringURLS: [String] = []
    var networkStatus = NetworkStatus.searching
    var constraintString = ""

    enum NetworkStatus {
        case searching
        case finishedSearching
    }

  override func viewDidLoad() {
    super.viewDidLoad()
    scrollView.isPagingEnabled = true
    getBrewerHistory(portalID: "7", galleryName: "Brewer History")

    activityIndicator.startAnimating()
    }

    func configureScrollView() {
        for i in 0..<stringURLS.count {
            let page = createAndAddTutorialStep(stringURLS[i], iconImageName: "", text: "")
            pages.append(page)
        }

        pageControl.numberOfPages = pages.count

        var views: [String: UIView] = ["view": view]

        for i in 0..<pages.count {
            views.updateValue(pages[i].view, forKey: "page" + String(i))
        }

    let metrics = ["edgeMargin": 10, "betweenMargin": 20]

    let verticalConstraints =
      NSLayoutConstraint.constraints(
        withVisualFormat: "V:|[page0(==view)]|", options: [], metrics: nil, views: views)
    NSLayoutConstraint.activate(verticalConstraints)

    for page in 0..<pages.count {
        switch page {
        case 0:
            constraintString += "H:|-edgeMargin-[page" + String(page) + "(==view)]-betweenMargin-"
        case pages.count-1:
            constraintString += "[page" + String(page) + "(==view)]-edgeMargin-|"
        default:
            constraintString += "[page" + String(page) + "(==view)]-betweenMargin-"
        }
    }
    let horizontalConstraints =
            NSLayoutConstraint.constraints(
                withVisualFormat: constraintString, options: [.alignAllTop, .alignAllBottom], metrics: metrics, views: views)
    NSLayoutConstraint.activate(horizontalConstraints)

  }

  fileprivate func createAndAddTutorialStep(_ stringURL: String, iconImageName: String, text: String) -> TutorialStepViewController {
    let tutorialStep = storyboard!.instantiateViewController(withIdentifier: "TutorialStepViewController") as! TutorialStepViewController
    tutorialStep.view.translatesAutoresizingMaskIntoConstraints = false

    let stringURL = stringURL
    // tutorialStep.backgroundImage = UIImage(named: backgroundImageName)
    let imageURL = URL(string: stringURL)
    tutorialStep.backgroundImageURL = imageURL


    tutorialStep.iconImage = UIImage(named: iconImageName)
    tutorialStep.text = text

    scrollView.addSubview(tutorialStep.view)

    addChildViewController(tutorialStep)
    tutorialStep.didMove(toParentViewController: self)

    return tutorialStep
  }
}

extension TutorialViewController: UIScrollViewDelegate {
  func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let pageWidth = scrollView.bounds.width
    let pageFraction = scrollView.contentOffset.x / pageWidth
    pageControl.currentPage = Int(round(pageFraction))
  }
}

//Mark: Networking Calls
extension TutorialViewController {

    func getBrewerHistory (portalID: String, galleryName: String) {
        networkStatus = .searching
        activityIndicator.isHidden = false
        activityIndicator.startAnimating()
        let apiURL = "http://www.smarttapp.com/DesktopModules/DnnSharp/DnnApiEndpoint/Api.ashx?method=GetBrewerHistory"
        let params = ["PortalID" : portalID,
                      "GalleryName" : galleryName
        ]

        Alamofire.request(
            apiURL,
            method: .get,
            parameters: params
            )

            .responseJSON { response in
                guard response.result.isSuccess else {
                    print("Error while fetching JSON: \(String(describing: response.result.error))")
                    return
                }

                guard let responseArray = response.result.value as? [NSDictionary] else { return }

                for dict in responseArray {
                    if let url = dict["fileName"] as? String {
                        let fullURL = "http://www.smarttapp.com" + url
                        self.stringURLS.append(fullURL)
                    }
                }

                DispatchQueue.main.async {
                    self.activityIndicator.stopAnimating()
                    self.activityIndicator.isHidden = true
                    self.networkStatus = .finishedSearching
                    self.configureScrollView()
                }
            }
        }
}

第二个VC:

import UIKit

class TutorialStepViewController: UIViewController {

  @IBOutlet fileprivate weak var backgroundImageView: UIImageView!
  @IBOutlet fileprivate weak var iconImageView: UIImageView!
  @IBOutlet fileprivate weak var textLabel: UILabel!

  var backgroundImage: UIImage?
    var backgroundImageURL: URL?
  var iconImage: UIImage?
  var text: String?

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    backgroundImageView.af_setImage(withURL: backgroundImageURL!)

    iconImageView.image = iconImage

    if let text = text {
      let font = UIFont(name: "HelveticaNeue-Light", size:20.0)!
      let paragraphStyle = NSMutableParagraphStyle()
      paragraphStyle.lineSpacing = 8
      paragraphStyle.alignment = .center

      textLabel.attributedText = NSAttributedString(string: text,
        attributes: [NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle])
    }
  }

}

0 个答案:

没有答案