I have a table view with buttons on programmatically generated cells to create a menu for my app, but I want to make the button on each cell go to a different view controller. This is the code to generate the cells.
import UIKit
import SwiftSoup
class ArticleListScreen: UIViewController {
var index: Int = 0
var titles: [String] = []
var images: [UIImage] = [ imageLiteral(resourceName: "outside-page.png"), imageLiteral(resourceName: "outside-page.png"), imageLiteral(resourceName: "outside-page.png"), imageLiteral(resourceName: "outside-page.png"), imageLiteral(resourceName: "outside-page.png")]
var articles: [Article] = []
var regular: [Regular] = []
var loaded = true
override func viewDidLoad() {
super.viewDidLoad()
titles.append(parseHTML(id: "article"))
titles.append(parseHTML(id: "article"))
titles.append(parseHTML(id: "article"))
titles.append(parseHTML(id: "article"))
titles.append(parseHTML(id: "article"))
articles = createArticleArray()
regular = createRegularArray()
}
func parseHTML(id: String) -> String{
do{
let html = "<html><head><title>First parse</title><p1 id='article'>helloworld</p1></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>"
let doc: Document = try SwiftSoup.parse(html)
return try (doc.getElementById(id)?.text())!
}catch{
print("")
}
return "None"
}
func createRegularArray() -> [Regular] {
var tempRegular: [Regular] = []
let regular1 = Regular(image: imageLiteral(resourceName: "sps.jpg"), title: "NFL lawyer who claimed Super Bowl is 'rigged' is found dead")
tempRegular.append(regular1)
return tempRegular
}
func createArticleArray() -> [Article] {
var tempArticles: [Article] = []
for (image, title) in zip(images, titles) {
tempArticles.append(Article(image:image, title: title))
}
return tempArticles
}
}
extension ArticleListScreen: UITableViewDataSource, UITabBarDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return articles.count+regular.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.row == 0){
tableView.rowHeight = 270
tableView.separatorStyle = .singleLine
let article = regular[0]
let cell = tableView.dequeueReusableCell(withIdentifier: "RegularCell") as! RegularCell
cell.setArticle(article: article)
return cell
}
else if (indexPath.row == 1){
tableView.rowHeight = 100
//tableView.separatorStyle = .none
let article = articles[indexPath.row-1]
let cell = tableView.dequeueReusableCell(withIdentifier: "ArticleCell") as! ArticleCell
cell.setArticle(article: article)
return cell
}
else{
tableView.rowHeight = 90
//tableView.separatorStyle = .none
let article = articles[indexPath.row-1]
let cell = tableView.dequeueReusableCell(withIdentifier: "ArticleCell") as! ArticleCell
cell.setArticle(article: article)
return cell
}
}
}
In the cell class, I have code to present a new view controller of the button linked to it is pressed:
import UIKit
class MenuCell: UITableViewCell {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var picture: UIImageView!
@IBOutlet weak var button: UIButton!
var index: Int = 0
let views = [PreviousIssue(), Bookmarks(), Settings(), PreviousIssue(), About()]
func setAttributes(item: menuItem) {
label.text = item.labelText
picture.image = item.picture
index = item.index
}
func survey(){
button.addTarget(self, action: #selector(buttonTapped),
for: .touchUpInside)
}
@objc func buttonTapped() {
if (index != 3){
//MenuViewController().present(views[index], animated: true, completion: nil)
views[index].view.transform = CGAffineTransform(translationX: -500, y: 0)
MenuViewController().view.superview?.addSubview(views[index].view)
UIView.animate(withDuration: 0.5, delay:0, options: .curveEaseInOut, animations: {
self.views[self.index].view.transform = CGAffineTransform.identity
}, completion: {
success in MenuViewController().present(self.views[self.index], animated: false, completion: nil)
})
}
}
/*
// 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.destination.
// Pass the selected object to the new view controller.
}
*/
}
When I navigate to the menu and press the different cells, I get feedback that it is trying to present the corresponding view controller, but it never presents and I get this warning:
2019-03-12 14:26:11.427721-0500 Pelican[1918:364019] Warning: Attempt to present <Pelican.PreviousIssue: 0x10402a6f0> on <Pelican.MenuViewController: 0x10a402b70> whose view is not in the window hierarchy!
2019-03-12 14:26:14.217328-0500 Pelican[1918:364019] Warning: Attempt to present <Pelican.Bookmarks: 0x104133b60> on <Pelican.MenuViewController: 0x10a4030c0> whose view is not in the window hierarchy!
2019-03-12 14:26:16.125883-0500 Pelican[1918:364019] Warning: Attempt to present <Pelican.Settings: 0x104139b50> on <Pelican.MenuViewController: 0x104501410> whose view is not in the window hierarchy!
2019-03-12 14:26:17.061922-0500 Pelican[1918:364019] Warning: Attempt to present <Pelican
Should I be doing something different? Or is there a way to solve this?