从其类型初始化viewcontroller

发布于 2025-01-26 15:59:55 字数 1200 浏览 2 评论 0原文

我有视图控制器类型的列表。我想初始化它的相应类型。

struct TabbedViewControllers {
    static var viewControllers: [UIViewController.Type] {
        return [FeedViewController.self,
                SearchViewController.self,
                NotificationViewController.self,
                ProfileViewController.self]
    }
}

在这里,feedViewControllersearchViewControllernotificationViewControllerprofile> profile> profile> profile> profile> profileviewcontroller是UiviewController类型。

我正在创建一个自定义的TABBARCONTROLLERVIEWCONTROLLER类,其中我需要从TabbedViewControllers.viewControllers property设置ViewControllers数组。

class TabBarControllerViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
    }
    
    func setViewControllers() {
        for vc in TabbedViewControllers.viewControllers {
            self.viewControllers?.append(vc)
        }
    }
}

我正在汇编时间错误 - 无法将'uiviewController.type'类型的值转换为预期参数类型'uiviewController'

如何从其类型中初始化viewcontrollers。

谁能帮忙。

I have a list of view controller types. I want to initialize the corresponding types out of it.

struct TabbedViewControllers {
    static var viewControllers: [UIViewController.Type] {
        return [FeedViewController.self,
                SearchViewController.self,
                NotificationViewController.self,
                ProfileViewController.self]
    }
}

Here, FeedViewController, SearchViewController, NotificationViewController, ProfileViewController are UIViewController types.

I am creating a custom TabBarControllerViewController class where I need to set the viewControllers array from the TabbedViewControllers.viewControllers property.

class TabBarControllerViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
    }
    
    func setViewControllers() {
        for vc in TabbedViewControllers.viewControllers {
            self.viewControllers?.append(vc)
        }
    }
}

I am getting compile time error -
Cannot convert value of type 'UIViewController.Type' to expected argument type 'UIViewController'

How to initialize viewControllers from its types.

Can anyone help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2025-02-02 15:59:55

您只是无法将UiviewController的 type 传递给tabbar。您需要将uiviewController的完全初始化对象传递给tabar,以使其工作。

您可以简单地初始化uiviewController并传递对象

struct TabbedViewControllers {
   static var viewControllers: [UIViewController] {
       return [FeedViewController(),
               SearchViewController(),
               NotificationViewController(),
               ProfileViewController()]
   }
}

或使用故事板

struct TabbedViewControllers {
   static var viewControllers: [UIViewController] {
       return [
          UIStoryboard(name: "FeedViewController", bundle: nil).instantiateViewController(withIdentifier: "FeedViewController"),
          UIStoryboard(name: "SearchViewController", bundle: nil).instantiateViewController(withIdentifier: "SearchViewController"),
          UIStoryboard(name: "NotificationViewController", bundle: nil).instantiateViewController(withIdentifier: "NotificationViewController"),
          UIStoryboard(name: "ProfileViewController", bundle: nil).instantiateViewController(withIdentifier: "ProfileViewController")
       ]
    }
}

You just can't pass Type of UIViewController to TabBar. You need to pass fully initialized object of UIViewController to TabBar for it to work.

You can simply initialize your UIViewController and pass the object

struct TabbedViewControllers {
   static var viewControllers: [UIViewController] {
       return [FeedViewController(),
               SearchViewController(),
               NotificationViewController(),
               ProfileViewController()]
   }
}

Or if using a storyboard

struct TabbedViewControllers {
   static var viewControllers: [UIViewController] {
       return [
          UIStoryboard(name: "FeedViewController", bundle: nil).instantiateViewController(withIdentifier: "FeedViewController"),
          UIStoryboard(name: "SearchViewController", bundle: nil).instantiateViewController(withIdentifier: "SearchViewController"),
          UIStoryboard(name: "NotificationViewController", bundle: nil).instantiateViewController(withIdentifier: "NotificationViewController"),
          UIStoryboard(name: "ProfileViewController", bundle: nil).instantiateViewController(withIdentifier: "ProfileViewController")
       ]
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文