Cocoa小例子

来源:互联网 发布:手机淘宝联盟登录失效 编辑:程序博客网 时间:2024/06/07 00:02

####.NSData类型

    bytes->NSData

   Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};

   NSData *adata = [[NSData alloc] initWithBytes:byte length:24];

   NSData->bytes

   NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
   Byte *testByte = (Byte *)[testData bytes];
   for(int i=0;i<[testData length];i++)
      printf("testByte = %d\n",testByte[i]);


####.NSNumber类型(_num)

NSNumber是数字对象,可以将所有的基本数字类型都包装起来。

     NSNumber *_num=[NSNumber numberWithInt:23];

     NSNumber *_num1 = [NSNumber numberWithFloat:23.00];

     int i = [_num intValue];//23

数字类型的比较

     if([_num isEqualToNumber:floatNumber])...//YES


####.NSString 的用法(_nsTest)

构建NSString-常量

     NSString *_nsTest = @"Hello Wall";

构建NSString-分配内存

     NSString *_nsTest = [[NSString alloc] init];

     NSString *_nsTest = @"Hello Wall";

     NSString *_nsTest = [[NSString alloc] initWithString:@"This is my Pro"];

构建NSString-标准C创建

     char *Cstring = "This is a C Standard String!";

     NSString *astring = [[NSString alloc] initWithCString:Cstring];

判断大小

     可以先转为C语言,在用strcmp函数

     也可:

     NSString *astring01 = @"This is a String!"; 

     NSString *astring02 = @"This is a String!";    

     BOOL result = [astring01 compare:astring02] == NSOrderedSame;    //0

     NSLog(@"result:%d",result);     

     //

     NSString *astring01 = @"This is a String!";

     NSString *astring02 = @"this is a String!";

     BOOL result = [astring01 compare:astring02] == NSOrderedAscending;    //-1

     NSLog(@"result:%d",result);

     //

     NSString *astring01 = @"this is a String!";

     NSString *astring02 = @"This is a String!";

     BOOL result = [astring01 compare:astring02] == NSOrderedDescending;    //1

     NSLog(@"result:%d",result);

NSString 与 const char 的转换

     NSString *_nsTest = [NSString stringWithFormat:@"%@ is player in %s",@"laf","guangzhou"];

     NSString ==>> const char* 

     const char *pPath = [_nsTest UTF8String];

尾部添加NSString

     [_nsTest stringByAppendingString:@" to go"];

获取字串:param1(初始位置),param2(长度)

     NSString *_nsSubTest = [_nsTest substringWithRange:NSMakeRange(0,1)];

获取长度

     [_nsTest length];

获取特定位置的字符

     char ch = [_nsTest characterAtIndex:3];

根据路径获取Name//eg.  /Desk/abc/test.app   ->  test.app

     NSString *name = path.lastPathComponent;

判断是否包含另一个string。找空字串 是没有意思的。

     NSString *str = @"abc123";

     BOOL isContain = [str rangeOfString:@"124"].length>0 ? YES : NO;

Trim(头尾)

     NSString *str = @"  abc123  ";

     NSString *strTrim = [str stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];

     //得@"abc123"

分割(空格完全保留哦)

     NSArray *arr = [strSource componetSeparatedByString:@":"];    


####.NSArray(NSMutableArray) 的用法(_nsarrTest)

区别:

     NSArray创建之后,不能改变,末尾必须nil

     NSMutableArray为动态数组,可以删除添加,较为灵活,必须init,用完后必须release

创建NSArray(NSMutableArray)

     NSArray *_nsarrTest = [NSArray arrWithObjects:@"a1",@"b2",@"c3",nil];

     NSMutableArray *_nsarrMuTest = [ [NSMutableArray alloc] init];

遍历

     for(id obj in _nsarrTest)

     {

          [_nsarrMuTest addObject:obj]

     }

     for(int i=0;i<[_nsarrTest count];i++)

     {

               NSLog(@"index %d value is : %@",[_nsarrTest objectAtIndex:i]);

     }

     for(int i=0;i<[_nsarrMuTest count];i++)

     {

          NSLog(@"index %d value is : %@",[_nsarrMuTest objectAtIndex:i]);

     }

####.NSTextField的用法(_tfTest)

赋值:

     [_tfTest setStringValue:@"HelloWorld"];

     [_tfTest setDoubleValue:9.00];

获取:

     NSString* ns = _tfTest.strintValue;const char* p = [ns UTF8String];

     or 

     NSString* ns = [_tfTest strintValue];const char* p = [ns UTF8String];



####.NSComboBox的用法(_cbTest)

删除所有Items

     [_cbTest removeAllItems];

添加一个Item

     [_cbTest addItemWithObjectValue:@"Item 1:Tiger"];

添加一组Items

     NSMutableArray *files = [NSMutableArray arrayWithCapacity:100];

     [files addObject:@"PDT"];

     [files addObject:@"PDT FDT"];

     [files addObject:@"PDT ADT"];

     [_cbTest addItemsWithObjectValues:files];

选中一个Item

     [_cbTest selectItemAtIndex:3];

获取当前选框中的数

     NSString * nsTemp = [_cbTest stringValue];



####.NSSlider的用法(_slrTest)

赋值,游标到设定位置

     [_slrTest setDoubleValue:45.00];


####.NSOpenPanel 打开文件对话框

 NSOpenPanel *panel = [NSOpenPanel openPanel];

    [panel setMessage:@""];

    [panel setPrompt:@"OK"];

    [panel setCanChooseDirectories:YES];

    [panel setCanCreateDirectories:YES];

    [panel setCanChooseFiles:YES];

    NSString *path_all;

    NSInteger result = [panel runModal];

    if (result == NSFileHandlingPanelOKButton)

    {

        path_all = [[panel URL] path];

        NSLog(path_all);

    }


####.NSSavePanel 保存文件对话框(以保存2进制文件为例)

    NSSavePanel *savePanel = [NSSavePanelsavePanel];

    [savePanel setNameFieldLabel:@"Untitle.BIN"];

    [savePanel setMessage:@"Choose the path to save the *Bin file"];

    [savePanel setAllowedFileTypes:[NSArrayarrayWithObject:@"BIN"]];

    [savePanel setExtensionHidden:NO];

    [savePanel beginSheetModalForWindow:self.windowcompletionHandler:^(NSInteger result) {

        if(NSFileHandlingPanelOKButton==result)

{

            UInt8 buf[100]={…………};

    NSString *path = [[savePanel URLpath];

    NSData *nsData = [[NSDataalloc]initWithBytes:buf length:100];

    [nsData writeToFile:path atomically:YES];

    NSLog(@"%@\n",path);

        }

    }];


####.到指定文件夹中遍历指定类型的文件

NSFileManager *mgr = [NSFileManagerdefaultManager];

NSDirectoryEnumerator *direnum = [mgr enumeratorAtPath:@"指定文件夹"];

NSMutableArray *files = [NSMutableArrayarrayWithCapacity:100];

NSString *filename;

while(filename=[direnum nextObject]){

if([[filename pathExtensionisEqualToString:@"bin"])

{

NSLog(@"%@",filename);

[files addObject:filename];

}

}




####弹出对话框

NSAlert *alert = [NSAlertalertWithMessageText:str

                                     defaultButton:@"OK"

                                   alternateButton:nil

                                       otherButton:nil

                         informativeTextWithFormat:@""];

    [alert runModal];

OR

      NSAlert *alert = [[NSAlertalloc]init];

      [alert addButtonWithTitle:@"OK"];

      [alert addButtonWithTitle:@"Cancel"];

      [alert setMessageText:@"Thanks !!!"];


      [alert setAlertStyle:NSWarningAlertStyle];

    

      if ([alertrunModal] !=NSAlertFirstButtonReturn) {

        ...

        ...

        ...


####

----------------------------新建NSTextView-------------------------------------------------------------------------------

NSScrollView *scrollView = [[NSScrollView alloc]initWithFrame:CGRectMake(0, 35, 335, 190)];
[scrollView setBorderType:NSNoBorder];
[scrollView setHasVerticalScroller:YES];
[scrollView setHasHorizontalScroller:NO];
[scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
NSTextView txtView =     [[NSTextView alloc]initWithFrame:CGRectMake(0, 0, 335, 190)];
[txtView setMinSize:NSMakeSize(0.0, 190)];
[txtView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[txtView setVerticallyResizable:YES];
[txtView setHorizontallyResizable:NO];
[txtView setAutoresizingMask:NSViewWidthSizable];
[[txtView textContainer]setContainerSize:NSMakeSize(335,FLT_MAX)];
[[txtView textContainer]setWidthTracksTextView:YES];
[txtView setFont:[NSFont fontWithName:@"Helvetica" size:12.0]];
[txtView setEditable:NO];
[scrollView setDocumentView:txtView];


NSView * v = [_window contentView];
[v addSubview:scrollView];

----------------------------新建NSTableView-------------------------------------------------------------------------------

   NSScrollView * tableContainer = [[NSScrollViewalloc] initWithFrame:NSMakeRect(10,10, 380, 200)];

    

   NSTableView *myTableView = [[NSTableViewalloc]initWithFrame:CGRectMake(0,0, 350, 170)];

    

    NSTableColumn * column1 = [[NSTableColumnalloc] initWithIdentifier:@"Col1"];

    

    [column1.headerCellsetTitle:@"第一列"];


    NSTableColumn * column2 = [[NSTableColumnalloc] initWithIdentifier:@"Col2"];    

    [column2.headerCellsetTitle:@"第二列"];

 

    [myTableViewaddTableColumn:column1];

    

    [myTableViewaddTableColumn:column2];

    

    [myTableViewsetDelegate:self];

    

    [myTableViewsetDataSource:self];

    

    [myTableViewreloadData];


    [tableContainersetDocumentView:myTableView];

    

    [tableContainersetHasVerticalScroller:YES];

    

    [[self.windowcontentView] addSubview:tableContainer ];



0 0
原创粉丝点击