iOS:返回上一个视图

8

我在Story Board中有两个视图:

[视图1] -> [导航控制器] -> [视图2]

要从视图1(带有表格视图)转到视图2,我会在视图1的行单击时执行以下操作:

self.performSegueWithIdentifier("showmyview2", sender:self)

这个有效。

现在我在视图2上有一个按钮,当点击它时,应该将我带回上一个视图,即视图1。

我尝试过这样做:

 navigationController!.popViewControllerAnimated(true)

但是这并不起作用。我如何返回到第一个视图?

你的 performsegue 是 pushview controller 还是 presentview controller? - Mukesh
miuku> 不确定,我在哪里可以看到? NavigationController是身份检查器中的UINavigationController类。 抱歉,我还是iOS,Swift新手。 - Jasper
1
你的View1不在navigationController层级结构中,因此你不能调用navigationController的popViewController方法返回到View1。 - Vinay Jain
1
请查看以下链接...https://dev59.com/SGcs5IYBdhLWcg3w0HIa - Shruti
Vinay> 那么我该如何将View1带入导航控制器层次结构中,或者有其他解决方案吗? - Jasper
@Jasper 你是说 ViewContoller 而不是 View 吗? - Ch0k0l8
3个回答

32

通过故事板向您的第一个视图1添加导航控制器。(编辑器->嵌入导航控制器),如果导航不可用。

或者

self.dismissViewControllerAnimated(true, completion: nil);
请调用此函数并尝试使用它代替导航popviewcontroller。

6
"SWIFT 3.0"语法的变化如下:self.dismiss(animated: true, completion: nil) 改为:self.dismiss(animated: true, completion: nil) - Trevor

5

在按钮操作中使用此代码以呈现第一个视图:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("FirstView") as! TableViewController
self.presentViewController(vc, animated: true, completion: nil)

别忘了给你的第一个视图(View)设置ID。

在storyBoard中选择你的firstView,然后点击Identity Inspector,你可以像下面的图片一样分配StoryBoard ID。

enter image description here


Dharmesh> 在Storyboard中选择View1后,我没有看到在Identity或Attribute Inspector中提供ID的方法。 - Jasper

0

self.dismiss(animated: true, completion: nil); 这段代码适用于Swift 3


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接