【Mac OS X开发】在NSAlert中添加NSTableView,双击NSTableView单元格用于确定选择的效果

要达到在NSAlert中添加NSTableView,双击NSTableView单元格用于确定选择的效果,需要如下三个步骤:


在NSAlert中添加NSTableView视图

即通过将包含有tableView的scrollView所在的视图添加到[alert window]的contentView的子视图集合中,即可在NSAlert中添加NSTableView视图。

    NSString *messageTitle = @"Add New Item";
    NSString *defaultButtonTitle = @"Add";
    NSString *alternateButtonTitle = @"Cancel";
    
    NSAlert *alert = [NSAlert alertWithMessageText:messageTitle
                                     defaultButton:defaultButtonTitle
                                   alternateButton:alternateButtonTitle
                                       otherButton:nil
                         informativeTextWithFormat:@""];
    
    [alert setAlertStyle:NSInformationalAlertStyle];
    NSImage *icon = [NSImage imageNamed:@"icon.png"];
    [alert setIcon:icon];

    [alert layout];
    
    NSPanel* panel = [alert window];
    NSRect rect = [selectNewItemAccessoryView frame];
    rect.size.height += 160;
    [panel setFrame:rect display:YES];
    NSView* contentView = [panel contentView];
    // Get original sizes
    NSRect cframe = [contentView frame];
    NSRect rect2 = NSMakeRect(0, 50, cframe.size.width, cframe.size.height-138);
    // accessoryView为要添加的NSTableView
    [accessoryView setFrame:rect2];
    [contentView addSubview:selectNewItemAccessoryView];
    
    [alert beginSheetModalForWindow:[self window]
                      modalDelegate:self
                     didEndSelector:@selector(addNewItemAlertDidEnd:returnCode:contextInfo:)
                        contextInfo:nil];

 处理NSTableView单元格双击事件

- (void)awakeFromNib
{
    [super awakeFromNib];

    [_tableView setTarget:self];
    [_tableView setDoubleAction:NSSelectorFromString(@"doubleClick:")]; //setDoubleAction双击选择事件
}

- (void) doubleClick: (id)sender
{
    NSInteger rowNumber = [_tableView clickedRow];
    NSLog(@"Double Clicked.%ld ",rowNumber);
    // ...
}


NSAlert视图中双击NSTableView单元格后模拟确定效果并关闭NSAlert视图

    // 关闭alertView并确定选择
    NSPanel* alertWindow = (NSPanel *)[self window];
    [NSApp endSheet:alertWindow returnCode:NSAlertDefaultReturn];
    [alertWindow orderOut:self];

将上面这段代码放到NSTableView的双击响应事件(void)doubleClick: (id)sender中。

这样通过捕获和传入NSAlertDefaultReturn代码,即可执行该alertView的handleResult函数,达到模拟点击默认的按钮的效果。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值