せでぃのブログ

ブログ初心者おいどんのどうでもいい愚痴やどうでもいい愚痴やどうでもいいマメ知識などを披露するチラシの裏です。

下に引っ張って更新する奴をObjectiveCで

パクり元はこちらの記事。新し目のコードは、qiitaさんの記事で当たりが多い印象。

Objective-C - iOSで下に引っ張って更新するやつの作り方 - Qiita

 アップロードしたプロジェクトはこちら。
csny/pullbelow · GitHub

完成画像

f:id:Sediment:20150305013107p:plain

作成手順

1.xcodeの初期設定
 SingleViewでObjective-Cを選び、適当に。
f:id:Sediment:20150305011418p:plain
f:id:Sediment:20150305011427p:plain
f:id:Sediment:20150305011851p:plain

2.ストーリーボードにtableview展開
 ストーリーボードを選び、既においてあるViewControllerへ、右ペインからtable viewを持ってきて貼り付ける。
f:id:Sediment:20150305011951p:plain

3.ストリーボードからViewController.mにひも付け
 Controlキーを押しながらドラッグし、tableViewをストーリーボードからViewController.mにひも付けする。名前はtableViewで。
f:id:Sediment:20150305012136p:plain
f:id:Sediment:20150305012256p:plain

4.ViewController.mにコード追加
 下記のコードをViewController.mの@endの1行前あたりにペロッと貼り付け。self.tableViewあたりで3.でつけた名前を使っている。

- (void)refleshControlSetting
{
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self
                       action:@selector(onRefresh:)
             forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:refreshControl];
}

- (void)onRefresh:(UIRefreshControl *)refreshControl
{
    [refreshControl beginRefreshing];
    NSLog(@"更新できた"); // ここの間に更新のロジックを書く
    [refreshControl endRefreshing];
}

5.ViewController.mのviewDidLoad修正
 4.で作ったrefleshControlSettingメソッドの読み込みをviewDidLoadに追加してやる。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self refleshControlSetting];
}

6.ビルド
 かなり引っ張らないとだめなんだなぁ。あと、質素。矢印とか色とか欲しいね。


参考)
 上へ引っ張って更新するものもあるらしい。
iOSで上へ引っ張って更新させるライブラリ - PILOG