第一個(gè)Mac OS X項(xiàng)目(純代碼)

本文作為快速創(chuàng)建項(xiàng)目入門,細(xì)節(jié)不做闡述,請(qǐng)自行探索

Mac OS X 的坐標(biāo)系不同于IOS
IOS 原點(diǎn)在屏幕左上角
Mac OS X 原點(diǎn)在左下角

Mac alloc init 一個(gè)viewcontroll 不會(huì)自動(dòng)創(chuàng)建view
IOS 會(huì)自動(dòng)創(chuàng)建view
所以Mac OS 的 ViewController
都需要實(shí)現(xiàn)loadView 方法
初始化view

創(chuàng)建項(xiàng)目

啟動(dòng) Xcode
選擇 創(chuàng)建新項(xiàng)目
頂部 選擇macOS
Application 下 選擇 cooca App

屏幕快照 2018-06-19 下午12.54.41.png

點(diǎn)擊next
取消勾選Use Storyboards


屏幕快照 2018-06-19 下午12.55.13.png

點(diǎn)擊next

既然是純代碼開(kāi)發(fā)
那么移除目錄中所有.xib文件
也就是把所有.xib結(jié)尾的都刪掉
同時(shí)info.plist 移除對(duì)應(yīng)的key

屏幕快照 2018-06-19 下午1.43.07.png

完成項(xiàng)目創(chuàng)建

編寫代碼

編輯main.m,否則會(huì)出現(xiàn)窗口不顯示

#import <Cocoa/Cocoa.h>
#import "AppDelegate.h"
int main(int argc, const char * argv[]) {
    AppDelegate *delegate = [[AppDelegate alloc] init];
    [NSApplication sharedApplication].delegate = delegate;
    return NSApplicationMain(argc, argv);
}

編輯appDelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()

@property (nonatomic, strong) NSWindow *window;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    
    //窗口 關(guān)閉,縮小,放大等功能,根據(jù)需求自行組合
    NSUInteger style =  NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
    float w = [[NSScreen mainScreen] frame].size.width/2;
    float h = [[NSScreen mainScreen] frame].size.height/2;
    self.window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, w, h) styleMask:style backing:NSBackingStoreBuffered defer:YES];
    self.window.title = @"hello";
    [self.window makeKeyAndOrderFront:self];
    [self.window center];
}


- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}


@end


創(chuàng)建第一個(gè)ViewController
快捷鍵commad+n


屏幕快照 2018-06-19 下午1.06.55.png
屏幕快照 2018-06-19 下午1.07.28.png

編輯HomeViewController

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController
- (void)loadView{
    NSRect frame = [[[NSApplication sharedApplication] mainWindow] frame];
    self.view = [[NSView alloc] initWithFrame:frame];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    //do like ios
    NSButton *button = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
    [button setTitle:@"button"];
    [self.view addSubview:button];
}

@end

再次編輯appdelete.m

#import "AppDelegate.h"
#import "HomeViewController.h"
@interface AppDelegate ()

@property (nonatomic, strong) NSWindow *window;
@property (nonatomic, strong) HomeViewController *homeVC;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    //
    NSUInteger style =  NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
    float w = [[NSScreen mainScreen] frame].size.width/2;
    float h = [[NSScreen mainScreen] frame].size.height/2;
    self.window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, w, h) styleMask:style backing:NSBackingStoreBuffered defer:YES];
    self.window.title = @"hello";
    
    [self.window makeKeyAndOrderFront:nil];
    [self.window center];
    
    self.homeVC = [[HomeViewController alloc] init];
    [self.window setContentViewController:self.homeVC];
    
}


- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}


@end

運(yùn)行一下,看看效果吧
更多功能自行探索
你會(huì)成長(zhǎng)很多

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容