【Objective_C】类的总结+实现简单的button点击功能

一、创建步骤
1.打开xcode
在这里插入图片描述
2.创建工程,点击commend line
在这里插入图片描述
3.创建Cocoa文件
在这里插入图片描述
4.弄清文件作用

.h文件:写接口,在此文件进行写属性和类方法(+),实例函数(-);
注:省去setter和getter可以使用 @property
() 有strong ,readwrite等类型
在这里插入图片描述
文件示例:

//
//  Car.h
//  ClassDemo
//
//  Created by LYF on 2020/9/5.
//  Copyright © 2020 LYF. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Car : NSObject{
    @private
    NSString *name_;
    @private
    int age;
}
// 定义属性
@property(strong) NSString *name;
@property(readwrite) int age;// 不能使用strong,strong用于对象
// 方法
// 类方法 ,对象需要指针;例如(NSString *)
+(void)sendThisDate:(NSDate *) date;
+(void)testNum:(int) number;
-(void)write:(NSString *)things;
@end

NS_ASSUME_NONNULL_END

.m文件:实现,实现接口的文件
示例:

//
//  Car.m
//  ClassDemo
//
//  Created by LYF on 2020/9/5.
//  Copyright © 2020 LYF. All rights reserved.
//

#import "Car.h"

@implementation Car
@synthesize age; // 省去setter和getter

-(void) setName:(NSString *)name{
    name_=name;
}
-(NSString *)name{
    return name_;
}


// 类方法实现
+(void)sendThisDate:(NSDate *)date{
    NSLog(@"今天是%@",date);
}
+(void)testNum:(int)number{
    NSLog(@"test number is %d",number);
}
-(void)write:(NSString *)things{
    NSLog(@"things are %@",things);// %@:字符串 %s 字符char  %d int
}
@end

二、关键代码

AppDelegate.h

//
//  AppDelegate.h
//  ClassDemo
//
//  Created by LYF on 2020/9/5.
//  Copyright © 2020 LYF. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
NS_ASSUME_NONNULL_BEGIN

@interface AppDelegate : NSObject <NSApplicationDelegate>
    @property (assign) NSWindow *window;
@end
NS_ASSUME_NONNULL_END

AppDelegate.m

//
//  AppDelegate.m
//  ClassDemo
//
//  Created by LYF on 2020/9/5.
//  Copyright © 2020 LYF. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window=_window;
-(void)applicaitonDidFinishLauching:(NSNotification *)aNotification{
    NSLog(@"Mac app finished lauching");
}

-(void)changeColor{
    self.window.backgroundColor = [NSColor blueColor];
}

-(void)applicationDidFinishLaunching:(NSNotification *)anotification{
    NSLog(@"结束加载");
    NSButton *button=[NSButton new];
    
    button = [[NSButton alloc] initWithFrame:NSMakeRect(230, 200, 100, 40)];
    [button setTitle:@"change color"];
    [button setButtonType:NSMomentaryLightButton];
    [button setTarget:self];
     [button setAction:@selector(changeColor)];
    [[self.window contentView] addSubview: button];
   
    NSTextField *text=[NSTextField new];
   
    text=[[NSTextField alloc] initWithFrame:NSMakeRect(100, 100, 300, 30)];
    
    [[self.window contentView] addSubview:text];
}
@end

main.m

//
//  main.m
//  ClassDemo
//
//  Created by LYF on 2020/9/5.
//  Copyright © 2020 LYF. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Car2.h"
#import "ClassLearn/Car.h"  // 表明引入路径 默认从ClassDemo开始
#import "ClassLearn/practice.h"
#import "AppDelegate.h"

@class Car;
@class Car2;
@class practice;
int main(int argc, const char * argv[]) {
    @autoreleasepool {     
        NSApplication *macApp = [NSApplication sharedApplication];
       
        AppDelegate *appDelegate = [[AppDelegate alloc] init];
        
        macApp.delegate = appDelegate;
        
        int style = NSClosableWindowMask | NSResizableWindowMask | NSTexturedSquareBezelStyle | NSTitledWindowMask | NSMiniaturizableWindowMask;
        
        NSWindow *appWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(50, 50, 600, 400) styleMask:style backing:NSBackingStoreBuffered defer:NO];
        appWindow.backgroundColor=[NSColor redColor];
        
        appDelegate.window = appWindow;
        
        [appWindow makeKeyAndOrderFront:appWindow];
        [macApp run];
        
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值