iPhone 获得标准用户默认值

[NSUserDefaults standardUserDefaults]

iPhone 改变了UINavigationBar背景

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
        UIImage *image = [UIImage imageNamed: @"NavigationBar.png"];
	[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

iPhone 取消选择UITableView看起来像邮件应用程序

- (void)viewWillAppear:(BOOL)animated {
	[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

iPhone 更改iPhone的产品名称

1. Go to Targets in Xcode 
2. "Get Info" on your project's target (your current silly development name)
3. Search for "Product Name" under "Packaging". Change the value of that what you want the new program name is going to be.

iPhone 将左按钮添加到UINavigationBar

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelEdit:)] autorelease];

iPhone 拖放javascript到iphone支持

<script type="text/javascript"> 

function touchHandler(event)
{
    var touches = event.changedTouches,
        first = touches[0],
        type = "";
        
    
    switch(event.type)
    {
        case "touchstart":
            type = "mousedown";
            break;
            
        case "touchmove":
            type="mousemove";        
            event.preventDefault();
            break;        
            
        case "touchend":
            type="mouseup";
            break;
            
        default:
            return;
    }
    
    var simulatedEvent = document.createEvent("MouseEvent");
    
    //initMouseEvent(type, canBubble, cancelable, view, clickCount, screenX, screenY, clientX, clientY, 
    //               ctrlKey, altKey, shiftKey, metaKey, button, relatedTarget);
    
    simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY,
                                  false, false, false, false, 0/*left*/, null);
                                                                            
    first.target.dispatchEvent(simulatedEvent);
 
    //event.preventDefault();
}
 
function init() 
{
    document.addEventListener("touchstart", touchHandler, false);
    document.addEventListener("touchmove", touchHandler, false);
    document.addEventListener("touchend", touchHandler, false);
    document.addEventListener("touchcancel", touchHandler, false);    
}

//and what i've done here is create a load_function yield statement that attaches init() to the body onload= tag if you are using rails - otherwise just do <body onload="init();"> 

 <% load_function "init();"%>
       
        </script>

iPhone 在Cocos2d-iphone上使用openGL绘图

/*After a long search in the web, forums, IRC chats and several hours of trial and error... I finally can make some openGL drawing inside cocos2d-iphone objects.
I'm using the version 0.99.1 of cocos2d-iphone.
In the beginning the code was breaking in the call to glDrawArrays() with a EXC_BAD_ACCESS exception. Then I was able to draw a line but there was no color.
To save some time to the ones that were having the same problem here goes my solution.
I created a very simple class that draws colored line.
The class declaration is minimal:
*/

@interface Line : CCNode
{
}

@end

/*
It just has to subclass the CCNode class from cocos2d-iphone.
For the implementation I just had to overload the draw() method.
*/

@implementation Line

-(void) draw
{
static const GLfloat vertices[] =
{
0.0, 0.0,
250.0, 280.0
};
static const GLubyte squareColors[] = {
255, 0, 0, 255,
0, 0, 255, 255
};

glDisable(GL_TEXTURE_2D);
BOOL colorArrayEnabled = glIsEnabled(GL_COLOR_ARRAY);
if (!colorArrayEnabled) {
glEnableClientState(GL_COLOR_ARRAY);
}

BOOL vertexArrayEnabled = glIsEnabled(GL_VERTEX_ARRAY);
if (!vertexArrayEnabled) {
glEnableClientState(GL_VERTEX_ARRAY);
}

glLineWidth(2.0f);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glDrawArrays(GL_LINES, 0, 2);

if (!vertexArrayEnabled) {
glDisableClientState(GL_VERTEX_ARRAY);
}

if (!colorArrayEnabled) {
glDisableClientState(GL_COLOR_ARRAY);
}
glEnable(GL_TEXTURE_2D);
}

@end
/*
The important parts are that you have to disable GL_TEXTURE_2D and you need to use both color and vertex arrays to do the drawing.
I found out in a forum that having GL_TEXTURE_2D enabled won't allow you to make any drawing with openGL primitives.
Then I was using just vertex arrays with a call to glColor4f(), but this result in a line with no color.

This class is very simple to change so that you can specify the star and end point of the line and the color when you initialize the object.
I hope this will save you some hours of work.*/

iPhone NSPredicate for TextLength和电子邮件验证

// predicates
	NSPredicate *nameTest;
	NSPredicate *mailTest;

	nameTest = [[NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[a-zA-Z0-9_]{5,}"] retain];
	mailTest = [[NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}"] retain];
	BOOL isNameOK=[nameTest evaluateWithObject:txtSignUpUserName.text];
	BOOL isEmailOK=[mailTest evaluateWithObject:txtSignUpEmail.text];

iPhone 删除键盘

/*
 * Gets rid of the keyboard when the user taps the background.
 */
- (IBAction) backgroundTap: (id) sender {
    [titleField resignFirstResponder];
    [description resignFirstResponder];
}

/*
 * Gets rid of the keyboard during edting when the "Return" key is pressed.
 * (works for a UITextView)
 */
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
 replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return FALSE;
    }
    return TRUE;
}

/*
 * Gets rid of the keyboard during edting when the "Return" key is pressed.
 * (works for a UITextField)
 */
-(IBAction) textFieldDoneEditing:(id)sender {
    [sender resignFirstResponder];
}

iPhone 景观定位变换

// make view appear in landscape orientation
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0)); 
self.view.bounds = CGRectMake(0.0, 20.0, 1024, 748);