私はAndroidのバックグラウンドからiOSの初心者です。私はビューコントローラからデータソース&デリゲートを分離しようとしています。どのように行うかについてのチュートリアルをいくつか見つけましたが、クリックされたアイテムを別のビューコントローラに送信する方法を理解していました。コードは次のとおりです。データソースとデリゲートがView Controllerと別の場合に、Table Viewのクリックしたアイテムを他のView Controllerに送信する方法
class PictureTableViewController: UIViewController {
@IBOutlet weak var pictureTableView: UITableView!
private let picsDataSource: PicturesDataSource
required init?(coder aDecoder: NSCoder) {
self.picsDataSource = PicturesDataSource()
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
pictureTableView.dataSource = picsDataSource
pictureTableView.reloadData()
pictureTableView.delegate = picsDataSource
}
}
class PicturesDataSource: NSObject, UITableViewDataSource, UITableViewDelegate{
private var pictureModels = [PictureModel]()
override init(){
let picModelsDataController = PictureModelsDataController()
pictureModels = picModelsDataController.pictureModels
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pictureModels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PictureCell.self)) as! PictureCell
let picModel = pictureModels[indexPath.row]
cell.pictureName = picModel.pictureName
cell.imageItem = picModel.imageItem
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//1 - try loading the "Detail" view controller and typecasting it to be DetailViewController
if let detailViewController = storyboard.instantiateViewController(withIdentifier: "PictureDetailView") as? PictureDetailViewController {
//2 - success! set its selecteImage property
detailViewController.selectedImgName = pictureModels[indexPath.row].pictureName
//3 - now push it onto the navigation controller
navigationController?.pushViewController(detailViewController, animated: true)
}
}
}
です。 「ストーリーボード」&「navigationController」以来はPicturesDataSourceクラスでは使用できません、どのように私はDetailsViewControllerにクリックされたアイテム(画像名)送信することができます
あり、データソースとデリゲートを分離についてStackOverflowの答えがありますが、私の問題を解決しませんでした。
使用
:Xcodeの8.3ベータ版は6
のように見えるのだろうか?この 'storyboardを利用してstoryBoardオブジェクトを作成することができます:UIStoryboard = UIStoryboard(名前:" Main "、バンドル:なし)' –
@Aditya、navigationControllerはどうですか? –
私の答えはあなたのために働くことを望む –