Objective C 添加到OS X状态托盘

@interface Tray : NSObject <NSApplicationDelegate> {
	NSStatusItem *trayItem;
}
@end

@implementation Tray

- (IBAction)testAction:(id)sender;
{
	NSLog(@"Hello World");
}

- (IBAction)quitAction:(id)sender;
{
	[NSApp terminate:sender];
}

- (void)applicationDidFinishLaunching:(NSNotification *)note;
{
	NSZone *zone = [NSMenu menuZone];
	NSMenu *menu = [[[NSMenu allocWithZone:zone] init] autorelease];
	NSMenuItem *item;
	
	item = [menu addItemWithTitle:@"Testing" action:@selector(testAction:) keyEquivalent:@""];
	[item setTarget:self];
	
	item = [menu addItemWithTitle:@"Quit" action:@selector(quitAction:) keyEquivalent:@""];
	[item setTarget:self];
	
	trayItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
	[trayItem setMenu:menu];
	[trayItem setHighlightMode:YES];
	[trayItem setTitle:@"HERE"];
}

- (void)dealloc;
{
	[trayItem release];
	[super dealloc];
}

@end

Objective C cocos2D tilemap中玩家的全方向移动

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
	
	BOOL moveX, moveY = NO;
	
    CGPoint touchLocation = [touch locationInView: [touch view]];		
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
    touchLocation = [self convertToNodeSpace:touchLocation];
	
    CGPoint playerPos = _player.position;
    CGPoint diff = ccpSub(touchLocation, playerPos);
	CGSize winSize = [[CCDirector sharedDirector] winSize];
	
	// Define areas on the screen to control player differently
	// Centralised cross that will move directly up, down, left or right (winsize.width/3, winsize.height/3)
	// Remaining areas will move diagonally
	// If central crossover area is pressed, player will not move.
	
	// Left/right press
	if (touchLocation.x < winSize.width/3) {
		moveX = YES;
	} else if (touchLocation.x > ((winSize.width/3)*2)) {
		moveX = YES;
	} else {
		moveX = NO;
	}
	
	
	// Up/down press
	if (touchLocation.y < winSize.height/3) {
		moveY = YES;
	} else 	if (touchLocation.y > ((winSize.height/3)*2)) {
		moveY = YES;
	} else {
		moveY = NO;
	}
	
	
	
    if (moveX) {
		if (diff.x >= _tileMap.tileSize.width) {
            playerPos.x += _tileMap.tileSize.width;
        } else if (diff.x <= (_tileMap.tileSize.width * -1)) {
            playerPos.x -= _tileMap.tileSize.width; 
        }    
	}
	
	if (moveY) {
        if (diff.y >= _tileMap.tileSize.height) {
            playerPos.y += _tileMap.tileSize.height;
        } else if (diff.y <= (_tileMap.tileSize.height * -1)) {
            playerPos.y -= _tileMap.tileSize.height;
        }
	}
	
	
	if (moveX || moveY) {
		
		if (playerPos.x <= (_tileMap.mapSize.width * _tileMap.tileSize.width) &&
			playerPos.y <= (_tileMap.mapSize.height * _tileMap.tileSize.height) &&
			playerPos.y >= 0 &&
			playerPos.x >= 0 ) 
		{
			[self setPlayerPosition:playerPos];
		}
		
		
	}
}

Objective C NSLog是一个NSString

NSLog(@"%@", string);

Objective C IPhone SDK创建并显示警报

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"You've Been Alerted" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
	[alert show];
	[alert release];

Objective C 获得单击双击触摸事件

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{	

	UITouch *touch = touches.anyObject;
    _dragStartPos = [touch locationInView:self];
	
/////////Logic To Handle Single and Double Tap

	//the the first tap delegate message if its a double tap and so on
	[NSObject cancelPreviousPerformRequestsWithTarget:self
											 selector:@selector(singleTap)
											   object:nil];
	
	[NSObject cancelPreviousPerformRequestsWithTarget:self
											 selector:@selector(doubleTap)
											   object:nil];
	
	//logic to get the tap count
	if(touches.count == 1)
	{  
		if([[touches anyObject] tapCount] == 2)
		{
			[self performSelector:@selector(doubleTap)
					   withObject:nil
					   afterDelay:0.35]; 
		}
		else if([[touches anyObject] tapCount] == 3)
		{
			[self trippleTap];
		}
		else
		{
			[self performSelector:@selector(singleTap)
					   withObject:nil
					   afterDelay:0.35]; 
		}
	}
	
	
}

Objective C 检测多点触控

NSSet *allTouches = [event allTouches];
	if([allTouches count]>=2)

Objective C 创建视图的位图图像

UIGraphicsBeginImageContext(srcView.bounds.size);
[srcView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();  

UIImageView* proxy = [[UIImageView alloc] initWithImage:viewImage];

Objective C 向NSDate添加天数

NSDate *now = [NSDate date];
int daysToAdd = 50;  // or 60 :-)
NSDate *newDate1 = [now addTimeInterval:60*60*24*daysToAdd];
NSLog(@"Quick: %@", newDate1);

Objective C 在NSString中拆分字符串

NSArray *cityname_arr = [Name componentsSeparatedByString:@","];

Objective C 在导航栏上的Infobutton

UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
		[infoButton addTarget:self action:@selector(toggleInfo) forControlEvents:UIControlEventTouchUpInside];
		UIBarButtonItem *iButton = [[UIBarButtonItem alloc] initWithCustomView: infoButton];
		self.navigationItem.rightBarButtonItem = iButton;
		[iButton release];