英文:
iOS app crashes built by Xcode 14.3: Cannot manually set the delegate on a UINavigationBar
问题
I have an iOS app. Recently, I updated my Xcode to ver 14.3. My iOS crashed right at the start point. The following is the exception error message:
Cannot manually set the delegate on a UINavigationBar managed by a controller.
This did not happen in the previous Xcode 14.1.
I use storyboards to setup the app's UI. Here is the entrance defined in my app:
In my app info plist file, I set the entrance of the storyboard as MainStoryboard
:
In this way, I don't need to specify which view controller to load as the initial start point in AppDelegate
class.
The root view controller in MainStoryboard.storyboard
is a tab view controller with 3 tabs, pointing to other 3 storyboard references. The root view controller is based on a customized class:
class RootViewController: UITabBarController {
....
}
where the three tabs are set up with titles and images. I tried to set a break point in viewLoaded
event, but the crash happened before reaching my code.
I could not find any place in my iOS project where the delegate on a UINavigationBar is set manually in this storyboard or in my customized class. There is no navigation controller in the root view controller at all.
There are no any compiling warnings or errors at all. I am not sure why the exception is raised before reaching my iOS codes. Is there any property settings in my iOS project, or any settings in my MainStoryboard.storyboard
that caused the error? Or are any major changes or rules in Xcode 14.3 I should comply?
The strange thing is that if I roll back to Xcode 14.1, the previous version, my app compiles and runs fine.
I greatly appreciate any help or advice to resolve the issue.
英文:
I have an iOS app. Recently, I updated my Xcode to ver 14.3. My iOS crashed right at the start point. The following is the exception error message:
Cannot manually set the delegate on a UINavigationBar managed by a controller.
This did not happen in the previous Xcode 14.1.
I use storyboards to setup the app's UI. Here is the entrance defined in my app:
In my app info plist file, I set the entrance of the storyboard as MainStoryboard
:
In this way, I don't need to specify which view controller to load as the initial start point in AppDelegate
class.
The root view controller in MainStoryboard.storyboard
is a tab view controller with 3 tabs, pointing to other 3 storyboard references. The root view controller is based on a customized class:
class RootViewController: UITabBarController {
....
}
where the three tabs are set up with titles and images. I tried to set a break point in viewLoaded
event, but the crash happened before reaching my code.
I could not find any place in my iOS project where the delegate on a UINavigationBar is set manually in this storyboard or in my customized class. There is no navigation controller in the root view controller at all.
There are no any compiling warnings or errors at all. I am not sure why the exception is raised before reaching my iOS codes. Is there any property settings in my iOS project, or any settings in my MainStoryboard.storyboard
that caused the error? Or are any major changes or rules in Xcode 14.3 I should comply?
The strange thing is that if I roll back to Xcode 14.1, the previous version, my app compiles and runs fine.
I greatly appreciate any help or advice to resolve the issue.
答案1
得分: 0
感谢neklas和Geoff-Hackworth的两条评论。根据他们的建议,我进一步调查了这个问题。
首先,我在AppDelegate
中添加了一些代码来启动我的起始视图控制器,如下所示:
// 代码部分不翻译
并在项目的应用信息属性列表文件中删除了“Main storyboard file base name”这一行。通过这些更改,我可以让应用停在项目中的代码处。正如上述代码中所注释的那样,我收到了关于“Cannot manually set the delegate on a UINavigationBar managed by a control”的相同异常。
我继续追踪了其他三个故事板。在它们的故事板中,在起始视图控制器之前有三个导航控制器(链接到3个故事板引用)。在故事板中,我确实看到导航栏和导航控制器之间有一个链接设置,如图片所示。
在最后的更改步骤中,我删除了这个链接,以及另外两个故事板中的相同链接。
就是这样!重新编译我的iOS应用程序后,应用程序可以在没有更多错误的情况下启动!
我认为在第一个选项卡视图控制器启动时,运行时引擎确实会检查链接的选项卡视图(所有视图控制器)。如果在故事板或代码中定义了任何导航栏委托,新的Xcode 14.3将抛出异常错误。
对我来说,这在我的项目中真的是一个难以发现的问题,至少对我来说是这样。
干杯!现在我可以继续使用Xcode 14.3的工具更新我的iOS应用程序。再次感谢那两个人给了我找到和解决问题的提示。
关于我进一步发现的更多信息
我在我的一个视图控制器中放置了以下代码,以找出导航栏委托被设置为什么:
// 代码部分不翻译
调试打印结果:
nv: <UINavigationController: 0x10a822600>
nv bar delegate: <UINavigationController: 0x10a822600>
它确认了委托被设置为导航控制器。然而,这个设置在Xcode 14.3中无法重复执行或更改,无论是在故事板中还是在已编译的目标代码中。
我还对之前的Xcode 14.1进行了检查。这些更改不影响我的应用程序正常启动。
英文:
Thanks for 2 comments by neklas and Geoff-Hackworth. Following their advice, I further investigated the issue.
First, I added some codes in AppDelegate
to launch my start view controller like this:
@UIApplicationMain
class AppDelegate : UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?)
-> Bool
{
// Set break point here
let mainStoryboard : UIStoryboard = UIStoryboard(
name: "MainStoryboard",
bundle: nil)
let initialVC : UIViewController?
= mainStoryboard.instantiateInitialViewController()!
// Exception raised at the above line!
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialVC!
self.window?.makeKeyAndVisible()
return true
}
}
and removed the line "Main storyboard file base name" in my app info plist file in the project. With these changes, I can let the app stop at the code in my project. As commented in the above codes, I got the same exception about "Cannot manually set the delegate on a UINavigationBar managed by a control".
I continued to chase down my other 3 storyboards. There are three navigation controllers before the start view controller in their storyboards (as linked to 3 storyboard references). In the storyboard, I did see there was a link setting between the navigation bar and navigation controller, as shown in the picture.
In the last change step, I removed this link, and the same links in another two storyboards.
That's it! After recompiling my iOS app, the app can be launched with no more errors!
I think that at the launch time of the first tab view controller, the runtime engine does check the linked tab views (all view controllers). If there is any navigation bar delegate defined, either in storyboards or codes, the new Xcode 14.3 will throw an exception error.
That's a really hard-to-find problem in my project, at least for me.
Cheers! Now I can continue to update my iOS app with the tool of Xcode 14.3. Thanks again for those two people giving me hints to find and resolve my issue.
MORE INFORMATION ABOUT MY FURTHER FINDINGS
I put the following codes in one of my view controllers to find out what has been set to the navigation bar delegate:
override func viewDidLoad()
{
super.viewDidLoad()
let dl = navigationController?.navigationBar.delegate
if let dl = dl {
print("nv: \(navigationController)\nnv bar delegate: \(dl)")
}
The debug print result:
nv: <UINavigationController: 0x10a822600>
nv bar delegate: <UINavigationController: 0x10a822600>
It confirms that the delegate is set to the navigation controller. However, this setting cannot be repeatedly done or changed, either in the storyboard or in the code in the compiled target in Xcode 14.3.
I also did a check for the previous Xcode 14.1. Those changes do not affect my app's normal launch.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论