【Cocoa(mac) Application 开发系列之二】总结一些常用控件及自定义View

简介:

上一篇已经对于xib与控件之间的关系都大致介绍了;

那么本篇不再详细解释如何如何连接控件以及控件代码等,直接给出代码以及需要注意的简单介绍下,便于童鞋们使用时可以给与参考:

1. 首先创建一个MyView类,继承NSView,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
//
//  MyView.h
//  ManyControlTest
//
//  Created by Himi on 12-6-6.
//  Copyright (c) 2012年 Himi. All rights reserved.
//
 
#import <Cocoa/Cocoa.h>
 
@interface MyView : NSView
@end
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
//  MyView.m
//  ManyControlTest
//
//  Created by Himi on 12-6-6.
//  Copyright (c) 2012年 Himi. All rights reserved.
//
 
#import "MyView.h"
 
@implementation MyView
- (id)initWithFrame:(NSRect)frame
{
     self = [super initWithFrame:frame];
     if  (self) {
         // Initialization code here.
     }
     
     return  self;
}
 
- ( void )drawRect:(NSRect)dirtyRect
{
     
     NSString * str =@ "MyView   --by Himi" ;
     
     //属性包装设置
     NSMutableDictionary *dic = [NSMutableDictionary dictionary];
     
     //设置字体样式
     [dic setObject:[NSFont fontWithName:@ "Times"  size:14] forKey:NSFontAttributeName];
     
     //设置字体颜色
     [dic setObject:[NSColor redColor] forKey:NSForegroundColorAttributeName];
     
     //绘制
     [str drawAtPoint:NSMakePoint(50, 50) withAttributes:dic];
}
 
@end

代码很easy理解,不在赘述啦~

下面我们看一些基础常用控件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
//  AppDelegate.h
//  ManyControlTest
//
//  Created by Himi on 12-6-3.
//  Copyright (c) 2012年 Himi. All rights reserved.
//
 
#import <Cocoa/Cocoa.h>
#import "MyView.h"
 
@interface AppDelegate : NSObject <NSApplicationDelegate,NSTabViewDelegate>
{
     
     IBOutlet NSTextField *nfCount;
     
     IBOutlet NSView *view ;
     
     IBOutlet NSButton *btn;
     
     IBOutlet NSPopUpButton *popBtn;
     
     IBOutlet NSSegmentedControl * nsc;
     
     IBOutlet NSForm *nForm;
     
     IBOutlet NSMatrix * ms;
     
     IBOutlet NSStepper * nsp;
     
     IBOutlet NSTabView *tbView;
     
     IBOutlet NSColorWell * nsWell;
     
     IBOutlet MyView * myView;
     
}
 
-(IBAction)btnPress:(id)sender;
 
@property (assign) IBOutlet NSWindow *window;
 
@end

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
//  AppDelegate.m
//  ManyControlTest
//
//  Created by Himi on 12-6-3.
//  Copyright (c) 2012年 Himi. All rights reserved.
//
 
#import "AppDelegate.h"
 
@implementation AppDelegate
 
@synthesize window = _window;
 
- ( void )applicationDidFinishLaunching:(NSNotification *)aNotification
{
     //------绑定Delegate
     [tbView setDelegate:self];
}
-(IBAction)btnPress:(id)sender{
     
     //------ 处理NSButton的
     if (btn == sender){
         [myView setHidden:YES];
     }
     
     
     //------处理NSPopUpButton
     if (popBtn == sender){
         NSLog(@ "%@" ,[popBtn itemTitleAtIndex:0]);
         NSLog(@ "%@" ,[popBtn itemTitleAtIndex:1]);
         NSLog(@ "%@" ,[popBtn itemTitleAtIndex:2]);
     }
     
     //------处理 NSSegmentedControl
     if (nsc ==sender){
         NSLog(@ "%i" ,[nsc isSelectedForSegment:0]);
         NSLog(@ "%i" ,[nsc isSelectedForSegment:1]);
         NSLog(@ "%i" ,[nsc isSelectedForSegment:2]);
     }
     
     //------处理 NSForm
     if (nForm == sender){
         NSLog(@ "NSForm Cell 1 is %@" ,[[nForm cellAtIndex:0] stringValue]);
 
         NSLog(@ "NSForm Cell 2 is %@" ,[[nForm cellAtIndex:1] stringValue]);
 
         NSLog(@ "NSForm Cell 3 is %@" ,[[nForm cellAtIndex:2] stringValue]);
     }
 
     //------处理NSMatrix
     if (ms == sender){
         NSLog(@ "NSMatrix is Select = %@" ,[[ms selectedCell] title]);
     }
 
     //-----处理 NSStepper
     if (nsp == sender){
         
         NSString *string = [NSString stringWithFormat:@ "%i" , ( int )[nsp doubleValue]];
         [nfCount setStringValue:string];
     }
     
     //-----处理 nsWell
     if (nsWell == sender){ 
         NSColor* color =  [nsWell color];
         NSLog(@ "R=%f,G=%f,B=%f" ,[color greenComponent],[color redComponent],[color blueComponent]);
     }
}
 
//------处理 tbView
//-(void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem{}
-( void )tabView:(NSTabView *)tabView willSelectTabViewItem:(NSTabViewItem *)tabViewItem{
     if  ([tbView indexOfTabViewItem:tabViewItem] == 0) {
         NSLog(@ "view 111" );
     } else  if  ([tbView indexOfTabViewItem:tabViewItem] == 1) {
         NSLog(@ "view 222" );
     }
}
 
@end

 

运行截图如下:










本文转自 xiaominghimi 51CTO博客,原文链接:http://blog.51cto.com/xiaominghimi/969970,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
Android开发 iOS开发 开发者
轻松实现 UniApp Xcode 上传 IPA 无需 Mac,appuploder 一键上传助你高效开发!
轻松实现 UniApp Xcode 上传 IPA 无需 Mac,appuploder 一键上传助你高效开发!
|
3月前
|
调度 iOS开发 MacOS
【MacOS 系列】mac常用快捷键收集,包含提高开发效率、精选快捷键、内置截图快捷键
【MacOS 系列】mac常用快捷键收集,包含提高开发效率、精选快捷键、内置截图快捷键
40 0
|
7月前
|
关系型数据库 MySQL 应用服务中间件
Mac PHP-Nginx-Mysql 本地开发日常启动流程
Mac PHP-Nginx-Mysql 本地开发日常启动流程
42 1
|
4月前
|
小程序 IDE 前端开发
mac开发必备软件
作为一名不资深开发工程师,用对工具可以节省我们大量的时间用来摸鱼,比如我的这篇文章就是在摸鱼的时候写的,在使用mac的过程中我下载过大量的软件,最终经过时间的洗礼留下了一些比较优秀的软件推荐给大家,如果你也有宝藏软件欢迎推荐!
|
8月前
|
Dart Android开发 iOS开发
Flutter Mac上使用VSCode支持Flutter开发(下)
Flutter Mac上使用VSCode支持Flutter开发(下)
140 0
|
1月前
|
开发工具 git iOS开发
Mac 安装软件包管理工具Homebrew
Mac 安装软件包管理工具Homebrew
|
3月前
|
Unix 网络安全 iOS开发
Mac 电脑如何安装Wireshark?
Mac 电脑如何安装Wireshark?
Mac 电脑如何安装Wireshark?
|
11天前
|
Java Android开发 芯片
Mac M芯片安装DBeaver Ultimate
Mac M芯片安装DBeaver Ultimate
14 0
Mac M芯片安装DBeaver Ultimate
|
1月前
QT 5.14.2版本 MAC环境安装部署流程
QT 5.14.2版本 MAC环境安装部署流程