在 IOS 编程中什么时候应该使用 UIViewController?

发布于 2024-09-24 13:39:46 字数 708 浏览 1 评论 0原文

我一直在研究 IOS 编程的 API,并且一直在阅读有关视图控制器和 UIView 的内容。看起来 UIViewController 子类对于模态导航和自定义动画确实很有用,但我看不到除此之外的任何其他用途。

使用 UIViewController 子类而不是普通的 NSObject 子类有什么好处?

为什么

@interface MyViewController : UIViewController {
}

-(void)handleEvent;

@end

你不直接

@interface MyViewController : NSObject {
    UIView* view;
}

@property(retain) UIView* view;
-(void)handleEvent;

@end

将视图添加到窗口,而不是实际的 viewController 本身? 对于大多数用途,您需要的所有功能不是都封装在 UIView 对象中吗? 您最终只需像这样添加它:

[window addSubview:myViewControllerInstance.view]

除了像模态导航这样的内置功能之外,UIViewController 还有其他用途吗?

谢谢。

(抱歉,如果这是一个愚蠢的问题,我已经学了两天了)

I've been looking at the API for IOS Programming, and have been reading about view controllers and UIViews. It looks like UIViewController subclasses are really useful for Modal navigation and custom animation, but I can't see any other uses than that.

What is the benefit to using a UIViewController subclasses rather than a normal NSObject subclass?

Why

@interface MyViewController : UIViewController {
}

-(void)handleEvent;

@end

Instead of just

@interface MyViewController : NSObject {
    UIView* view;
}

@property(retain) UIView* view;
-(void)handleEvent;

@end

Don't you just end up adding just the view to the window, not the actual viewController itself?
For most purposes, isnt all of the functionality you need encapsulated within the UIView object?
You just end up adding it like this:

[window addSubview:myViewControllerInstance.view]

Is there a use for UIViewController other than built in functionality like Modal Navigation?

Thanks.

(Sorry if this is a stupid question, I've been learning this for 2 days now)

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

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

发布评论

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

评论(2

萌化 2024-10-01 13:39:46

Mac OS 和 iOS 上的 Cocoa 大量使用 模型-视图-控制器 (MVC) 模式。遵循 MVC 模型,UIViewController 是一个 Controller 类。它的工作是协调用户界面(视图对象)和应用程序数据(模型对象)之间的交互。更基本的是,控制器主要是放置应用程序逻辑的地方。它处理事件并相应地调用视图和模型。

请参阅 UIViewController 参考,其中对班级有很好的概述。

通过从 UIViewController 派生,您可以免费获得大量 Controller 功能,例如从 Nib 文件加载视图 (initWithNibName:bundle:) 以及响应与视图相关的事件(viewWillAppear:、viewWillDisappear:)。此外, UIViewController 本身就是 UIResponder,包含处理触摸事件的功能(touchesBegan:withEvent、touchesMoved:withEvent、touchesEnded:withEvent)。

基本上,没有理由不使用 UIViewController 及其提供的所有功能。即使你能设法做到这一点,也会付出更多的努力,而且没有任何实际好处。

Cocoa on Mac OS and iOS make heavy use of the Model-View-Controller (MVC) pattern. Following the MVC model, UIViewController is a Controller class. Its job is to coordinate interactions between your user interface (View objects) and your application data (Model objects). More basically, the Controller is primarily where you place your application's logic. It handles events and calls upon the view and model accordingly.

See the UIViewController reference, which has a nice overview on the class.

By deriving from UIViewController, you get a bunch of Controller functionality for free, such as loading views from a Nib file (initWithNibName:bundle:) and responding to view-related events (viewWillAppear:, viewWillDisappear:). Additionally, UIViewController is itself a subclass of UIResponder, which contains functionality for handling touch events (touchesBegan:withEvent, touchesMoved:withEvent, touchesEnded:withEvent).

Basically, there's no reason NOT to use UIViewController with all the functionality it provides. Even if you could manage to to do so, it would be way more work, for no real benefit.

流年里的时光 2024-10-01 13:39:46

看一下 UIViewController 类的属性和实例方法,如果您只是子类化 NSObject,则无法免费获得它们。其中有很多东西您将在所有应用程序中使用。

Take a look at the properties and instance methods of the UIViewController Class, which you would not get for free if you just subclassed NSObject. There is a lot of stuff in there that you will use in all of your applications.

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