2011 Archives

无题

| No Comments | No TrackBacks
晚上和哥哥姐姐在IM上聊了很多
总想写很多事情,但是不知道如何写起。

将来的某一天会下吧。

UsingBlocksAsContextInfo - Demonstrates how to implement Objective-C "blocks" passed in as the 'contextInfo' to NSAlert, helping to handle the alert result.


// This is a quick tip on how to use the context info as a block parameter.

// You can use this type of pattern for any methods that have a delegate/selector/contextInfo pattern.

- (void)btnShowAlertClicked:(id)sender  {

    NSAlert *alert = [NSAlert alertWithMessageText:@"Alert Message"

                                     defaultButton:@"Default Button"

                                   alternateButton:@"Alternate Button"

                                       otherButton:@"Other Button"

                         informativeTextWithFormat:@"Informative Text"];

    

    BOOL someLocalVariable = YES;

    

    // We create a block that can easily access local variables to this method.

    // This is much easier than trying to package them all up into a contextInfo object

    void (^blockCallback)(NSInteger) = ^(NSInteger returnCode) {

        // Inside the block callback we can easily access locals

        if (someLocalVariable) {

            if (returnCode == NSAlertDefaultReturn) {

                [button setTitle:@"Default Return Button Clicked!"];

            } else {

                [button setTitle:@"Something else clicked...try again."];

            }

        }

    };

    

    // We copy the block, since it needs to stay alive for longer than the current scope

    [alert beginSheetModalForWindow:self.window

                      modalDelegate:[self class]

                     didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)

                        contextInfo:Block_copy(blockCallback)];

}


+ (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void (^)(NSInteger returnCode))continuationHandler {

    continuationHandler(returnCode);

    // The block must always be retained before the first call. This is the matching release

    Block_release(continuationHandler);

}


| No Comments | No TrackBacks

static void _processData(AppDelegate *self, NSInteger i) {

    // Notice that the _window can be accessed here, even though it is a private ivar

    // A compiler error (or warning) will happen if this method was outside the @implementation scope of AppDelegate.

    NSLog(@"Processing %d in window %@", i, self->_window);

}


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    for (NSInteger i = 0; i < 200; i++) {

        _processData(self, i);

    }

}

@金色

| No Comments | No TrackBacks
kong_hometown.JPG
@yarshure 2011.09.19 下午  

AppKit 一点笔记(三)

| No Comments | No TrackBacks
程序如何运行?
  入口main函数
  
#import <Cocoa/Cocoa.h>
 
int main(int argc, char *argv[])
{
    return NSApplicationMain(argc,  (const char **) argv);
}
  
应用程序主进程开启
 通过Info.plist 查找 NSMainNibFile,并load之
NSMainNibFile 文件Load完成后,下一步NibFile中"File's owner' 回调用awakeFromNib
10.6 之后支持[super awakeFromNib]

程序启动完成

程序状态管理和维护通过

NSApplication,NSApplicationDelegate 

NSNibLoading,单独load

使用NSBundle 3个方法

  

+ (BOOL)loadNibFile:(NSString *)fileName externalNameTable:(NSDictionary *)context withZone:(NSZone *)zone;

+ (BOOL)loadNibNamed:(NSString *)nibName owner:(id)owner;

- (BOOL)loadNibFile:(NSString *)fileName externalNameTable:(NSDictionary *)context withZone:(NSZone *)zone;


看起来第二个更简单些

  


另外Document base Application NSDocument 类可以使用

- (NSString *)windowNibName;方法加载目标Nib文件


NSWindowController 同样可以使用上面的这个方法


AppKit 一点笔记(二)

| No Comments | No TrackBacks
1 Dock Menu 效果如图
dock_menu.png
实现方法

NSApplicationDelegate

实现

- (NSMenu *)applicationDockMenu:(NSApplication *)sender

{

    return _menu;

}

2 状态条菜单

  需要NSMenuDelegate protocol

@protocol NSMenuDelegate <NSObject>

@optional

- (void)menuNeedsUpdate:(NSMenu*)menu;


- (NSInteger)numberOfItemsInMenu:(NSMenu*)menu;

- (BOOL)menu:(NSMenu*)menu updateItem:(NSMenuItem*)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel;

- (void)menuWillOpen:(NSMenu *)menu;

- (void)menuDidClose:(NSMenu *)menu;

- (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;

@end


statbar_menu.png

StatusBar加Icon

- (void)activateStatusMenu:(int)status

{

    NSStatusBar *bar = [NSStatusBar systemStatusBar];

    icon = [bar statusItemWithLength:NSVariableStatusItemLength];

    [icon retain];

NSString* filePath;

switch (status) {

case 0:

filePath = [[NSBundle mainBundle] pathForResource:@"sync_complete" ofType:@"png"];

break;

case 1:

filePath = [[NSBundle mainBundle] pathForResource:@"sync_processing" ofType:@"png"];

break;

case 2:

filePath = [[NSBundle mainBundle] pathForResource:@"sync_error" ofType:@"png"];

break;

default:

break;

}

NSImage* image = [[NSImage alloc] initWithContentsOfFile:filePath];

[icon setImage:image];

[icon setHighlightMode:YES];

[icon setMenu:mainMenu];

}


关于菜单就讲这么多

AppKit 一点笔记

| No Comments | No TrackBacks

NSWindowController

- (id)initWithWindow:(NSWindow *)window

或者使用 - (NSString *)windowNibName

NSWindowController *controller = [[c alloc] init];

    if (_windowControllers == nil) {

        _windowControllers = [NSMutableArray new];

    }

    [_windowControllers addObject:controller];

    [controller showWindow:self];//显示Window,self 为NSWindowController

    [controller release];


注册窗口关闭通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_windowClosed:) name:NSWindowWillCloseNotification object:nil];


处理关闭窗口通知,移出不需要的Window

- (void)_windowClosed:(NSNotification *)note {

    NSWindow *window = [note object];

    for (NSWindowController *winController in _windowControllers) {

        if (winController.window == window) {

            [[winController retain] autorelease]; // Keeps the instance alive a little longer so things can unbind from it

            [_windowControllers removeObject:winController];

            break;

        }

    }

}

Enhanced by Zemanta

sysctl on iOS5

| No Comments | No TrackBacks
iPhone4.txt



See iPhone4s hardware  info 

hw.machine = iPhone4,1
hw.model = N94AP
hw.ncpu = 2
hw.byteorder = 1234
hw.physmem = 529481728
hw.usermem = 447721472
hw.pagesize = 4096
hw.epoch = 1
hw.vectorunit = 0
hw.busfrequency = 200000000
hw.cpufrequency = 800000000
hw.cachelinesize = 32
hw.l1icachesize = 32768
hw.l1dcachesize = 32768
hw.l2settings = 1
hw.l2cachesize = 1048576
hw.tbfrequency = 24000000
hw.memsize = 529481728
hw.availcpu = 2

开发中使用了 http://opensource.apple.com/ 开发的项目 system_cmds 中sysctl 


Enhanced by Zemanta