在ios 8 beta 5中是否损坏了带有nib文件的视图控制器? [英] Are view controllers with nib files broken in ios 8 beta 5?

查看:98
本文介绍了在ios 8 beta 5中是否损坏了带有nib文件的视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ios 8 beta 4中创建了一个测试项目,它作为主视图控制器和第二个视图控制器创建为带有xib文件的UIViewController子类。

I created a test project in ios 8 beta 4 which as a main view controller and a second view controller created as a UIViewController subclass with a xib file.

I在主控制器上放置一个按钮以显示第二个控制器:

I put a button on the main controller to present the second controller:

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func testVCBtnTapped() {
    let vc = TestVC()
    presentViewController(vc, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

我运行应用程序并按下按钮显示第二个控制器 - 一切都很好

I run the app and pressing the button presents the second controller - all is well

移动到xcode beta 5,我运行应用程序,当我按下按钮时,屏幕进入黑色。

Moving to xcode beta 5, I run the app and when I press the button the screen goes black.

因为我知道他们搞砸了初始化代码,所以我尝试使用覆盖来查看它会修复它:

Since I know they messed with the init code, I tried putting in overrides to see it that would fix it:

class TestVC: UIViewController {

override init() {
    super.init()
}
required init(coder aDecoder: NSCoder!) {
    super.init(coder: aDecoder)
}
override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

同样的问题。将所需和覆盖更改为xcode接受的所有可能组合都没有效果。

Same problem. Changing the required and overrides in to all possible combinations accepted by xcode has no effect.

如果我使用故事板创建另一个控制器并且它已经完成,那么一切都很好。

If I use the storyboard to create another controller and segue to it all is well.

任何想法?

编辑 - 新信息


  1. 在init中尝试了nibName = nil - 同样的问题

  2. 在目标c中创建了相同的应用程序并且工作正常

显然是一个快速的beta 5问题

Apparently a swift beta 5 problem

推荐答案

我不知道它是否是一个bug或不是,但它绝对是一个变化。当然他们可能会改回来......无论如何,种子5中的规则是:

I can't tell whether it's a bug or not, but it is definitely a change. Of course they might change it back... In any case, the rule in seed 5 is:

视图控制器只是因为它具有相同的名称而自动找到它的.xib。您必须明确提供名称。

The view controller will not automatically find its .xib just because it has the same name. You have to supply the name explicitly.

因此,在您的情况下,任何初始化此视图控制器的尝试必须最终调用 nibName:bundle :带有明确的笔尖名称(TestVC),我假设。

So in your case any attempt to initialize this view controller must ultimately call nibName:bundle: with an explicit nib name ("TestVC"), I assume.

如果你想通过调用来初始化

If you want to be able to initialize by calling

let vc = TestVC()

正如您在演示视图控制器中所做的那样,然后简单地覆盖 init()提出视图控制器来调用 super.init(nibName:TestVC,bundle:nil)这就是你所需要的(除了我认为你还需要 init(编码器:) 塞子讨论在这里)。

as you are doing in your presenting view controller, then simply override init() in the presented view controller to call super.init(nibName:"TestVC", bundle:nil) and that's all you need (except that I presume you will also need the init(coder:) stopper discussed here).

编辑你绝对正确,这是一个仅限Swift的问题。发现得好。在Objective-C中,使用 init (或 new )初始化仍然可以正常工作;视图控制器正确找到它的同名.xib文件。

EDIT You are absolutely right that this is a Swift-only problem. Well spotted. In Objective-C, initializing with init (or new) still works fine; the view controller finds its eponymous .xib file correctly.

另一个编辑决定因素不是Objective-C或Swift是否调用初始化视图控制器本身是用Objective-C还是Swift编写的。

ANOTHER EDIT The determining factor is not whether Objective-C or Swift calls init. It is whether the view controller itself is written in Objective-C or Swift.

最终编辑解决方法是像这样声明你的Swift视图控制器:

FINAL EDIT The workaround is to declare your Swift view controller like this:

@objc(ViewController) ViewController : UIViewController { // ...

括号中的名称消除了导致问题的名称错误。很可能在下一个版本中,这将被修复,你可以再次取走 @objc

The name in parentheses gets rid of the name mangling which is causing the problem. It is likely that in the next release this will be fixed and you can take the @objc away again.

另一个最终编辑坏消息:我提交的错误报告回来了按预期工作。他们指出,我所要做的就是在周围模块之后命名 .xib 文件,例如:如果我的应用程序名为 NibFinder ,那么如果我将 .xib 文件命名为 NibFinder.ViewController.xib ,则会在实例化时自动找到它code> ViewController()。

ANOTHER FINAL EDIT Bad news: the bug report I filed on this came back "works as intended". They point out that all I have to do is name the .xib file after the surrounding module, e.g. if my app is called NibFinder, then if I name my .xib file NibFinder.ViewController.xib, it will be found automatically when instantiating ViewController().

这是真的,但在我看来它只是重述了这个bug; Swift查找过程在模块名称前面加上。因此,苹果公司表示我应该为我的 .xib 文件提供相同的模块名称,而我要说苹果应该关注并在执行搜索时删除模块名称。

That is true enough, but in my view it merely restates the bug; the Swift lookup procedure is prepending the module name. So Apple is saying I should knuckle under and prepend the same module name to my .xib file, whereas I am saying that Apple should knuckle under and strip the module name off as it performs the search.

编辑真的很棒这个错误在iOS 9 beta 4中得到修复,所有这些变通办法都变得不必要了。

EDIT THAT IS TRULY TRULY FINAL This bug is fixed in iOS 9 beta 4 and all these workarounds become unnecessary.

这篇关于在ios 8 beta 5中是否损坏了带有nib文件的视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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