Objective-C:有条件创建对象的优雅解决方案? [英] Objective-C: elegant solution to conditional creation of objects?

查看:42
本文介绍了Objective-C:有条件创建对象的优雅解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个Objective-C/iphone SDK问题.

This is an Objective-C/iphone SDK question.

我有一个仪表板视图,用于在应用程序中切换到不同的视图.本质上,该视图具有六个按钮,这些按钮可快速跳转到另一个视图.

I have a dashboard view that is used to switch to different views in my application. Essentially the view has six buttons that shortcut to a different view.

这是按钮事件处理代码的示例:

Here is an example of the button event handling code:

- (IBAction)moreButtonPressed:(id)sender{
 // switch view here
 if(self._moreViewController == nil){
  MoreViewController *moreViewController = [[MoreViewController alloc] initWithNibName:@"MoreView" bundle:[NSBundle mainBundle]];
  self._moreViewController = moreViewController;
  [moreViewController release];
 }

 [self SwitchView:self._moreViewController];
}

我可以为每个按钮复制此代码并更改适当的类(视图控制器类),但是我希望有一个更优雅的解决方案.

I can duplicate this code for each button and change the appropriate classes (view controller classes)), but I would love a more elegant solution.

有人对如何执行此操作有任何想法吗?

Does anyone have any idea of how to do this?

推荐答案

首先,使用以下代码将NIB的名称移至ViewController类中:

First, move the name of the NIB into the ViewController class with code such as:

- (MoreViewController *)init
{
    self = [super initWithNibName:@"MoreView" bundle:nil];
    if (self != nil)
    {
    }
    return self;
}

这将使您的视图控制器具有独立性,因此您可以干净地创建它们:

This will make your view controllers self-contained so you can create them cleanly:

MoreViewController *moreViewController = [[MoreViewController alloc] init];

无论您做什么,我都建议使用此方法.苹果公司的示例代码无法承受,将NIB的名称放在调用方中是很愚蠢的.为什么每个视图控制器调用者都需要了解此内部实现细节?

I recommend this approach no matter what else you do. Apple's sample code not withstanding, it is silly to put the name of the NIB in the caller. Why should every caller to a view controller need to know this internal implementation detail?

接下来,请记住ObjC是一种动态语言.您可以像这样根据类名称构造对象:

Next, remember that ObjC is a dynamic language. You can construct objects based on their Class name like this:

Class vcClass = NSClassFromString([self.classNameForSender objectForKey:sender])
UIViewController *vc = [[[vcClass alloc] init] autorelease];
[self switchToView:vc];

在上面, -classNameForSender 是一个 NSDictionary ,它将按钮对象映射到类名,但是您当然可以通过许多其他方式获得这些类名.根据发送者的不同,您也许可以将类直接挂在发送者上(尽管对于标准 UIControl s来说这很困难,因为它们只能使用整数 tag ).

In the above, -classNameForSender is an NSDictionary that maps the button objects to the class name, but you can get these class names lots of other ways of course. Depending on the sender, you may be able to hang the class directly on the sender (though this is difficult with standard UIControls because they only have an integer tag to work with).

我没有将您存储的VC复制到ivar中.您真的需要这个吗?如果是这样,则可以按名称将其存储在字典中,也可以将所有VC存储在 NSMutableSet 中.但是在大多数情况下,您实际上并不需要这样做,因为大多数UI控制器(例如NavController)都会为您管理内存.

I haven't duplicated your storing the VC into an ivar. Do you really need this? If so, you can store it in a dictionary by name or you can store all the VCs in an NSMutableSet. But in most cases you really don't need this since most UI controllers (like NavController) will manage memory for you.

另一种较不动态的方法是在开始时创建所有视图控件,并将它们放入字典中,然后您可以根据发送者将它们从字典中加载出来.这样具有更好的编译时检查优势,但会占用更多内存.

Another less dynamic approach is to create all the view controls at the beginning and put them in a dictionary, then you can just load them out of the dictionary based on the sender. This has the advantage of better compile-time checks, but can eat a lot more memory.

这篇关于Objective-C:有条件创建对象的优雅解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆