天天看点

【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

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");

运行截图如下:

【COCOA(MAC) APPLICATION 开发系列之二】总结一些常用控件以及简单在自定义VIEW上绘制字符串

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