ツールバーとアクションシートの連携

テスト項目

とりあえず動かす。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    UIToolbar *tb;
    UIBarButtonItem *bb[1];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    tb = [[UIToolbar alloc] init];
    tb.frame =
      CGRectMake(0, self.view.bounds.size.height-44,
                 self.view.bounds.size.width, 44);
    tb.autoresizingMask =
    UIViewAutoresizingFlexibleWidth |
    UIViewAutoresizingFlexibleTopMargin;
    
    bb[0] = [[UIBarButtonItem alloc] initWithTitle:@"てすと"
            style:UIBarButtonItemStyleBordered
            target:self action:@selector(bb_touch:)];
    NSArray *ar = [NSArray arrayWithObjects:bb[0], nil];
    
    tb.items = ar;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:tb];
}

- (void)bb_touch:(UIBarButtonItem *)sender
{
    UIActionSheet *as = [[UIActionSheet alloc] init];
    
    as.title = @"写真";
    as.delegate = self;
    
    [as addButtonWithTitle:@"test 1"];
    [as addButtonWithTitle:@"test 2"];
    
    [as showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0){
        UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"XXX"
                                                     message:@"alert" delegate:nil
                                           cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [av show];
    }
}
@end