Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift:在执行segue之前设置变量_Ios_Swift_Uitableview_Segue_Cell - Fatal编程技术网

Ios Swift:在执行segue之前设置变量

Ios Swift:在执行segue之前设置变量,ios,swift,uitableview,segue,cell,Ios,Swift,Uitableview,Segue,Cell,我有一个桌面视图,其中pokedex中的每个口袋妖怪都有一个单元格。当我按下单元格中的一个按钮时,我想显示一个包含该生物详细信息的视图 我要做的是,我有一个全局变量currentPokemon,当按下按钮时,我将其设置为请求的pokemon。这是我的手机代码: class PokemonTableViewCell: UITableViewCell { var pokemon: Pokemon! @IBOutlet weak var pokemonImage: UIImageVi

我有一个桌面视图,其中pokedex中的每个口袋妖怪都有一个单元格。当我按下单元格中的一个按钮时,我想显示一个包含该生物详细信息的视图

我要做的是,我有一个全局变量currentPokemon,当按下按钮时,我将其设置为请求的pokemon。这是我的手机代码:

class PokemonTableViewCell: UITableViewCell {

    var pokemon: Pokemon!
    @IBOutlet weak var pokemonImage: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var releaseDateLabel: UILabel!

    @IBAction func setPokemon(sender: UIButton) {
        currentPokemon = self.pokemon

    }
}

当我试图在details视图中访问currentPokemon变量时,我得到一个致命错误,因为currentPokemon为零。如何在segue之前执行此代码?

如果使用segue,则应使用此UIViewController方法将数据传递给详细控制器:


func performsguewithidentifier标识符:String,sender:Any?

您需要为所选Pokemon添加存储区,其中包含singleton实例,如:

struct Pokemon {
    let name: String
}

class PokemonStore {

    static let instance = PokemonStore()
    var currectPokemon: Pokemon?
}

稍后,您可以使用PokemonStore在代码的任何位置保存pokemon。instance.CurrencPokemon

用户可以通过prepareForSegue方法将您的pokemon发送到下一个控制器。但是对于你的情况-你在哪里存储当前的口袋妖怪?你没有设置口袋妖怪的任何东西。您只是在创建变量,但从未初始化它。@ArtemNovichkov它存储在一个.swift文件中,在Pokemon类定义之外,可以从其他视图访问它,如果我设置了一个默认的Pokemon,它就会工作。哪个对象存储它?它是单例吗?@TheValyLeanGroup在TableViewController中创建单元格时已初始化,我可以访问其名称和其他值。这是一个Override函数,您需要将帖子格式化为代码。