&tag(Objective-C, UIKit);
*目次 [#l3e91e9b]
#contents

*ボタンの追加 [#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が押されました");
    }
}
}}



トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS