第一個Mac OS X專案(純程式碼)

weixin_34138377發表於2018-06-19

本文作為快速建立專案入門,細節不做闡述,請自行探索

Mac OS X 的座標系不同於IOS
IOS 原點在螢幕左上角
Mac OS X 原點在左下角

Mac alloc init 一個viewcontroll 不會自動建立view
IOS 會自動建立view
所以Mac OS 的 ViewController
都需要實現loadView 方法
初始化view

建立專案

啟動 Xcode
選擇 建立新專案
頂部 選擇macOS
Application 下 選擇 cooca App

842182-878109684be64e2c.png
螢幕快照 2018-06-19 下午12.54.41.png

點選next
取消勾選Use Storyboards


842182-fb620bd9c175afb8.png
螢幕快照 2018-06-19 下午12.55.13.png

點選next

既然是純程式碼開發
那麼移除目錄中所有.xib檔案
也就是把所有.xib結尾的都刪掉
同時info.plist 移除對應的key

842182-f1ecd19213aaec2b.png
螢幕快照 2018-06-19 下午1.43.07.png

完成專案建立

編寫程式碼

編輯main.m

#import <Cocoa/Cocoa.h>
#import "AppDelegate.h"
int main(int argc, const char * argv[]) {
    
    NSApplication *app = [NSApplication sharedApplication];
    id delegate = [[AppDelegate alloc] init];
    app.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
    
    //視窗 關閉,縮小,放大等功能,根據需求自行組合
    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


建立第一個ViewController
快捷鍵commad+n


842182-a271d9793804fc32.png
螢幕快照 2018-06-19 下午1.06.55.png
842182-bca8eac465f20b4b.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

執行一下,看看效果吧
更多功能自行探索
你會成長很多

相關文章