如何使用Swift访问tabBarController中的ObjectAtIndex? [英] How to access ObjectAtIndex in tabBarController with Swift?

查看:403
本文介绍了如何使用Swift访问tabBarController中的ObjectAtIndex?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经说过obj-c

i used to say in obj-c

[self.tabBarController.viewControllers objectAtIndex:1];

但现在在swift中没有ObjectAtIndex了

but now in swift no ObjectAtIndex any more

self.tabBarController.viewControllers.ObjectAtIndex






更新

好的我会让它变得简单让我觉得我有tabBarController它包含2个对象
[FirstViewController,SecondViewController]
我试图在对象之间建立一个委托
这里是设置委托的代码

ok i am gonna make it simple lets consider i have tabBarController it contains 2 object [FirstViewController,SecondViewController] and i am trying to make a delegate between the object here is the code to set the delegate

var Svc:SecondViewController = self.tabBarController.viewControllers[1] as SecondViewController!
Svc.delegate = self

当我运行时,我收到此错误0x1064de80d:movq% r14,%rax并且没有出现控制台错误

when i Run , i got this error 0x1064de80d: movq %r14, %rax and no console error is showing up

推荐答案

您的代码没问题:

var svc:SecondViewController = self.tabBarController.viewControllers[1] as SecondViewController!
svc.delegate = self

...但是你可以省略在结尾处标记:SecondViewController 类型定义,因为它可以由演员推断:

... however you can omit ! mark at the end and :SecondViewController type definition since it can be inferred by the cast:

var svc = self.tabBarController.viewControllers[1] as SecondViewController

问题出现是因为你试图转换为错误的类。
尝试打印到 [1] 的对象类的调试日志名称;在演员表之前添加此项来检查班级名称:

The problem appears because you try to cast to the wrong class. Try to print to debug log name of the class of object at [1]; add this before your cast to check the class name:

let vcTypeName = NSStringFromClass(self.tabBarController.viewControllers[1].classForCoder)
println("\(vcTypeName)")

更新:

正如我们在评论中指出的那样,您应该将接收到的视图控制器转换为 UINavigationController

As we figured out in comments, you should cast received view controller to UINavigationController:

var nc = self.tabBarController.viewControllers[1] as UINavigationController

稍后你可以检查 nc.viewControllers 属性,看看是否它的 topViewController SecondViewController

Later you can examine nc.viewControllers property and see if for instance its topViewController is SecondViewController:

if nc.topViewController is SecondViewController {
    var svc = nc.topViewController as SecondViewController
    // your code goes here
}

这篇关于如何使用Swift访问tabBarController中的ObjectAtIndex?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆