在代码中决定在应用程序启动时加载哪个类和 xib 文件

发布于 2024-08-21 13:32:19 字数 403 浏览 4 评论 0原文

我正在更改我的应用程序以添加应用程序内购买。当我最初设置应用程序时,我使用了界面生成器,但现在我需要决定使用哪个类和类。 xib 文件根据用户是否购买了某项功能在应用程序启动时加载。

我的应用程序设置了一个选项卡栏控制器,每个选项卡都有一个 naviagion 控制器。在 MainWindow.xib 文件中,我告诉 IB 使用 HomeView.xib 和 HomeView 类。现在我需要检查是否已购买某个功能并加载 HomeView 或 HomeView2。我不知道应用程序中的哪个位置告诉 naviagionController 要加载哪个类和视图,因为我之前在界面生成器中设置了所有这些。

我可以在 HomeViewNavigationController 中做出这个决定,而不是 IB 自动为我做出这个决定吗?

预先感谢您的帮助!

I'm changing my app to add an in app purchase. When I initially set up the app I used interface builder but now i need to decide which class & xib file to load on app launch based on whether or not a user has purchased a feature.

My app is set up with a tab bar controller that has a naviagion controller for each tab. Inside the MainWindow.xib file I've told IB to use the HomeView.xib and HomeView class. Now I need to check if a feature has been purchased and load either HomeView or HomeView2. I don't know where in the app to tell the naviagionController which class and view to load as I previously set all this up in interface builder.

Can I make this decision inside the HomeViewNavigationController instead of IB automatically making this decision for me?

Thanks in advance for your help!

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

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

发布评论

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

评论(2

难以启齿的温柔 2024-08-28 13:32:19

保持简单。

在 HomeView.xib 中,有两个可以加载的不同视图。让 xib 中的 UIViewController 视图成为“容器视图”。在 HomeView 控制器的 viewDidLoad 中执行检测代码,然后添加适当的视图作为子视图。

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (featureWasPaidFor)
    {
        [self.view addSubview:self.homePurchaseView];
    } else {
        [self.view addSubview:self.homeNonPurchaseView];
    }
}

Keep it simple.

In HomeView.xib, have two different views you can load. Have the view of the UIViewController in the xib be the "container view". Do your detection code in the viewDidLoad of your HomeView controller and then add the proper view as a subview.

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (featureWasPaidFor)
    {
        [self.view addSubview:self.homePurchaseView];
    } else {
        [self.view addSubview:self.homeNonPurchaseView];
    }
}
我们只是彼此的过ke 2024-08-28 13:32:19

您可以在 Info.plist 中设置要加载的 xib。在启动时系统将自动加载此 xib。虽然更改 Info.plist 文件中的 xib 名称很容易,但它会被 Apple 拒绝。

这就是我要做的:不要使用 xibs,至少对于你提到的两个。相反,在 applicationDidFinishLaunching:(NSNotification *)notification 中创建所有内容

- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
   UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

   UITabBarController *t = [[UITabBarController alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)];
   // add the other view controllers

   if(/* check for which xib to load */)
   {
      // change whatever you need to change
   }
   else
   {
      // ...
   }

   [window addSubview:[t view]];
   [window makeKeyAndVisible];
}

You set the xib you want to load in your Info.plist. At launch time the system will automatically load this xib. While its easy to change the xib name in the Info.plist file, it'll get rejected by Apple.

Here's what I would do: don't use xibs, at least for the two you mentioned. Instead, create everything in applicationDidFinishLaunching:(NSNotification *)notification

- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
   UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

   UITabBarController *t = [[UITabBarController alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)];
   // add the other view controllers

   if(/* check for which xib to load */)
   {
      // change whatever you need to change
   }
   else
   {
      // ...
   }

   [window addSubview:[t view]];
   [window makeKeyAndVisible];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文