[Xcode][UI][iOS] XcodeでStoryboardのUser Defined Runtime Attributes使い方わかった

StoryBoardには、実行時、プロパティに値を設定できる機能があります。

例えばアニメーションやサイズの動的な変更でこれを使用すれば
最終調整で、プログラムでわざわざ値を修正して微調整するとかしなくてもよくなります。

環境

手順

UITableView Cellでパフォーマンス向上のためにCellを再利用する機能がありますが、
そこで使用するIDをUser Defined Runtime Attributeで宣言してみます。

  1. ViewControllerにプロパティを宣言
  2. Storyboardの画面を開いてプロパティを宣言したViewControllerを選択
  3. Utility areaの「Show the identity inspector」タブを選択
  4. [+]をクリックして、必要なAttributeを追加



実行結果


使用場面

Storyboard経由でViewControllerを呼び出すとき、`initWithNibName:bundle:`とか呼ばれないので、Viewの細かいサイズ変更などコードで行いたいが、プロパティのデフォルト値として定数値を設定したい場合。
`viewDidLoad`で毎回プロパティに値をセットするのはちょっと違うなというとき

例えば、独自のカスタムフォントを適用したが、Storyboard上では適用できないが、フォント名をプロパティのデフォルト値としてあらかじめ設定しときたいとか需要があると思います。

Sample Code

    // MasterViewController.h
    #import <UIKit/UIKit.h>

    @interface MasterViewController : UITableViewController

    @property (strong, nonatomic) NSString *cellIdentifier;
    @property (strong, nonatomic) NSArray *cellTitleList;

    @end

    // MasterViewController.m
    #import "MasterViewController.h"

    @implementation MasterViewController


    @synthesize cellIdentifier = _cellIdentifier;
    @synthesize cellTitleList = _cellTitleList;

    [...]
#pragma mark - View lifecycle

    - (void)viewDidLoad
    {
        [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
        self.cellTitleList = [NSArray arrayWithObjects:@"title1", @"title2", @"title3", nil];
    }

    [...]
    #pragma mark - UITableViewDataSource

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [self.cellTitleList count];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.cellIdentifier];
        }
        cell.textLabel.text = [self.cellTitleList objectAtIndex:indexPath.row];
        return cell;
    }