上一篇已經對于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
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
#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];
代碼很easy了解,不在贅述啦~
下面我們看一些基礎常用控件:
41
42
43
// appdelegate.h
// created by himi on 12-6-3.
@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;
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
#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");
運作截圖如下:

<a href="http://www.himigame.com/wp-content/uploads/2012/06/1232.png"></a>