栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Swift中以编程方式制作Segue

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Swift中以编程方式制作Segue

您可以像在此答案中建议的那样进行操作:
InstantiateViewControllerWithIdentifier。

此外,由于链接中的答案最初是用Objective-C编写的,因此我为您提供了用Swift重写的链接答案中的代码。

let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "identifier") as! SecondViewControllervc.resultsArray = self.resultsArray

编辑:

由于此答案引起了一定的关注,我想我为您提供了另一种更安全的方法。在上述答案中,如果

ViewController
with “ identifier”
不是type,则应用程序将崩溃
SecondViewController
。在Swift中,您可以通过使用可选绑定来防止此崩溃:

guard let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewControllerWithIdentifier("identifier") as? SecondViewController else {    print("Could not instantiate view controller with identifier of type SecondViewController")    return}vc.resultsArray = self.resultsArrayself.navigationController?.pushViewController(vc, animated:true)

ViewController
如果类型为,则以这种方式推送
SecondViewController
。如果无法转换
SecondViewController
为消息,则会打印一条消息,并且应用程序将保持当前状态
ViewController



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/403897.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号