【问题标题】:How to create a UIView that can be shared/used by multiple UIVIewController?如何创建一个可以被多个 UIVIewController 共享/使用的 UIView?
【发布时间】:2019-01-11 03:42:49
【问题描述】:

我希望以UIView 的形式构建自定义错误消息。我想在我的应用程序中为每个UIViewController 使用这个UIView。显然我不想为每个屏幕重新创建它,所以我正在寻找“共享”它的最佳解决方案。

将其创建为UIViewController 并以这种方式添加会更好还是有更好的方法?

【问题讨论】:

  • 是的,您可以使用单色调方法创建自定义视图的共享/单个实例,并且可以在所有 UIViewController 中使用它
  • @vivekDas 从应用架构的角度来看这是一个糟糕的建议,更不用说容易出错并且违反几乎所有 UI 设计原则
  • 怎么能说它很糟糕,这取决于使用情况如果你不能使用它是你的问题,没有这样的限制@mag_zbc
  • 基于下面的 cmets,我沿着 UIViewController 路线走下去,我通过 segue 将它作为弹出窗口打开,然后通过错误消息。谢谢大家

标签: ios objective-c uiview storyboard


【解决方案1】:

UIView 实例只能在一个地方使用。它们只能有一个superview,因此不能同时在多个视图层次结构中使用。

如果您更改视图的superview,您实际上会移动该视图。

我认为有两种可能:

  • 要么您希望始终在屏幕上显示此错误消息。在这种情况下,您可能应该像您提到的那样创建一个专用的 UIViewController,并始终将其显示在屏幕上,
  • 或者您想显示不同的错误。这样,为显示错误的每个控制器创建一个 UIView 实例是有意义的。

【讨论】:

    【解决方案2】:

    是的,使用 ViewController,它比简单的视图更好。您的消息可能包含按钮等操作。

    为避免过多的代码重复,您可以使用 mixin :

    protocol ErrorDisplay {
        func display(_ error: Error)
    }
    
    extension ErrorDisplay where Self: UIViewController {
        func display(_ error: Error) {
            let viewController = ErrorViewController(error: error)
            // You could also add it as a child view controller rather than presenting it, 
            // if you want to add its view in the current hierarchy
            present(viewController, animated: true, completion: nil)
        }
    }
    

    并在每个希望显示错误的视图控制器中实现协议。

    extension LoginViewController : ErrorDisplay {}
    extension SettingsViewController : ErrorDisplay {}
    ...
    

    【讨论】:

      【解决方案3】:

      如果您想要一个始终显示在屏幕上的视图,您可以使用 window 来显示视图。

      窗口是屏幕上显示的所有视图的根,窗口始终存在,因此一旦您将视图添加到将始终存在于屏幕中的窗口中。

      只需使用以下代码将视图添加到窗口

      let window = UIApplication.shared.keyWindow!
      let v = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
      window.addSubview(v);
      

      【讨论】:

        【解决方案4】:

        对于这个问题,我更喜欢你创建一个 UIView 类,你可以在你的 viewController 中重用它。为此,请按照以下步骤操作,

        1. Create a Xib of UIView and design it what ever you want.
        2. Create a UIView class
        3. Change the fileOwner of Your UIView XIB to the created UIView class
        4.  Create an Outlet of ContentView in YourView.h
         @property (strong, nonatomic) IBOutlet UIView *contentView;
        5. Add the following codes in YouView.m
        
         -(instancetype)initWithCoder:(NSCoder *)aDecoder
        {
            self = [super initWithCoder:aDecoder];
            if(self)
            {
                [self customInit];
            }
            return self;
        }
        
        -(instancetype)initWithFrame:(CGRect)frame
        {
            self = [super initWithFrame:frame];
            if(self)
            {
                [self customInit];
            }
            return  self;
        }
        
        -(void) customInit
        {
            [[NSBundle mainBundle]loadNibNamed:@"View" owner:self options:nil];
            [self addSubview:self.contentView];
            self.contentView.frame = self.bounds;
        }
        
        6. Use this UIView in your ViewController what ever you want.
        
        YourView *YourViewObj = [[YourView alloc] initWithFrame:CGRectMake(0, 0, 100,100)];
        [self.view addSubview:YourViewObj];
        

        【讨论】:

          猜你喜欢
          • 2012-03-27
          • 1970-01-01
          • 2023-03-23
          • 1970-01-01
          • 2019-06-15
          • 1970-01-01
          • 1970-01-01
          • 2022-12-03
          • 2012-08-29
          相关资源
          最近更新 更多