忍者ブログ

Home > > IB不使用&オープンソースなiPadアプリ(テキストエディタ)を作る 第5回:Undo・Redoボタン

[PR]

  • 2024-04-19

Share on Tumblr このエントリーをはてなブックマークに追加

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

Comments:

Trackback+Pingback:

Listed below are links to weblogs that reference
[PR] from Born Neet

Home > > IB不使用&オープンソースなiPadアプリ(テキストエディタ)を作る 第5回:Undo・Redoボタン

Home > Edhita > IB不使用&オープンソースなiPadアプリ(テキストエディタ)を作る 第5回:Undo・Redoボタン

IB不使用&オープンソースなiPadアプリ(テキストエディタ)を作る 第5回:Undo・Redoボタン

Share on Tumblr このエントリーをはてなブックマークに追加

続けざまに第5回。
簡単そうな課題が残ってたを思い出したので。

参考

今回は、参考サイトはありません。
参考書籍は第1回をご参照下さい。

素材

引き続き、Soft * Accessory様の素材をお借りしています。

1. Undo/Redoボタンの追加

まずはボタンの追加です。
といっても既に新規ファイル・フォルダでやったのと同じ手順です。
今回は、DetailViewControllerのinitに実装します。

		// targetとactionをnilにしたら勝手にundo,redoしてくれるっぽいけど保証されるかわからんのでやめとく
		UIBarButtonItem *undoButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemUndo target:self action:@selector(undoDidPush)];
		UIBarButtonItem *redoButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRedo target:self action:@selector(redoDidPush)];
		NSArray *items = [NSArray arrayWithObjects:undoButton, redoButton, nil];
		[toolbar setItems:items];

2. Undo/Redo機能の実装

NSUndoManagerを使って行ないます。
こいつはUIResponderのundoManagerプロパティから取得できます。
TextViewもUIResponderのsubclassの為、もちろんこのプロパティを持っています。
今回はTextViewから取得したundoMangerに対して、undo/redoメソッドを呼び出しています。

- (void)undoDidPush {
	[[textView_ undoManager] undo];
}

- (void)redoDidPush {
	[[textView_ undoManager] redo];	
}

[1.]のコメントにも書きましたが、
実はUIBarButtonSystemItemUndo・Redoのボタンを作るとき、
selfとactionをnilにしておけば勝手にやってくれるようです。
※ ドキュメントに記載が見当たらなかったので使うのは止めてます。

[完成]

できました!
Undo/Redoボタンの実装

[まとめ]

これでおしまいです。
そんなに苦労しないだろうと思ってましたが、
予想を上回る簡単さでした。

[コード]

今回のコードは「Edhita5.zip」に入っています。
GitHubのdownloadページからダウンロードして下さい。

Downloads for tnantoka's Edhita - GitHub

PR

Comments:0

Comment Form

Trackback+Pingback:

TrackBack URL for this entry
Listed below are links to weblogs that reference
IB不使用&オープンソースなiPadアプリ(テキストエディタ)を作る 第5回:Undo・Redoボタン from Born Neet

Home > Edhita > IB不使用&オープンソースなiPadアプリ(テキストエディタ)を作る 第5回:Undo・Redoボタン

Search
Loading
Feeds
Links
スポンサードリンク

Page Top