MVC 中的 ViewController、XCode 中的 ViewController

发布于 2024-12-08 08:30:57 字数 150 浏览 0 评论 0原文

一个简单的问题,

我最近一直在涉足 XCode,并试图理解视图控制器,虽然我了解了它的本质,但我没有看到的一件事是视图控制器类对象的实例化位置。它本质上是一个类,因此必须实例化一个对象才能向其发送消息。

这让我有点摸不着头脑。

非常感谢!

a quick question,

I have been dabbling with XCode off late and am trying to understand the View Controller, while I get the nitty gritty of it, one thing I fail to see is where the View Controller class object is instantiated. It is, in essence a class and hence has to have an object instantiated to be able to send messages to it.

It's kind of left me scratching my head.

Thanks much!

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

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

发布评论

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

评论(2

揽清风入怀 2024-12-15 08:30:57

每当需要显示时就会实例化它。由你。

例如,如果您想在按下按钮时在导航堆栈上显示新视图?

-(IBAction)buttonClicked:(id)sender{
    /* Create VC here */
    YourViewController *controller = [[YourViewController alloc]initWithNibName:@"ViewName"];

    /* Push */
    [self.navigationController pushViewController:controller animated:YES];

    /* Let go since you don't have control over it anymore. */
    [controller release];
}

我相信在大多数情况下,这样做通常比在内存中保存实例更好,以防止过多的内存使用。

现在(假设 iOS5 已经结束 NDA,大部分内容已在今天公布),您可以使用 XCode 中的 Storyboarding 来为您处理所有这些事情。

It is instantiated whenever it needs to be displayed. By you.

For example, if you wanted to display a new view on the navigation stack on the press of a button?

-(IBAction)buttonClicked:(id)sender{
    /* Create VC here */
    YourViewController *controller = [[YourViewController alloc]initWithNibName:@"ViewName"];

    /* Push */
    [self.navigationController pushViewController:controller animated:YES];

    /* Let go since you don't have control over it anymore. */
    [controller release];
}

I believe it is generally better to do this instead of holding an instance in memory in most situations, to prevent too much memory usage.

Now (assuming iOS5 is out of NDA now that most of it has been announced today), you can use Storyboarding in XCode that will handle all this for you.

关于从前 2024-12-15 08:30:57

视图控制器在其 init 函数指定的初始化程序中实例化,如下所示:

-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
    if( (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) )
    {
         // Custom initialization
    }
    return self;
}

正如您可能在一些 Apple 示例或您浏览过的任何其他源代码中注意到的那样,您可能已经看到了类似于以下的代码行:

MyViewController* viewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];

This is where/当视图控制器被实例化时。您会注意到视图控制器有一个名为 view 的 UIView 类型的成员对象,它被添加到该视图所属的窗口或视图中。
创建视图控制器是为了处理与该视图相关的消息。一切都在此处详细说明。

The view controller is instantiated in it's init function designated initializer being the following:

-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
    if( (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) )
    {
         // Custom initialization
    }
    return self;
}

As you will perhaps have noted in some of Apples examples or any other source code you've looked through you might have seen a line of code similar to

MyViewController* viewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];

This is where/when the view controller gets instantiated. You'll notice that a view controller has a member object of type UIView called view it is this that gets added to the window or view that this view is to be apart of.
The view controller is created to handle messages pertaining to this view. It's all spelled out here.

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