Ios 通过根视图控制器传递对象
我有一个通过编程创建的Ios 通过根视图控制器传递对象,ios,swift,swift2,segue,Ios,Swift,Swift2,Segue,我有一个通过编程创建的rightBarButtonItem,我想触发导航控制器的模式序列。导航控制器是“编辑视图控制器”场景的“根视图控制器” 这里是一个,除了这里按钮是在情节提要中创建的,因此您可以控制从情节提要中的按钮拖动片段。这就是我所说的结构 HomeVC --(modal)--> Navigation Controller --(root view)--> EditVC 我不能在我的故事板中插入rightBarButtonItem,所以我必须以编程的方式创建一个 我在这里
rightBarButtonItem
,我想触发导航控制器的模式序列。导航控制器是“编辑视图控制器”场景的“根视图控制器”
这里是一个,除了这里按钮是在情节提要中创建的,因此您可以控制从情节提要中的按钮拖动片段。这就是我所说的结构
HomeVC --(modal)--> Navigation Controller --(root view)--> EditVC
我不能在我的故事板中插入rightBarButtonItem
,所以我必须以编程的方式创建一个
我在这里的目的是提供一个模式屏幕来编辑对象,因此我需要通过导航控制器将该对象从我的HomeVC传递到我的EditVC。第一个链接中的示例是创建一个对象,因此无需传递任何内容
我现在就在这里。My HomeVC(简化名称以便更容易理解)有一个导航栏,其中有一个通过编程创建的rightBarButtonItem
,其选择器调用一个函数:
func editButtonPressed() {
// doesn't call prepareForSegue unless I prepend super.
super.performSegueWithIdentifier("ShowEdit", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
print("this one prints")
if segue.identifier == "ShowEdit" {
print("this one prints")
// prints out the UINavigationController, not the EditVC
print("segue: \(segue.destinationViewController)")
if let editViewController = segue.destinationViewController as? EditViewController {
// THIS IS WHAT I WANT TO ACCOMPLISH
editViewController.selectedObject = selectedObject
print("DOES NOT PRINT")
} else {
print("this one prints")
}
}
我的问题在于我的prepareForSegue函数:
func editButtonPressed() {
// doesn't call prepareForSegue unless I prepend super.
super.performSegueWithIdentifier("ShowEdit", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
print("this one prints")
if segue.identifier == "ShowEdit" {
print("this one prints")
// prints out the UINavigationController, not the EditVC
print("segue: \(segue.destinationViewController)")
if let editViewController = segue.destinationViewController as? EditViewController {
// THIS IS WHAT I WANT TO ACCOMPLISH
editViewController.selectedObject = selectedObject
print("DOES NOT PRINT")
} else {
print("this one prints")
}
}
如果你能看到我的意思,我想把这个对象从HomeVC传递到EditVC,但是我的segue当前要传递到导航根视图控制器。如何通过导航控制器将其传递到EditVC
我考虑过为导航控制器创建一个控制器,因此它将是
myNavVC.selectedObject==selectedObject
,然后在myNavVC的prepareForSegue中使用EditVC.selectedObject==selectedObject
,但由于标识符是根视图,我将如何使用它呢?在Objective-C中演示它:
UINavigationController *navigationController = (UINavigationController*)segue.destinationViewController;
EditViewController *editViewController = (EditViewController *)[[navigationController viewControllers] firstObject];
editViewController.selectedObject = selectedObject;
Swift演示:
if let navigationController = segue.destinationViewController as? UINavigationController {
if let editViewController = navigationController.viewControllers.first as? EditViewController {
editViewController.selectedObject = selectedObject
}
}
在Objective-C中演示:
UINavigationController *navigationController = (UINavigationController*)segue.destinationViewController;
EditViewController *editViewController = (EditViewController *)[[navigationController viewControllers] firstObject];
editViewController.selectedObject = selectedObject;
Swift演示:
if let navigationController = segue.destinationViewController as? UINavigationController {
if let editViewController = navigationController.viewControllers.first as? EditViewController {
editViewController.selectedObject = selectedObject
}
}
您确定destVC是editVC吗?因此,现在destVC是导航控制器,它是editVC的根视图,这就是为什么如果让editViewController…,它不会进入
。请参阅我的最后一段,了解我想做什么,但不知道如何做。您确定destVC是editVC吗?因此,现在destVC是导航控制器,它是editVC的根视图,这就是为什么如果让editViewController…
,它不会进入。查看我的最后一段,了解我想做什么,但不知道怎么做。我刚开始iOS编程,只使用了Swift,所以我不知道Objective-C语法,所以我不确定第二行中发生了什么[[navigationController ViewController]firstObject]
看起来它可以工作了,谢谢@pacification我将他的代码实现为'if let navigationController=segue.destinationViewController as?UINavigationController{如果让editViewController=navigationController.ViewController.first作为?editViewController{editViewController.selectedObject=selectedObject}}`为了摆脱强制展开,你看起来不错?很抱歉@Proton,你给出了正确的答案,但是是的,Tommy K,如果让…
或保护…
我刚开始iOS编程,只使用了Swift,所以我不知道Objective-C语法,所以我不确定第二行中发生了什么[[navigationController ViewController]firstObject]
看起来很有效谢谢@pacification我将他的代码实现为'if let navigationController=segue.destinationViewController as?UINavigationController{如果让editViewController=navigationController.ViewController.first作为?editViewController{editViewController.selectedObject=selectedObject}}`为了摆脱强制展开,你看起来不错?很抱歉@Proton,你给出了正确的答案,但是是的,Tommy K,如果让…或保护…