【Cocoa(mac) Application 开发系列之二】总结一些常用控件以及简单在自定义View上绘制字符串
2015-11-18 09:28
1016 查看
【Cocoa(mac) Application 开发系列之二】总结一些常用控件以及简单在自定义View上绘制字符串
那么本篇不再详细解释如何如何连接控件以及控件代码等,直接给出代码以及需要注意的简单介绍下,便于童鞋们使用时可以给与参考:1. 首先创建一个MyView类,继承NSView,如下:
// // 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
// // 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理解,不在赘述啦~
下面我们看一些基础常用控件:
// // 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
// // 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
运行截图如下:
![](http://www.himigame.com/wp-content/uploads/2012/06/1232.png)
相关文章推荐
- 安卓App热补丁动态修复技术介绍
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 自定义View步骤
- unity中实现压缩解压功能
- 【Cocoa(mac) Application 开发系列之一】创建第一个application—计算器
- 在android studio上使用小米真机调试
- android多媒体编程--加载大图片
- IOS程序crash捕获
- unity中实现断点续传功能
- 关于android使用Xutil保存cookie
- iOS 9的 Core Image新滤镜
- App界面交互设计规范
- Android Dev Tools
- iOS 百度统计的使用技巧
- Android之activity的生命周期
- [Android自定义控件] Android自定义控件
- swift之类和结构体
- android学习路径
- Android手势识别器
- iOS中 @synthesize 和 @dynamic 区别