Problema con segue

rspadar

Nuovo Utente
23 Nov 2016
4
0
1
50
Ciao a tutti,

sto cercando di realizzare una piccola app che legge un feed rss ed al click sull'articolo mi apre la pagina in una webview.

Accade che quando dall'elenco degli articoli clicco su un elemento mi si aprono due pagine consecutive, la prima mi carica la pagina web e immediatamente una seconda che resta in caricamento.
Tornando indietro visualizzo la pagina web correttamente caricata, tornando ancora indietro vado sull'elenco degli articoli.
Sto praticamente impazzendo, non riesco a capire il perché, ho controllato il codice a fondo pensando di aver lasciato quel che chiamata doppia, ma niente.
Qualcuno riesce a darmi qualche indicazione?


Raffaele
 

rspadar

Nuovo Utente
23 Nov 2016
4
0
1
50
DI seguito ecco il codice delle due classi che uso:

Questa gestisce l'elenco caricato dal feed:
ListViewController.swift
//
// ListViewController.swift
// SwiftRSSReader
//
// Created by Prashant on 14/09/15.
// Copyright (c) 2015 PrashantKumar Mangukiya. All rights reserved.
//
import UIKit
class ListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, XMLParserDelegate {

// outlet - table view
@IBOutlet var myTableView: UITableView!

// outet - activity indicator
@IBOutlet var spinner: UIActivityIndicatorView!


// xml parser
var myParser: XMLParser = XMLParser()

// rss records
var rssRecordList : [RssRecord] = [RssRecord]()
var rssRecord : RssRecord?
var isTagFound = [ "item": false , "title":false, "pubDate": false ,"link":false]




// MARK - View functions

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

// set tableview delegate
self.myTableView.dataSource = self
self.myTableView.delegate = self
}
override func viewDidAppear(_ animated: Bool) {

// load Rss data and parse
if self.rssRecordList.isEmpty {
self.loadRSSData()
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}



// MARK: - Table view dataSource and Delegate

// return number of section within a table
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

// return row height
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}

// return how may records in a table
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.rssRecordList.count
}

// return cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

// collect reusable cell
let cell = tableView.dequeueReusableCell(withIdentifier: "rssCell", for: indexPath)

// find record for current cell
let thisRecord : RssRecord = self.rssRecordList[indexPath.row]

// set value for main title and detail tect
cell.textLabel?.text = thisRecord.title
cell.detailTextLabel?.text = thisRecord.pubDate

// return cell
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "segueShowDetails", sender: self)
}



// MARK: - NSXML Parse delegate function
// start parsing document
func parserDidStartDocument(_ parser: XMLParser) {
// start parsing
}

// element start detected
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {

if elementName == "item" {
self.isTagFound["item"] = true
self.rssRecord = RssRecord()

}else if elementName == "title" {
self.isTagFound["title"] = true

}else if elementName == "link" {
self.isTagFound["link"] = true

}else if elementName == "pubDate" {
self.isTagFound["pubDate"] = true
}

}

// characters received for some element

func parser(_ parser: XMLParser, foundCharacters string: String) {
if isTagFound["title"] == true {
self.rssRecord?.title += string

}else if isTagFound["link"] == true {
self.rssRecord?.link += string

}else if isTagFound["pubDate"] == true {
self.rssRecord?.pubDate += string
}

}

// element end detected
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {

if elementName == "item" {
self.isTagFound["item"] = false
self.rssRecordList.append(self.rssRecord!)

}else if elementName == "title" {
self.isTagFound["title"] = false

}else if elementName == "link" {
self.isTagFound["link"] = false

}else if elementName == "pubDate" {
self.isTagFound["pubDate"] = false
}
}

// end parsing document
func parserDidEndDocument(_ parser: XMLParser) {

//reload table view
self.myTableView.reloadData()

// stop spinner
self.spinner.stopAnimating()
}

// if any error detected while parsing.
func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {

// stop animation
self.spinner.stopAnimating()

// show error message
self.showAlertMessage(alertTitle: "Error", alertMessage: "Error while parsing xml.")
}




// MARK: - Utility functions

// load rss and parse it
fileprivate func loadRSSData(){

if let rssURL = URL(string: RSS_FEED_URL) {

// start spinner
self.spinner.startAnimating()

// fetch rss content from url
self.myParser = XMLParser(contentsOf: rssURL)!
// set parser delegate
self.myParser.delegate = self
self.myParser.shouldResolveExternalEntities = false
// start parsing
self.myParser.parse()
}

}

// show alert with ok button
fileprivate func showAlertMessage(alertTitle: String, alertMessage: String ) -> Void {

// create alert controller
let alertCtrl = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) as UIAlertController

// create action
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler:
{ (action: UIAlertAction) -> Void in
// you can add code here if needed
})

// add ok action
alertCtrl.addAction(okAction)

// present alert
self.present(alertCtrl, animated: true, completion: { (void) -> Void in
// you can add code here if needed
})
}




// 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.

if segue.identifier == "segueShowDetails" {

// find index path for selected row
let selectedIndexPath : [IndexPath] = self.myTableView.indexPathsForSelectedRows!

// deselect the selected row
self.myTableView.deselectRow(at: selectedIndexPath[0], animated: true)

// create destination view controller
let destVc = segue.destination as! DetailsViewController

// set title for next screen
destVc.navigationItem.title = self.rssRecordList[selectedIndexPath[0].row].title

// set link value for destination view controller
destVc.link = self.rssRecordList[selectedIndexPath[0].row].link

}

}

}
 

rspadar

Nuovo Utente
23 Nov 2016
4
0
1
50
Qua il codice della classe che permette il caricamento dell item selezionato:

DetailsViewController.swift
//
// DetailsViewController.swift
// SwiftRSSReader
//
// Created by Prashant on 14/09/15.
// Copyright (c) 2015 PrashantKumar Mangukiya. All rights reserved.
//
import UIKit
class DetailsViewController: UIViewController, UIWebViewDelegate {

// outlet - activity indicator
@IBOutlet var spinner: UIActivityIndicatorView!

// outlet - web view
@IBOutlet var myWebView: UIWebView!

// refresh button
@IBAction func refreshButtonClicked(_ sender: UIBarButtonItem) {
self.refreshWebView()
}


// link to browse (this value set by parent controller)
var link: String?




// MARK: - view functions

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

// set webview delegate
self.myWebView.delegate = self
// start spinner
self.spinner.startAnimating()

// load url in webview
if let fetchURL = URL(string: self.link! ) {
let urlRequest = URLRequest(url: fetchURL)
self.myWebView.loadRequest(urlRequest)
}

}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}



// MARK: - Webview delegate

func webViewDidFinishLoad(_ webView: UIWebView) {

// stop spinner
self.spinner.stopAnimating()
}

func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {

// stop spinner
self.spinner.stopAnimating()

// show error message
self.showAlertMessage(alertTitle: "Error", alertMessage: "Error while loading url.")
}




// MARK: - Utility function

// refresh webview
func refreshWebView(){

// start spinner
self.spinner.startAnimating()
// reload webview
self.myWebView.reload()

}

// show alert with ok button
fileprivate func showAlertMessage(alertTitle: String, alertMessage: String ) -> Void {

// create alert controller
let alertCtrl = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) as UIAlertController

// create action
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler:
{ (action: UIAlertAction) -> Void in
// you can add code here if needed
})

// add ok action
alertCtrl.addAction(okAction)

// present alert
self.present(alertCtrl, animated: true, completion: { (void) -> Void in
// you can add code here if needed
})
}

}
 
Discussioni simili
Autore Titolo Forum Risposte Data
O problema con dvr dahua xvr5116 IP Cam e Videosorveglianza 0
G Problema con Xampp Web Server 1
andrea barletta Problema con miniature comandi Photoshop 0
I problema con alice Posta Elettronica 0
N Problema con position absolute e overflow HTML e CSS 4
L Problema con inner join PHP 11
K [php] Problema con inner join PHP 4
K [PHP] Problema con variabili concatenate. PHP 1
O problema con query PHP 4
I problema con 2 account Posta Elettronica 1
L problema collegamento file css con html HTML e CSS 1
E Problema accesso a file con app sviluppata con MIT APP INVENTOR 2 Sviluppo app per Android 0
M Problema con Try Catch PHP 0
Sergio Unia Problema con gli eventi del mouse su una data table: Javascript 2
T PROBLEMA CON SESSIONI PHP 3
T ALTRO PROBLEMA CON ARRAY PHP PHP 1
R problema con else PHP 0
T PROBLEMA CON ARRAY PHP 8
L problema con query select PHP 2
R Problema query con ricerca id numerico PHP 2
F Problema con risposta PHP 0
S problema con recupero dati tabella mysql PHP 2
Z Problema con il mio tp-l i nk Reti LAN e Wireless 1
L Problema RAM con Tomcat 8 Apache 0
napuleone problema con sort e asort PHP 4
Z Problema con INT MySQL PHP 1
Z Problema database MySQL con XAMPP PHP 0
M Problema con controllo form in real time jQuery 6
Z Problema di sincronizzazione PAYPAL con PHP PHP 1
G Problema con Get page PHP 4
P Problema con require once PHP 6
P Problema con i package Java 1
A Problema login con Safari PHP 14
F INDESIGN: problema esportazione esecutivo per la stampa con foto B/N Webdesign e Grafica 0
S problema con css bootstrap3 HTML e CSS 4
M .load() problema con caricamenti dinamici di js Javascript 0
G Problema con eccessiva nitidezza apertura Camera Raw Photoshop 0
G Problema ------- con Query PHP 1
G Problema con Query PHP 1
T problema con select dinamica con jquery Javascript 0
S Problema con spazi bianchi HTML e CSS 5
A PROBLEMA: insert mysqli con dati Tagsinput Presentati al Forum 0
Tommy03 Problema con z-index HTML e CSS 3
M Problema inserimento parole con apostrofo nel db PHP 5
C Problema con dati meteo xml XML 1
S Problema con infrarossi videocamera IP Cam e Videosorveglianza 1
V Problema con librerie allegro5 c++ C/C++ 1
M Problema con php per calcolo costo percentuale PHP 7
S Problema con mysqli_num_rows PHP 18
grgfede Problema javascript con aruba Javascript 1

Discussioni simili