Ios 如何创建可由多个UIVIewController共享/使用的UIView?
我希望以Ios 如何创建可由多个UIVIewController共享/使用的UIView?,ios,objective-c,uiview,storyboard,Ios,Objective C,Uiview,Storyboard,我希望以UIView的形式构建自定义错误消息。我想为我的应用程序中的每个UIViewController使用此UIView。显然,我不想为每个屏幕重新创建它,所以我正在寻找“共享”它的最佳解决方案 将它创建为一个UIViewController并以这种方式添加是更好的方法还是有更好的方法?UIView实例只用于一个地方。它们只能有一个superview,因此不能同时在多个视图层次结构中使用 如果更改视图的superview,实际上会移动视图 我认为有两种可能性: 或者您希望始终在屏幕上显示此错
UIView
的形式构建自定义错误消息。我想为我的应用程序中的每个UIViewController
使用此UIView
。显然,我不想为每个屏幕重新创建它,所以我正在寻找“共享”它的最佳解决方案
将它创建为一个
UIViewController
并以这种方式添加是更好的方法还是有更好的方法?UIView实例只用于一个地方。它们只能有一个superview
,因此不能同时在多个视图层次结构中使用
如果更改视图的superview
,实际上会移动视图
我认为有两种可能性:
- 或者您希望始终在屏幕上显示此错误消息。在这种情况下,您可能应该像您提到的那样创建一个专用的UIViewController,并始终在屏幕上显示它
- 或者您希望显示不同的错误。这样,为每个显示错误的控制器创建UIView实例是有意义的
superview
,因此不能同时在多个视图层次结构中使用
如果更改视图的superview
,实际上会移动视图
我认为有两种可能性:
- 或者您希望始终在屏幕上显示此错误消息。在这种情况下,您可能应该像您提到的那样创建一个专用的UIViewController,并始终在屏幕上显示它
- 或者您希望显示不同的错误。这样,为每个显示错误的控制器创建UIView实例是有意义的
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 {}
...
是的,使用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 {}
...
对于这个问题,我希望您创建一个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];
对于这个问题,我希望您创建一个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];
若你们想要一个在那个时总是出现在屏幕上的视图,你们可以使用窗口来显示一个视图 窗口是屏幕上显示的所有视图的根,窗口始终存在,所以一旦在窗口中添加视图,它将始终存在于屏幕中 只需使用下面的代码将视图添加到窗口
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);
若你们想要一个在那个时总是出现在屏幕上的视图,你们可以使用窗口来显示一个视图 窗口是屏幕上显示的所有视图的根,窗口始终存在,所以一旦在窗口中添加视图,它将始终存在于屏幕中 只需使用下面的代码将视图添加到窗口
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);
是的,您可以使用singletone方法创建自定义视图的共享/单个实例,并且可以在所有UIViewController@vivekDas从应用程序架构的角度来看,这是一个糟糕的建议,更不用说容易出错和违反几乎所有的UI设计原则了。你怎么能说这很糟糕呢,这取决于使用情况,如果你不能使用它,这是你的问题,没有这样的限制@mag_zbcb基于下面的评论,我沿着UIViewController路线,通过一个segue将其作为popover打开,并传递错误消息。谢谢。是的,您可以使用singletone方法创建自定义视图的共享/单个实例,并且可以在所有UIViewController@vivekDas从应用程序架构的角度来看,这是一个糟糕的建议,更不用说容易出错和违反几乎所有的UI设计原则了。你怎么能说这很糟糕呢,这取决于使用情况,如果你不能使用它,这是你的问题,没有这样的限制@mag_zbcb基于下面的评论,我沿着UIViewController路线,通过一个segue将其作为popover打开,并传递错误消息。谢谢大家