简体   繁体   English

ios超级视图和子视图

[英]ios superview and subview

I have a superview and I add a subview to make a selection. 我有一个超级视图,并添加了一个子视图进行选择。 In the superview (main view) I do the following: [self.view addSubview:cityViewController.view]; 在超级视图(主视图)中,我执行以下操作:[self.view addSubview:cityViewController.view];

In the cityView, when I have done what I need to do, I just do self.view removeFromSuperView. 在cityView中,当我完成需要做的事情后,我只做self.view removeFromSuperView。

The question is, from within the superview, how can I tell when the subview has removed itself. 问题是,从超级视图内部,我如何知道子视图何时已将其自身移除。

There's a few ways, but honestly since the current view controller (let's call it main) is just adding the cityViewController 's view, keep the handling of adding/removing the views to the current view controller, and just have the main controller call [cityViewController.view removeFromSuperView] 有几种方法,但是说实话,由于当前视图控制器(我们称其为main)只是添加cityViewController的视图,保留将视图添加/删除到当前视图控制器的处理,并且只需要主控制器调用[cityViewController.view removeFromSuperView]

This way you can execute whatever code you want when it receives this notification (be it a method that fires or a UINotification ). 这样,您可以在收到此通知时执行所需的任何代码(无论是触发方法还是UINotification )。

-- edit for sample UINotification code -- -编辑示例UINotification代码-

main.m 主目录

...
//Define cityViewController as an iVar and alloc/init it
[[UINotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishView:) name:@"DidFinishView" object:nil];
[self.view addSubview:cityViewController.view];
...

-(void) didFinishView:(NSNotification *)notification {
    [cityViewController.view removeFromSuperView];
}

CityViewController.m CityViewController.m

-(IBAction) doneButtonClick:(id) sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DidFinishView" object:nil];
}

The quick answer is your view should not be removing itself. 快速的答案是您的观点不应该被删除。 It's better practice for a view to communicate user interactions to a relevant controller through an interobject communication mechanism. 通过对象间通信机制将视图中的用户交互信息传达给相关控制器是一种更好的做法。 The most common methods are direct messaging, protocols and notifications. 最常见的方法是直接消息传递,协议和通知。 The iOS framework uses all of these and there are great docs explaining them. iOS框架使用了所有这些,并且有很多出色的文档对其进行了解释。 Here's a brief summary: 这是一个简短的摘要:

  • Direct messaging. 直接消息传递。 Use this when an object needs to communicate with a specific object of a known type. 当对象需要与已知类型的特定对象通信时,请使用此选项。 For example, if MyView is always contained in MyViewController and needs to send messages to it you can add a property to the MyView class that keeps a pointer to the specific MyViewController object. 例如,如果MyView始终包含在MyViewController并且需要向其发送消息,则可以向MyView类添加一个属性,该属性保留指向特定MyViewController对象的指针。 You can then send a message from myView to it's myViewController via [myView.myViewController userDidTapSaveButton] or whatever. 然后,您可以通过[myView.myViewController userDidTapSaveButton]或其他方式将消息从myView发送到myViewController

  • Protocols. 协议。 A protocol defines a contract between objects that don't know anything about each other other than that they abide by the contract. 协议定义了彼此之间不了解彼此的对象之间的契约,除非它们遵守契约。 For example, UITableView knows that it's delegate conforms to the UITableViewDelegate protocol and it can send the required protocol messages to it's delegate. 例如, UITableView知道它的委托符合UITableViewDelegate协议,并且可以向其委托发送所需的协议消息。 Any object can conform to the UITableViewDelegate protocol. 任何对象都可以符合UITableViewDelegate协议。

  • Notifications. 通知。 Notifications allows an object to post notifications through a central mechanism ( NSNotificationCenter ) that other objects can observe and respond to. 通知允许对象通过中央机制( NSNotificationCenter )发布通知,其他对象可以观察和响应。 Notifications are useful when the object posting the notification doesn't know or care what objects are observing it's notifications. 当发布通知的对象不知道或不在意哪些对象正在观察其通知时,通知非常有用。

I'd read the relevant docs and other Q&A on SO about these methods. 我已经阅读了有关这些方法的相关文档以及关于SO的其他问答。 I'd also study up a bit on the MVC (Model/View/Controller) design pattern so you get more comfortable knowing where to put app logic. 我还将对MVC(模型/视图/控制器)设计模式进行一些研究,以使您更轻松地知道将应用程序逻辑放在哪里。 Generally, a view should only be responsible for it's display (based on properties set by it's controller), observing/responding to user actions, and notifying it's controller for relevant actions. 通常,视图仅应负责显示(基于其控制器设置的属性),观察/响应用户操作并通知其控制器进行相关操作。

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

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