在没有界面生成器的情况下使用 UIViewController 和 UIViews 切换视图的最佳方法是什么?

发布于 2024-11-06 21:55:53 字数 324 浏览 0 评论 0原文

嘿伙计们,我想到了几个解决方案,但对它们不太满意,所以需要一些想法。

我有:

  • 3 个 UIView 实例 view1、view2、view3
  • 1 个 UIViewController 实例,UIViewController1 存储 view1、view2 和 view3

我想要:

  • 在 view1 中放置一个方法,告诉 UIViewController1 切换到 view2。

我是否需要在 view1 中传递对 UIViewController1 的引用?这是唯一正确的方法吗?

Hey guys ive thought of a couple solutions but not really happy with them so need some ideas please.

I have:

  • 3 UIView instances view1, view2, view3
  • 1 UIViewController instance, UIViewController1 that stores view1,view2 and view3

I want:

  • to put a method in view1 that will tell UIViewController1 to switch to view2.

Do I need to put a pass a reference to UIViewController1 in view1? would this be the only correct way?

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

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

发布评论

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

评论(2

烟酉 2024-11-13 21:55:53

首先,这些是全屏视图吗?如果是这样,您应该考虑拥有 3 个视图控制器,每个视图一个,也许将它们安装在 UITabBarController 中,这与您描述的行为类似。但无论如何:

不,视图不应该告诉控制器做任何事情。

在 MVC 架构中,视图呈现信息并接收(某些)用户交互。控制者有责任决定如何处理该操作。

视图所做的作用是告诉控制器交互已经发生。

因此,您面临的主要问题是 - 什么事件会产生这种行为?

它是一个按钮吗?按钮(UIControl 的子类)使用目标操作模式来处理此通信。您的视图应该提供对按钮的引用:

@property (nonatomic, retain) UIButton *switcherButton;

接下来,在视图控制器中您将实现一个方法:

-(void)viewSwitcherButtonTapped:(id)sender;

这就是您的视图如何告诉控制器发生了 UI 事件。这就是它应该做的。由控制器根据模型中的状态信息,或者根据其自身的内部状态或逻辑来决定点击该按钮时应该发生什么。

最后,创建视图后,控制器会将自身连接到按钮:

[view.switcherButton addTarget:self action:@selector(viewSwitcherButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

这样任何视图控制器都可以使用此视图,并且视图不依赖于对控制器的引用(因为它不应该)。

但按钮只是一个示例,如果生成行为的事件不同,您的实现可能会有所不同。回答这个问题,也许我可以修改这个答案来应用。

您应该看看

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html

First off, are these full screen views? If so you should consider having 3 view controllers, one for each view and perhaps installing them in a UITabBarController, which is similar to the behavior you described. But in any case:

No, a view should not tell a controller to do anything.

In an MVC architecture, views present information and receive (some) user interaction. It is the controller's responsibility to decide what to do with that action.

What the view does do is tell the controller that an interaction has occurred.

So the main question for you is - what event would be generating this behavior?

Is it a button? Buttons (subclasses of UIControl) use a target-action pattern to handle this communication. Your view should provide a reference to the button:

@property (nonatomic, retain) UIButton *switcherButton;

Next, in the view controller you would implement a method:

-(void)viewSwitcherButtonTapped:(id)sender;

This is how your view can tell the controller that a UI event occurred. That's all it should do. It is up to the controller to decide, based on state information in the Model, or based on its own internal state or logic what should happen when that button is tapped.

And finally, after the view is created, the controller would hook itself up to the button:

[view.switcherButton addTarget:self action:@selector(viewSwitcherButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

This way any view controller can use this view and the view does not depend on a reference to the controller (as it shouldn't).

But the button is just one example and your implementation might be different if the event that generates the behavior is different. Answer that question and maybe I can amend this answer to apply.

You should take a look at

"The Model-View-Controller Design Pattern", "The Target-Action Mechanism" and "Delegation" in http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html

星星的軌跡 2024-11-13 21:55:53

你有两个选择:

快速和肮脏的一个:只需在每个视图中声明一个指向 UIViewController 的属性,将其定义为分配以避免循环引用(我假设你在视图控制器中保留视图)。然后您可以通过此属性调用 uiviewcontroller 上的方法,

这是一个不错的方法:让每个视图广播一个通知,uiviewcontroller 注册此通知并在其回调中执行它需要执行的操作。

You have 2 options:

the quick and dirty one: just declare a property in each view that points to the UIViewController, define it as assign to avoid circular references (i presume you retain the views in your viewcontroller). then you can call the methods on uiviewcontroller via this property

the nice one: have each view broadcast a notification, the uiviewcontroller registers for this notification and in its callback does what it needs to do.

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