- 追加された行はこの色です。
- 削除された行はこの色です。
&tag();
&tag(Objective-C, UIKit);
*目次 [#l3e91e9b]
#contents
*プログラムで追加する [#b8d44353]
-[[iOS: Xcode 4.2 and Navigation Controller - Stack Overflow:http://stackoverflow.com/questions/8606825/ios-xcode-4-2-and-navigation-controller]]
-[[iPhone Dev Tips: Navigation and Tap Bar Controller « Theolternative’s Weblog:http://theolternative.wordpress.com/2009/04/27/iphone-dev-tips-navigation-and-tap-bar-controller/]]
-UINavigationControllerのインスタンスを忘れずreleaseするようにしないとだめ。サンプルでinitはしてるがreleaseしてないものがたくさんみつかるので注意。
#pre{{
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
navController=[[UINavigationController alloc] init];
MyViewController *firstController=[[MyViewController alloc] init];
[navController pushViewController:firstController animated:NO];
[window addSubview navController.view];
}
- (void) dealloc
{
...
[navController release];
...
}
}}
*ボタンの追加 [#l6a47c0b]
**"+"ボタンを追加 [#bd62954c]
**バーの右側に"+"ボタンを追加 [#bd62954c]
-self.navigationItem.rightBarButtonItemにUIBarButtonItemを設定すればよい。
#pre{{
- (void)viewDidLoad {
[super viewDidLoad];
///// ナビゲーションバーのボタン
//"+"ボタンを生成
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(newPushed)];
//右側のボタンにセット
self.navigationItem.rightBarButtonItem = newButton;
// わすれずrelease。最初にautoreleaseしておくのでも良い。
[newButton release];
}
}}
**バーの右側にアクションボタンを追加してアクションシートを表示 [#t54a7f56]
-アクションボタンを押す→UIActionSheetを表示→UIActionSheet上のボタンが押されたときの処理。
#pre{{
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
UIBarButtonItem *actionButton = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:@selector(actionPushed)] autorelease];
self.navigationItem.rightBarButtonItem = actionButton;
}
- (void)actionPushed {
UIActionSheet *sheet = [[[UIActionSheet alloc] init] autorelease];
sheet.delegate = self;
[sheet addButtonWithTitle:@"テスト1"];
[sheet addButtonWithTitle:@"テスト2"];
[sheet addButtonWithTitle:@"Cancel"];
sheet.cancelButtonIndex = 2;
[sheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(@"テスト1が押されました");
} else if (buttonIndex == 1) {
NSLog(@"テスト2が押されました");
}
}
}}