简体   繁体   English

在多个情节提要板中使用相同的UIViewController

[英]Using same UIViewController in Multiple Storyboards

There are three types of users in an iOS app which I am working on these days, the user types are listed below: 目前,我正在使用的iOS应用中有三种类型的用户,下面列出了这些用户类型:

  • Standard User 标准用户
  • Team Leader 队长
  • Board Member 董事会成员

As the functionalities for the users can be different, I think it would make sense to have three Storyboards in the app, each containing its respective flow for the user who is currently logged in. So, the Story Boards will be: 由于用户的功能可以有所不同,因此我认为在应用程序中拥有三个故事板将是有意义的,每个故事板包含针对当前登录用户的各自流程。因此,故事板将为:

  • Standard User Story Board 标准用户故事板
  • Team Leader Story Board 团队负责人故事板
  • Board Member Story Board 董事会成员故事板

So far, so good! 到现在为止还挺好!

Now, I have a TasksViewController which is responsible for displaying tasks specific to user currently logged in, this View Controller is created in XIB. 现在,我有一个TasksViewController,它负责显示特定于当前登录用户的任务,该View Controller是在XIB中创建的。 TasksViewController offers a common functionality and it makes some logical sense to me that I should be able to use TasksViewController in all the Story Boards. TasksViewController提供了一个通用功能,从我的逻辑上来说,我应该能够在所有Story Board中使用TasksViewController。

The questions are: 问题是:

  • Is this a right thing to do? 这是对的事吗?
  • If it is a right thing to do, is it possible to do so? 如果做对了,有可能这样做吗?
  • If its possible, how to implement it? 如果可能,如何实施?
  • Is this a right thing to do? 这是对的事吗?

It depends on your global project. 这取决于您的全球项目。 Keep in mind that what you suggest is possible but can make the code hard to understand/debug . 请记住,您的建议是可能的,会使代码难以理解/调试 Indeed, imagine you push your controller by instantiating it manually from XIB, how to handle segue from this controller (assuming the segue must have a different behaviour depending on the user type)? 确实,想像您通过从XIB手动实例化控制器来推送控制器,如何从该控制器处理segue(假设segue根据用户类型必须具有不同的行为)? Switch the segue depending on the user type? 根据用户类型切换segue? Ugly if too frequent.. 如果太频繁则很难看..

  • Is it possible to do so? 有可能这样做吗? How to implement it? 如何执行呢?

Yes of course. 当然是。 For example, create a storyboard dedicated to shared components and instantiate controllers on-the-fly when needed (snippet written in Swift, methods are the same in Objective-C). 例如,创建一个专用于共享组件的情节提要,并在需要时即时实例化控制器(用Swift编写的代码段,在Objective-C中的方法相同)。

let sharedStoryboard: UIStoryboard = UIStoryboard(name: "SharedStoryboard", bundle: NSBundle.mainBundle())
let sharedController: UIViewController = sharedStoryboard.instantiateViewControllerWithIdentifier("ControllerIdentifier") as! UIViewController
self.navigationController?.presentViewController(sharedController, animated: true, completion: nil)

This will instantiate a controller contained in your shared storyboard and present it to the user. 这将实例化包含在共享情节提要中的控制器,并将其呈现给用户。 This is the old way but when dealing with several storyboards, this is the best solution. 这是旧方法,但是当处理多个情节提要时,这是最佳解决方案。

  • So what to do ? 那么该怎么办 ?

If you just have a few controllers, I'd suggest you replicate it for each user - it will also be easier in the future if you need to add user-type specific features on a controller. 如果您只有几个控制器,建议您为每个用户复制它-如果将来需要在控制器上添加特定用户类型的功能,将来也会更容易。 If a big part of your app is shared, use the way presented above. 如果您的应用程序有很大一部分是共享的,请使用上面介绍的方法。

Hope this helps! 希望这可以帮助!

Yes you can do that. 是的,你可以这么做。 And I think it is good to reuse the same ViewController. 而且我认为最好重用相同的ViewController。 The other way would be to use Storyboard to hold all reusable view controllers and reuse the ViewControllers via identifier. 另一种方法是使用Storyboard来容纳所有可重用的视图控制器,并通过标识符重用ViewController。

Here is the solution with xib: 这是xib的解决方案:

NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"TaskController"
                                                      owner:self
                                                    options:nil];

TaskController *tasks = (TaskController *)[nibViews objectAtIndex:0];

// Do the stuff you want here

For completeness sake, here is reusing view controller from storyboard: 为了完整起见,这里从情节提要中重用视图控制器:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"SomeStoryboard" bundle:[NSBundle mainBundle]];
TaskController *nextOpportunity = [sb instantiateViewControllerWithIdentifier:@"YourIdentifier"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM