簡體   English   中英

將數據從類傳遞到視圖控制器的相同實例

[英]Passing data from a class to same instance of a viewcontroller

當發生特定事件時(在我的情況下,當選項卡欄更改時),我想從數組創建一個新鏈接。 我已經做到這一點,但我沒有遇到的問題是當我嘗試將生成的鏈接傳遞給同一個viewcontroller時出現錯誤

致命錯誤:解開Optional值時意外發現nil

當我嘗試更改UILabel movietitle和imageview時,會發生這種情況。 我認為這是因為每次發送鏈接時,它都會創建一個新的ViewController而不是使用現有的ViewController。 也可能是因為我錯過了某個地方的包裝價值。 希望這里有人可以幫助我!

StringBuilder的:

import UIKit

class StringBuilder: NSObject {

let urlString = "https://api.themoviedb.org/3/discover/movie?api_key=935f539acb9e5534ddeed3fb57e&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1&with_genres=12"

let urlStringMultipleGenres = "https://api.themoviedb.org/3/discover/movie?api_key=935f539acbf5534ddeed3fb57e&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1&with_genres=28,12,10749"
var currentGenreArray: Array<Int> = []






//This will be run after the user has selected or deselected genres in the genreControllerView
func updateGenres(genreArrayIn: Array<Int>){
    print("update genres input: ")
    print(genreArrayIn)

    //If new array input is the same as old arrayinput, do nothing
    if genreArrayIn == currentGenreArray{
        return
    }
    else{
        let returnedLink = generateString(genreID: genreArrayIn)
        print("Returned link after generate string" + returnedLink)
        sendLink(link: returnedLink)
    }


}
//After the updated genres have been put into an Array, this function will generate the whole string which
//will be the main String the getMovieRequest follows
func generateString(genreID: Array<Int>) -> String{

    let filteredGenreArray = filterZeroes(unfilteredArray: genreID)

    currentGenreArray = genreID
    print("current genre array: ")
    print(currentGenreArray)

    let baseString = "https://api.themoviedb.org/3/discover/movie?api_key=935f539acbfed4ddeed3fb57e&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1&with_genres="
    var generatedString = baseString

    for id in filteredGenreArray{
        let k = String(id)
        generatedString += k + ","
    }
    print("Generated Link from Strinbuilder: ")
    print(generatedString)


    return generatedString
}
func filterZeroes(unfilteredArray: Array<Int>) -> Array<Int>{

    let filteredGenreArray = unfilteredArray.filter {$0 > 0}
    print("filtered array: ")
    print(filteredGenreArray)
    return filteredGenreArray
}


func sendLink(link: String){
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let movieVC = storyBoard.instantiateViewController(withIdentifier: "movieView") as! ViewController

    movieVC.getMovieData(activeGenreLink: link)
    print("new link sent from sendlink()")
}

}

視圖控制器:

import UIKit
import Alamofire
import AlamofireImage





class ViewController: UIViewController{

static let sharedInstance = ViewController()

var movieIndex = 0
var movieArray:[Movie] = []
var downloadGrp = DispatchGroup()




@IBOutlet var uiMovieTitle: UILabel!

@IBOutlet var uiMoviePoster: UIImageView!

@IBOutlet var posterLoading: UIActivityIndicatorView!



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let firstTimeLink = "https://api.themoviedb.org/3/discover/movie?api_key=935f539acb9e5534ddeed3fb57e&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1&with_genres=35,18"

    getMovieData(activeGenreLink: firstTimeLink)

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
    uiMoviePoster.isUserInteractionEnabled = true
    uiMoviePoster.addGestureRecognizer(tapGestureRecognizer)
    print("settings Sucessful")
}


func imageTapped(tapGestureRecognizer: UITapGestureRecognizer){





     performSegue(withIdentifier: "detailsSegue", sender: self)
    }



@IBAction func yesBtn(_ sender: UIButton) {
    movieIndex += 1
    updateUI()
}

@IBAction func seenBtn(_ sender: UIButton) {
    movieIndex += 1
}

@IBAction func noBtn(_ sender: UIButton) {
    movieIndex += 1
}




//Get movie data

func getMovieData(activeGenreLink: String){

    //self.posterLoading.startAnimating()

    movieIndex = 0
    self.downloadGrp.enter()
        Alamofire.request(activeGenreLink).responseJSON { response in
            //print(response.request)  // original URL request
            //print(response.response) // HTTP URL response
            //print(response.data)     // server data
            //print(response.result)   // result of response serialization

            self.movieArray = []

            print(self.movieArray)
            if let json = response.result.value as? Dictionary<String,AnyObject> {
                if let movies = json["results"] as? [AnyObject]{
                    for movie in movies{
                        let movieObject: Movie = Movie()

                        let title = movie["title"] as! String
                        let releaseDate = movie["release_date"] as! String
                        let posterPath = movie["poster_path"] as! String
                        let overView = movie["overview"] as! String
                        let movieId = movie["id"] as! Int
                        let genre_ids = movie["genre_ids"] as! [AnyObject]

                        movieObject.title = title
                        movieObject.movieRelease = releaseDate
                        movieObject.posterPath = posterPath
                        movieObject.overView = overView
                        movieObject.movieId = movieId


                        for genre in genre_ids{//Genre ids, fix this
                            movieObject.movieGenre.append(genre as! Int)
                        }

                        Alamofire.request("http://image.tmdb.org/t/p/w1920" + posterPath).responseImage {
                            response in
                            //print(response.request)
                            //print(response.response)
                            //debugPrint(response.result)

                            if var image = response.result.value {
                                image = UIImage(data: response.data!)!

                                movieObject.poster = image
                            }
                        }

                        self.movieArray.append(movieObject)


                    }//End of for each movie
                }
                else{
                    print("error while making results anyobject")
                }
            }
            else{
            print("error while trying to make NSDictionary")}
     self.downloadGrp.leave()
    }//End of Json request


    downloadGrp.notify( queue: .main){
        print("all downloads finished")


        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) {
            print(self.movieArray[0].title!)
            self.updateUI()
            print("updatedUI")
        }


    }

}//End of getmoviedata


override func prepare(for segue: UIStoryboardSegue,sender: Any?){

    // Create a variable that you want to send
    let currentMovie = self.movieArray[movieIndex]


    if let destinationVC = segue.destination as? DetailsViewController{
        destinationVC.currentMovie = currentMovie
    }

}

func updateUI(){
    //self.posterLoading.stopAnimating()
    if uiMoviePoster == nil{
    print(uiMovieTitle.debugDescription)
    }
    else{
        print("first time debugID: " + uiMovieTitle.debugDescription)
    uiMovieTitle.text = self.movieArray[movieIndex].title
    uiMoviePoster.image = self.movieArray[movieIndex].poster
    }

}


}

您想獲取不實例化故事板的sharedInstance

let movieVC = ViewController.sharedInstance()

但我還是不明白為什么你需要這樣做

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM