iOS 子视图、SRP 和自定义事件

发布于 2024-12-20 15:24:28 字数 325 浏览 3 评论 0原文

我是 iOS 开发新手,需要一些建议。我有一个类似聊天的应用程序。 UI 应该有一个用于将新消息发布到服务器的子视图和一个用于在表视图中查看消息的子视图。

我已在 Interface Builder 中将两个子视图构建为 XIB:s。但我不确定如何在主视图控制器上使用它们。我可以使用 IB 将自定义视图添加到设计图面吗?或者我需要以编程方式添加这些?

在这两个子视图之间发送消息或自定义事件的最佳方式是什么?我想让它们尽可能地分离。大多数情况下,我想在用户登录或注销时发送一个事件,以便 UI 可以对这些更改做出反应。我还希望带有消息的表视图知道何时从写入视图发布新消息。

// 约翰

I'm kind of new to iOS development and need some advice. I have a chat like app. The UI should have a child view for posting new messages to the server and one child view for viewing messages in a table view.

I've build both child views in Interface Builder as XIB:s. But I'm not sure how to use these on the main view controller. Can I use IB to add my custom views to the design surface? Or do I need to add these programmatically?

What is the best way to send messages or custom events between these two child views? I'd like to have them as decoupled as possible. Mostly I'd like to send an event when the user log on or off, so that the UI can react to these changes. I'd also like the table view with messages to know when a new messages is posted from the write view.

// Johan

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

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

发布评论

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

评论(1

老子叫无熙 2024-12-27 15:24:28

为了获取 xib 文件的内容,您必须首先向 NSBundle 类发送 loadNibNamed:owner:options: 消息来加载它。

假设您有一个名为 CustomView 的 UIView 子类和 CustomView.xib 文件。在 xib 文件中,每个视图都有一个标签。您的 .h 文件如下所示:

@interface CustomView : UIView

@property (nonatomic, assign) UILabel *someTextLabel; //use assign in order to not to override dealloc method

@end

.m
@implementation CustomView

- (id)init {
  self = [super init];
  if (self) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil options:nil];
    [self addSubview:[topLevelObjects objectAtIndex:0]]; //this object is a CustomView.xib view
    self.someTextLabel = (UILabel *)[self viewWithTag:5]; //consider you have a UILabel on CustomView.xib that has its tag set to 5
  }
  return self;
}

@end

这是关于如何将 .xibs 用于您的自定义 UIView 子类。如果您的应用程序类似于聊天应用程序,那么您必须以编程方式添加它们。

至于在两个自定义视图之间发送消息的最佳方式,您必须在每个视图中创建彼此的弱引用。

在一个

@property (nonatomic, assign) CustomView *customView;

另一个

@property (nonatomic, assign) AnotherCustomView *anotherCustomView;

中,甚至在发生某些情况时向他们发送消息,

- (void)buttonPressed {
  [customView handleButtonPressedEvent];
}

请告诉我这是否清楚。

In order to get the contents of a xib file you've got to load it first sending loadNibNamed:owner:options: message to NSBundle class.

Consider you have a UIView subclass named CustomView and CustomView.xib file. In the xib file each view has a tag. Your .h file would look like:

@interface CustomView : UIView

@property (nonatomic, assign) UILabel *someTextLabel; //use assign in order to not to override dealloc method

@end

.m
@implementation CustomView

- (id)init {
  self = [super init];
  if (self) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil options:nil];
    [self addSubview:[topLevelObjects objectAtIndex:0]]; //this object is a CustomView.xib view
    self.someTextLabel = (UILabel *)[self viewWithTag:5]; //consider you have a UILabel on CustomView.xib that has its tag set to 5
  }
  return self;
}

@end

This is about how to use .xibs for your custom UIView subclasses. If your app is like a chat then you'll have to add them programmatically.

As for the best way to send messages between two custom views, you'll have to create a weak reference to each other in each of them.

in one

@property (nonatomic, assign) CustomView *customView;

in another

@property (nonatomic, assign) AnotherCustomView *anotherCustomView;

and just send them messages when some even happens

- (void)buttonPressed {
  [customView handleButtonPressedEvent];
}

Let me know if this is clear.

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