导航视图控制器问题
以下是我想要实现的内容:
我的应用程序的主屏幕是 UITableView。 表视图中的每一行都是一个类别,当您单击该行中的详细信息公开按钮时,您可以在类别详细视图中看到该类别下的一堆项目。
现在在主屏幕中,我单击导航栏中的“+”按钮来创建一个新类别。 (新类别将成为表视图中的新行)。 然后应用程序将我带到“添加类别”视图。 (我使用了presentModalViewController)
在“添加类别”视图中,我设置了一些内容,然后单击“保存”按钮以关闭“添加类别”视图。 (我使用了missModalViewControllerAnimated)
通常在我单击“保存”按钮后,应用程序会将我带回主视图,我会在表格中看到一个新行。
但这不是我想要的,我想要的是 - 单击“保存”按钮后,“添加类别”视图将被关闭,但不会返回到主视图。 相反,我将看到新创建的类别的详细信息,以便我可以继续在此类别下添加项目。 结果就像“我返回主视图,然后单击新创建的行(类别)的详细信息公开按钮”。
有人知道如何实现这一点吗? 谢谢!
Below is what I want to implement:
The main screen of my app is a UITableView. Each row in the table view is a category, when you click the detail disclosure button in the row, you can see a bunch of items under this category in the category detail view.
Now in the main screen, I click the "+" button in navigation bar to create a new category. (The new category will become a new row in the table view). The app then take me to the "Add Category" view. (I used presentModalViewController)
In the "Add Category" view, I set something, then click "Save" button to dismiss the "Add Category" view. (I used dismissModalViewControllerAnimated)
Usually after I click "Save" button, the app will take me back to the main view and I will see a new row in the table.
But that's not what I want to go, what I want is - after I click the "save" button, the "Add category" view will be dismissed but not return to the main view. Instead, I will see the details of the new-created category so I can continue to add items under this category. The result is just like "I return to the main view and then click the detail disclosure button of the new-created row (category)".
Does any one know how to realize that? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用
presentModalViewController
及其相应的dismissModalViewControllerAnimated
,那么您将返回到发出初始presentModalViewController
消息的视图控制器。相反,您可能希望将负责添加新类别的视图控制器推入堆栈,完成后,您只需将负责显示该类别的所有项目的视图控制器推入堆栈即可。 因此,您应该使用
If you use
presentModalViewController
and its correspondingdismissModalViewControllerAnimated
, then you will return to the view controller in which you issued the initialpresentModalViewController
message.Instead, you may want to push on the stack the view controller in charge of adding the new category, and when you are done, you simply push on the stack the view controller responsible for showing all of the items of that category. Thus, you should use
您可以通过将一个屏幕与另一个屏幕进行一些解耦来做到这一点:
- 为您的类别创建模式对话框创建自定义委托和协议。 简单的事情如下:
- 在模式对话框中,当用户点击“保存”按钮时,在关闭视图控制器后,您调用委托方法:
此外,还有类似于“取消”按钮的方法。
- 您的主控制器实现
categoryAddDone
方法并将其自身设置为模式对话框的categoryDelegate
。- 在运行时,当用户点击
Save
时,将调用委托方法,以便您的主视图收到发生了某些事情的通知,并且它可以将正确的视图推入到位,甚至跳转到正确的类别。- 在您的情况下,一旦类别创建完成,主视图控制器就会收到通知,因此它可以释放类别创建对话框并将类别详细信息视图推入堆栈。 用户看到模式对话框消失并直接滑入详细视图。
- 一般来说,使用委托/协议进行推送导航控制器和模式对话框是制作解耦和可重用视图的一种非常方便的模式。 这样就可以从不同的地方调用它们。 为了保持一致,您可能还希望在每个模式对话框上都有一个
show
方法,并推送调用者可以调用的视图控制器。 这样,就可以以一致的方式进入,并以一致的方式通知用户已完成操作。You can do this by doing a little decoupling of one screen from another:
- Create a custom delegate and protocol for your category creation modal dialog. Something simple like:
- In the modal dialog when the user taps the 'Save' button, after dismissing the view controller, you invoke the delegate method:
Also, something similar for the
Cancel
button.- Your main controller implements the
categoryAddDone
method and sets itself to be thecategoryDelegate
for the modal dialog.- At runtime, when the user taps
Save
the delegate method is invoked so your main view is notified that something has happened and it can push the right view into place and even jump to the proper category.- In your case, as soon as the category creation is done, the main view controller is notified, so it can release the category creation dialog and push the category detail view into the stack. The user sees the modal dialog disappear and slides right into the detail view.
- In general, using delegate/protocols for push navcontroller and modal dialogs is a really handy pattern for making decoupled and reusable views. This way they can be invoked from a variety of places. To make it consistent, you may also want to have a
show
method on each modal dialogs and pushed view controllers that the caller can invoke. This way there's a consistent way to get into and a consistent way to get notified that the user is done with it.