gpt4 book ai didi

ios - 在 swift 中实现 GTScrollNavigationBar

转载 作者:搜寻专家 更新时间:2023-11-01 05:39:39 25 4
gpt4 key购买 nike

我正在尝试使用 GTScrollNavigationBar在我的快速项目中。我是 swift 和 iOS 的新手。所以我正在使用 obj-C 中的演示项目来实现它。演示项目的 AppDelegate 如下所示

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.viewController = [[DemoTableViewController alloc] initWithStyle:UITableViewStylePlain];
self.navController = [[UINavigationController alloc] initWithNavigationBarClass:[GTScrollNavigationBar class] toolbarClass:nil];
[self.navController setViewControllers:@[self.viewController] animated:NO];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
return YES;
}

我已经用swift实现了,看起来像这样

var window: UIWindow?
var navigationController : UINavigationController?
var viewController : UIViewController?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()

self.viewController = TableViewController(style: UITableViewStyle.Plain)
self.navigationController = UINavigationController(navigationBarClass: GTScrollNavigationBar.self, toolbarClass: nil)
(self.window!.rootViewController as! UINavigationController).viewControllers = [viewController!]
self.window?.rootViewController = self.navigationController
self.window?.makeKeyAndVisible()


return true
}

它显示错误 unwrappedly found nil while unwrapping an Optional value at this line (self.window!.rootViewController as! UINavigationController).viewControllers = [viewController!]有人可以告诉我我做错了什么吗?

最佳答案

您的代码应按如下方式转换为 Swift:

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()

self.viewController = UITableViewController(style: UITableViewStyle.Plain)

self.navigationController = UINavigationController(navigationBarClass: GTScrollNavigationBar.self, toolbarClass: nil)
self.navigationController?.viewControllers = [viewController!]

self.window?.rootViewController = self.navigationController
self.window?.makeKeyAndVisible()

我使用 GTScrollNavigationBar 以外的 navigationBar 对其进行了测试;它似乎在工作。

关于ios - 在 swift 中实现 GTScrollNavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32153794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com