xcode 如何创建可重用的 iOS 控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6462124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:20:05  来源:igfitidea点击:

How do I create reusable iOS controls?

objective-ciosxcode

提问by ChrisP

As a new iOS developer I am trying to determine the best approach to creating reusable controls in iOS4+. For example if I wanted to create a customized UIImageView that could be used in multiple other views what is the best approach.

作为一名新的 iOS 开发人员,我正在尝试确定在 iOS4+ 中创建可重用控件的最佳方法。例如,如果我想创建一个可以在多个其他视图中使用的自定义 UIImageView,那么最好的方法是什么。

I have created the control including the following files:

我创建了包含以下文件的控件:

  • MyImageViewController.h
  • MyImageViewController.m
  • MyImageViewController.nib
  • MyImageViewController.h
  • MyImageViewController.m
  • MyImageViewController.nib

Assume that I want a control that utilizes a .nib file.

假设我想要一个使用 .nib 文件的控件。

How would I do the following:

我将如何执行以下操作:

  1. Create an instance of the custom control in another view, programmatically and via IB?
  2. How do I structure the custom control so it can expose "events", e.g., ontouch?
  3. How do I structure the custom control so I can call actions/methods, e.g., loadImage()?
  1. 以编程方式和通过 IB 在另一个视图中创建自定义控件的实例?
  2. 我如何构建自定义控件以便它可以公开“事件”,例如,ontouch?
  3. 如何构建自定义控件以便我可以调用操作/方法,例如 loadImage()?

Any insights or reference appreciated. Thanks

任何见解或参考表示赞赏。谢谢

回答by RyanR

You probablywouldn't use a nib/xib to create a reusable view 'control'. iOS is very different than what you may be used to from, say Java or .NET. For some kinds of views, you'll create a xib and controller/class which backs it - UITableViewCell is an example of this.

可能不会使用 nib/xib 来创建可重用的视图“控件”。iOS 与您可能习惯使用的 Java 或 .NET 非常不同。对于某些类型的视图,您将创建一个 xib 和支持它的控制器/类 - UITableViewCell 就是一个例子。

I wish I could give you a step by step guide how to achieve what you're trying to do, but I can't because I don't have enough info on what you are actually trying to do. There are several great guides on the apple developer site to get you acquainted with the View/Controller/Touch event interactions, but the site is not responding right now (I assume because of the release of the WWDC videos). Speaking of, watching videos from WWDC 2010 would be another great way to learn some of what you're trying to achieve, especially the UIKit sessions.

我希望我能给你一个逐步指导如何实现你正在尝试做的事情,但我不能,因为我没有足够的信息来说明你实际尝试做的事情。苹果开发者网站上有几个很棒的指南可以让您熟悉 View/Controller/Touch 事件交互,但该网站现在没有响应(我猜是因为 WWDC 视频的发布)。说起来,观看 WWDC 2010 的视频将是另一种了解您正在努力实现的目标的好方法,尤其是 UIKit 会议。

You aren't going to create 'controls' as you're probably used to them, but custom UIView-descendant objects and custom UIViewController-descendant objects. In my projects, I have found very veryfew scenarios where creating a reusable UIView was of any benefit (other than UITableViewCells). The vast majority of reusable code in an iOS project is ViewControllers. You'll expose events through the use of delegation - your controller will define a delegate which another object will implement, and receive the method calls from your controller to handle whatever was delegated. Again, the Apple guides and WWDC videos explain this very well.

您不会创建“控件”,因为您可能已经习惯了它们,而是创建自定义 UIView 后代对象和自定义 UIViewController 后代对象。在我的项目,我发现非常非常少数场景中创建可重用的UIView是(除了UITableViewCells其他)任何好处。iOS 项目中绝大多数可重用代码是 ViewControllers。您将通过使用委托公开事件 - 您的控制器将定义另一个对象将实现的委托,并从您的控制器接收方法调用以处理委托的任何内容。同样,Apple 指南和 WWDC 视频很好地解释了这一点。

Here's an example of a reusable controller and it's usage:

这是可重用控制器的示例及其用法:

MyEntityViewController.m

MyEntityViewController.m

- (id)initWithEntity:(EntityFoo *)theEntity inEditMode:(BOOL)inEditMode {
  self = [super initWithNibNamed:@"EntityFooView" bundle:nil];
  if(self) {
    //retain entity somewhere
    //dont setup view in here!
  }
  return self;
}

- (void)viewDidLoad {
  //setup view in here. This ensures compatibility with standalone operation
}

RootViewController.m

根视图控制器.m

- (void)viewDidLoad {
  [super viewDidLoad];

  EntityViewController *evc = [[EntityViewController alloc] initWithEntity:self.someEntity inEditMode:NO];
  [self.view addSubview:evc.view];

  //You MUST call this method, the framework will not call it on sub-controllers
  [evc viewDidLoad];

  self.entityViewController = evc;
  [evc release];
}

This is not a complete implementation, as your experience and framework grows, you'll find a pattern of notifying (hint hint) sub-viewcontrollers of events in the main view controller. I strongly encourage you to look at the WWDC 2011 videos if you have access to them, as there is something coming in the next OS which has bearing on this subject matter.

这不是一个完整的实现,随着您的经验和框架的增长,您会在主视图控制器中找到一种通知(提示提示)子视图控制器事件的​​模式。如果您可以访问 WWDC 2011 视频,我强烈建议您查看它们,因为下一个操作系统中将有一些与此主题相关的内容。

回答by jlstrecker

Here's an articlethat describes how to create a reusable control with a nib. The section called "A reusable subview" sounds like what you want.

这是一篇介绍如何使用笔尖创建可重用控件的文章。名为“可重用子视图”的部分听起来像您想要的。

Contrary to what @RyanR said, it's perfectly fine to create a reusable control in iOS. It's not a pattern I've seen a lot, but you can totally do it.

与@RyanR 所说的相反,在 iOS 中创建可重用控件完全没问题。这不是我见过的很多模式,但你完全可以做到。

回答by Maciej

This is my version for storyboards.

这是我的故事板版本。

Add view controller id:

添加视图控制器 ID:

enter image description here

在此处输入图片说明

in .h file create a property to keep the controller. Otherwise it will be removed from the memory:

在 .h 文件中创建一个属性来保留控制器。否则将从内存中删除:

@property (nonatomic, strong) TestViewController *timetableViewController;

in .m file:

在 .m 文件中:

-(void)viewDidLoad{
    [super viewDidLoad];
    TestViewController *centerViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"VIEW_CONTROLLER_ID"];
    [self.view addSubview:centerViewController.view];
    [centerViewController viewDidLoad];
    self.timetableViewController = centerViewController;

}