iOS和Mac开发区别

iOS和Mac开发区别


UIButton & NSButton

//iOS中有一些常用的方法,Mac中没有,比如
//UIButton
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[button addTarget:@(selector(click:)) action:self forControlEvents:<#(UIControlEvents)#>];

//NSButton
NSButton *button=[[NSButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
button.target=self;
button.action=@selector(click);
[self.view addSubview:button];

没有Label控件

//在Mac开发中UILabel控件是用TextFiled实现的

label.enabled=NO;

label.bordered=NO;

label.selectable=YES;

获取NSBundle中的图片

//根据路径获取图片
[[NSImage alloc]initByReferencingFile:@"/Users/guodong/Desktop/mac/61f.png"];

NSWindow

  • Mac程序是可以有多个window

设置主窗口

NSApplication *app=[NSApplication sharedApplication];

NSWindow *window=app.keyWindow;

window.alphaValue=1;

window.backgroundColor=[NSColor clearColor];

[window setStyleMask:1];//设置窗口的风格

[window setFrame:CGRectMake(100, 100, 500, 500) display:YES animate:YES];//移动窗口,立即刷新,动画效果

创建新窗口

NSWindow *newWin=[[NSWindow alloc]initWithContentRect:CGRectMake(100,100, 300, 300) styleMask:1 backing:NSBackingStoreRetained defer:NO];

NSWindowController *wc=[[NSWindowController alloc]initWithWindow:new];

[wc showWindow:newWin];

[newWin setLevel:10];//设置窗口显示优先级

弹出选择文件用的控件

NSOpenPanel *openPanel=[NSOpenPanel openPanel];

[openPanel setCanChooseFiles:YES];

[openPanel setCanChooseDirectories:YES];

//[openPanel setAllowedFileTypes:[NSArray arrayWithObject:(NSString *)kUTTypeDirectory]];

[openPanel setAllowedFileTypes:@[@"mp3"]];//允许的类型

[openPanel setDirectoryURL:[NSURL fileURLWithPath:@"/Users/guodong/Desktop/song"]];//设置默认路径

[openPanel setAllowsMultipleSelection:YES];//允许按住shift/command多选

[openPanel runModal];//显示,并且返回结果 0取消了,1确认了

if ([openPanel runModal] == NSModalResponseOK) {

    NSURL *url = [openPanel URL];

    NSLog(@"%@",[openPanel filenames]);
}

打开文件/文件夹的操作

[[NSWorkspace sharedWorkspace]openFile:@"/Users/guodong/Desktop/Tools"];//用默认的程序打开文件/文件夹
[[NSWorkspace sharedWorkspace]openFile:@"/Users/guodong/Desktop/首页 - 知乎.html" withApplication:@"Xcode"];//用指定的程序打开
//NSWorkspace可以做一些设计到其他App的操作,比如获取活动的app列表,隐藏其他app等
[[NSDocumentController sharedDocumentController]openDocument:@"/Users/guodong/Desktop/Tools"];
//NSDocument应该是 苹果提供的网络同步功能用的 大概

NSTableView

 NSTableView *tableView;
    tableView=[[NSTableView alloc]initWithFrame:CGRectMake(0, 0, 400, 300)];
    tableView.delegate=nil;//代理
    tableView.dataSource=nil;

    //[tableView setAutosaveName:@"downloadTableView"];
    //[tableView setAutoresizesSubviews:FULLSIZE];
    //[tableView setBackgroundColor:[NSColor whiteColor]];
    //[tableView setGridColor:[NSColor lightGrayColor]];
    //[tableView setGridStyleMask: NSTableViewSolidHorizontalGridLineMask];
    //[tableView setUsesAlternatingRowBackgroundColors:YES];
    //[tableView setAutosaveTableColumns:YES];
    //[tableView setAllowsEmptySelection:YES];
    //[tableView setAllowsColumnSelection:YES];



    NSScrollView *scrollView=[[NSScrollView alloc]initWithFrame:CGRectMake(0, 0, 400, 300)];
    [scrollView setDocumentView:tableView];//设置内容视图

    //[scrollView addSubview:tableView];虽然也能添加上,但是不能滑动什么的
    [self.view addSubview:scrollView];




    //设定表头
    NSTableHeaderView *tableHeadView=[[NSTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, 300, 20)];
    [tableView setHeaderView:tableHeadView];



    //以前不知道的。。。设定列,可以设定几列,而且可调
    NSTableColumn *column=[[NSTableColumn alloc] initWithIdentifier:@"column"];
    [[column headerCell] setStringValue:@"column"];
    //[[column headerCell] setAlignment:NSCenterTextAlignment];
    [column setWidth:400];
    [column setMinWidth:50];
    [column setEditable:YES];

    //[column setResizingMask:NSTableColumnAutoresizingMask | NSTableColumnUserResizingMask];

    [tableView addTableColumn:column];

//代理
 -(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
  return 100;
}
-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
  return @"ecwve";
}
-(CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
  return 100;
}
-(NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
   NSView *cellView=[[NSView alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];

  NSTextField *textField=[[NSTextField alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];

  [textField.cell setTitle:@"wcvc"];

  [cellView addSubview:textField];

  return cellView;
}

你可能感兴趣的:(Mac-OSX开发)