简体   繁体   English

如何理解这行 Swift 代码以弹出根视图控制器?

[英]How to understand this line of Swift code to pop to root view controller?

I want to write some code to pop from the current view controller back to the root view controller.我想写一些代码从当前视图控制器弹出回到根视图控制器。 Confusingly enough, I have found numerous answer regarding this, with all sorts of syntax.令人困惑的是,我用各种语法找到了很多关于此的答案。 (I'm guessing old answers in Objective C or different version of Swift...) Here is the simplest syntax that is currently working for me in my AppDelegate: (我猜测在 Objective C 或不同版本的 Swift 中的旧答案......)这是目前在我的 AppDelegate 中为我工作的最简单的语法:

let navigationViewController = self.window?.rootViewController as! UINavigationController

[navigationViewController .popToRootViewController(animated: true)]

I got this far by viewing this answer: https://stackoverflow.com/a/23027260/8887398 His syntax was slighting different, which I guess is old syntax since it was from 2012, so I had to change it slightly, but it's working with my code above.我通过查看这个答案得到了这么远: https : //stackoverflow.com/a/23027260/8887398他的语法略有不同,我猜这是从 2012 年开始的旧语法,所以我不得不稍微改变它,但它是使用我上面的代码。

Question: I don't really understand what's going on in the 2nd line of the code.问题:我真的不明白代码的第 2 行发生了什么。 Sure, I understand it's popping back to the root view controller, but I don't understand the syntax at all.当然,我知道它会弹回到根视图控制器,但我根本不了解语法。 Most confusing is the code is surrounded by a pair of [ ] brackets that I don't understand at all.最令人困惑的是代码被一对我完全不理解的 [] 括号包围。 Next, the popToViewController starts with a dot '.', which is different than the answer I linked.接下来,popToViewController 以点“.”开头,这与我链接的答案不同。 I'm used to using '.'我习惯使用'.' to access properties of an object, but here's it's just used stand alone without anything on the left hand side of it.访问对象的属性,但这里只是单独使用它,左侧没有任何内容。

This code seems to be functioning properly for me, but which countless versions of syntax and ways to pop back to root view controller I've found across the internet, I have no idea whether what I'm doing is proper or how it works at all.这段代码似乎对我来说运行正常,但是我在互联网上找到了无数版本的语法和弹出根视图控制器的方法,我不知道我在做什么是正确的还是它是如何工作的全部。 All I know is "it's working right now".我所知道的是“它现在正在工作”。 Please help me understand this code.请帮助我理解这段代码。

The UINavigationController class has a method inside named as popToRootViewController which takes a bool parameter named animated. UINavigationController 类内部有一个名为 popToRootViewController 的方法,它接受一个名为 animation 的 bool 参数。 If you pass animated value as true the navigation will happen with animation.如果您将动画值传递为 true,则导航将随动画一起发生。

So to call the method in objective c所以在目标c中调用方法

UINavigationController *navigationController = (UINavigationController  *)self.window.rootViewController;
[navigationController popToRootViewControllerAnimated:YES];

Above in first time we got a pointer to navigationController.上面我们第一次得到了一个指向 navigationController 的指针。 Using that we are calling the popToRootViewControllerAnimated method inside the UINavigationController class with parameter value YES(in Objc YES is for true and NO is for false)使用它,我们使用参数值 YES 调用 UINavigationController 类中的 popToRootViewControllerAnimated 方法(在 Objc 中,YES 表示真,NO 表示假)

The same thing above can be written in swift as上面同样的东西可以用swift写成

let rootNavController = self.window?.rootViewController as! UINavigationController

rootNavController.popToRootViewController(animated: true)

Similar to Scenario in OBjc the above written syntax is in swift.与 OBjc 中的 Scenario 类似,上面的书面语法是 swift 。 we get the instance of navController an we call the popToRootViewController method()我们得到了 navController 的实例,我们调用了 popToRootViewController 方法()

In fact, it's just an array...其实它只是一个数组...

You could write: navigationViewController.popToRootViewController(animated: true)你可以写: navigationViewController.popToRootViewController(animated: true)

And that would do the same.这样做也一样。

If you do :如果你这样做:

let x = navigationViewController.popToRootViewController(animated: true)

x will contain an optional array of UIViewController of type [UIViewController]? x将包含的可选阵列UIViewController类型的[UIViewController]?

If you do :如果你这样做:

let x = [navigationViewController.popToRootViewController(animated: true)]

x will contain an array of with an optional array of UIViewController of type [[UIViewController]?] x将包含一个带有[[UIViewController]?]类型的可选UIViewController数组的数组

If you do :如果你这样做:

[navigationViewController.popToRootViewController(animated: true)]

The array of array will never be used...永远不会使用数组数组...

And the space is simply ignored in Swift, for instance you can do :而空格在 Swift 中被简单地忽略,例如你可以这样做:

myObject
    .myMethod()

It's often used for fluent syntax...它通常用于流畅的语法......

EDIT :编辑

The Objective-C call for this method would be:此方法的 Objective-C 调用将是:

[navigationViewController popToRootViewControllerAnimated: YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM