字符串的静态 NSArray - 如何/在哪里初始化视图控制器 [英] Static NSArray of strings - how/where to initialize in a View Controller

查看:38
本文介绍了字符串的静态 NSArray - 如何/在哪里初始化视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Master-Detail 应用程序中,我想显示一个包含 5 个部分的 TableView,标题为:

In a Master-Detail app I'd like to display a TableView with 5 sections titled:

  1. 你的行动
  2. 他们的行动
  3. 赢得比赛
  4. 输掉的比赛
  5. 选项

所以我在 Xcode 5.0.2 中创建了一个空白的 Master-Detail 应用程序,然后在它的 MasterViewController.m(这是一个 UITableViewController)中尝试实现该方法:

So I create a blank Master-Detail app in Xcode 5.0.2 and then in its MasterViewController.m (which is a UITableViewController) I'm trying to implement the method:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return _titles[section];
}

我的问题是如何初始化 NSArray _titles?

My question is however how to init the NSArray _titles?

我正在 MasterViewController.m 中尝试:

I'm trying in the MasterViewController.m:

#import "MasterViewController.h"
#import "DetailViewController.h"

static NSArray *_titles_1 = @[
    @"Your Move",
    @"Their Move",
    @"Won Games",
    @"Lost Games",
    @"Options"
];

@interface MasterViewController () {
    NSMutableArray *_games;

    NSArray *_titles_2 = @[
                         @"Your Move",
                         @"Their Move",
                         @"Won Games",
                         @"Lost Games",
                         @"Options"
    ];
}
@end

@implementation MasterViewController

- (void)awakeFromNib
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.clearsSelectionOnViewWillAppear = NO;
        self.preferredContentSize = CGSizeMake(320.0, 600.0);
    }
    [super awakeFromNib];
}

- (void)viewDidLoad
{
     ....
}

但上面的两次尝试都给我语法错误:

but both tries above give me syntax errors:

更新:

令我惊讶的是,对于这个简单的问题有很多建议,但作为 iOS/Objective-C 新手,我不确定哪种解决方案最合适.

To my surprise there are many suggestions for this simple question, but as an iOS/Objective-C newbie I'm not sure, which solution is most appropriate.

dispatch_once - 这不是在多线程应用程序中执行一次的运行时操作吗?这里是不是太过分了?我期待一个用于启动 const 数组的编译时解决方案...

dispatch_once - isn't it a runtime operation to execute something once in a multi-threaded app? Isn't it overkill here? I was expecting a compile-time solution for initiating a const array...

viewDidLoad - 当我的应用程序在后台和前台之间切换时,是否不需要一次又一次地启动我的 const 数组?

viewDidLoad - when my app changes between background and foreground, wouldn't it unnecessary initiate my const array again and again?

我不应该更好地在 awakeFromNib 中设置 NSArray(因为我的所有 ViewControllers 都使用了 stroyboard 场景)?或者也许在 initSomething (是正确的方法 initWithStyle 吗?)

Shouldn't I better set the NSArray in awakeFromNib (since I use stroyboard scenes for all my ViewControllers)? Or maybe in initSomething (is the correct method initWithStyle?)

推荐答案

编写一个返回数组的类方法.

Write a class method that returns the array.

+ (NSArray *)titles
{
    static NSArray *_titles;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _titles = @[@"Your Move",
                    @"Their Move",
                    @"Won Games",
                    @"Lost Games",
                    @"Options"];
    });
    return _titles;
}

然后您可以在任何需要的地方访问它,如下所示:

Then you can access it wherever needed like so:

NSArray *titles = [[self class] titles];

这篇关于字符串的静态 NSArray - 如何/在哪里初始化视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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