Swift 3ではUIImageView(named: string_of_image)
が割り当てられていません。他の同様の質問は画像を格納するためにimage.xcassets
を使用していますが、私はAssets.xcassets
を使用しています。たぶんそれはいくつかのエラーを引き起こしたが、私はそれが主要な問題ではないとは思わない。ここに私のコードは次のとおりです。UIImage(名前:文字列)は割り当てられていません
contentViewController.swift
import UIKit
class ContentViewController: UIViewController {
@IBOutlet weak var guideImage: UIImageView!
var pageIndex:Int!
var imageFile: String = "" {
didSet {
if let imageView = guideImage {
imageView.image = UIImage(named: imageFile)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
print(imageFile)
if let image = UIImage(named: imageFile) {
guideImage.image = image
}
else {
print("image not assigned")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
GuideViewController.swift
import UIKit
class GuideViewController: UIViewController, UIPageViewControllerDataSource {
var PageViewController: UIPageViewController!
var PageImage: Array<String>!
override func viewDidLoad() {
super.viewDidLoad()
self.PageImage = ["페이지1", "페이지2", "페이지3"]
let pageController = self.storyboard!.instantiateViewController(withIdentifier: 일반사용자가이드컨트롤러) as! UIPageViewController
pageController.dataSource = self
if PageImage.count > 0 {
let firstController = viewControllerAtIndex(index: 0)
let startingViewControllers = [firstController]
pageController.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
self.PageViewController = pageController
addChildViewController(self.PageViewController!)
self.view.addSubview(self.PageViewController!.view)
self.PageViewController!.didMove(toParentViewController:self)
let appearance = UIPageControl.appearance()
appearance.pageIndicatorTintColor = UIColor.gray
appearance.currentPageIndicatorTintColor = UIColor.white
appearance.backgroundColor = UIColor.darkGray
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private func viewControllerAtIndex (index : Int) -> ContentViewController {
let vc : ContentViewController = self.storyboard?.instantiateViewController(withIdentifier: 페이지컨텐츠뷰) as! ContentViewController
vc.pageIndex = index
vc.imageFile = self.PageImage[index] as String
return vc
}
/**
* 이전 ViewPageController 구성
*/
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if(index == 0 || index == NSNotFound) {
return nil
}
index = index-1
vc.guideImage.image = UIImage(named: self.PageImage[index])
return self.viewControllerAtIndex(index: index)
}
/**
* 이후 ViewPageController 구성
*/
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if(index == NSNotFound) {
return nil
}
index += 1
if (index == self.PageImage.count) {
return nil
}
vc.guideImage.image = UIImage(named: self.PageImage[index])
return self.viewControllerAtIndex(index:index)
}
/**
* 인디케이터의 총 갯수
*/
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return self.PageImage.count
}
/**
* 인디케이터의 시작 포지션
*/
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
詳細な情報が必要な場合、私に知らせてください。 UIImageで
(解決) (命名:StringOfFile)、StringOfFileは英語でなければなりません...それはあなたのファイルの拡張子を追加し、このような
特定のコードセグメントを追加してください。 –
あなたはプロジェクトにすでにある既定の資産を使用していますか、またはカスタム資産を作成していますか?両方のイメージ名がアセットとコードで完全に同じであることを確認してください。 – Lion
@Lion私はデフォルト資産を使用していますが、画像の名前はまったく同じです...コピーして貼り付けました! –