iphone开发笔记(三)



2010-08-21 20:35 
Tracy E 
阅读(1338
评论(4
编辑 
收藏 
举报

p.p1 { margin: 0; font: 14px Monaco; color: rgba(251, 32, 148, 1) }
p.p2 { margin: 0; font: 14px Monaco; color: rgba(60, 16, 130, 1) }
p.p3 { margin: 0; font: 14px Monaco; min-height: 19px }
p.p4 { margin: 0; font: 14px Monaco; color: rgba(83, 129, 135, 1) }
p.p5 { margin: 0; font: 14px “Heiti SC Light”; color: rgba(251, 32, 148, 1) }
p.p6 { margin: 0; font: 14px Monaco; color: rgba(8, 0, 171, 1) }
p.p7 { margin: 0; font: 14px Monaco; color: rgba(251, 32, 148, 1); min-height: 19px }
p.p8 { margin: 0; font: 14px Monaco }
p.p9 { margin: 0; font: 14px Monaco; color: rgba(110, 52, 171, 1) }
p.p10 { margin: 0; font: 14px Monaco; color: rgba(34, 145, 255, 1) }
p.p11 { margin: 0; font: 14px Monaco; color: rgba(252, 149, 0, 1) }
p.p12 { margin: 0; font: 14px Monaco; color: rgba(34, 145, 255, 1); min-height: 19px }
p.p13 { margin: 0; font: 14px “Heiti SC Light”; color: rgba(252, 149, 0, 1) }
p.p14 { margin: 0; font: 14px Monaco; color: rgba(182, 28, 164, 1) }
p.p15 { margin: 0; font: 14px Monaco; color: rgba(18, 0, 255, 1) }
span.s1 { font: 14px “Heiti SC Light”; letter-spacing: 0 }
span.s2 { letter-spacing: 0 }
span.s3 { }
span.s4 { }
span.s5 { }
span.s6 { }
span.s7 { }
span.s8 { }
span.s9 { }
span.s10 { }
span.s11 { font: 14px Monaco; letter-spacing: 0 }
span.s12 { text-decoration: underline; letter-spacing: 0 }
span.s13 { }
span.s14 { }
span.Apple-tab-span { white-space: pre }

自定义NavigationBar

navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

[navigationBar setBarStyle:UIBarStyleBlackOpaque];

myNavigationItem = [[UINavigationItem alloc] initWithTitle:@”Setting”];

[navigationBar setItems:[NSArray arrayWithObject:myNavigationItem]];

[self.view addSubview:navigationBar];

backButton = [[UIBarButtonItem alloc] initWithTitle:@”Back” style:UIBarButtonItemStylePlain target:self action:@selector(back)];

myNavigationItem.leftBarButtonItem = backButton;

 

 

利用Safari打开一个链接

NSURL *url = [NSURL URLWithString:@”http://www.cnblogs.com/tracy-e/];

[[UIApplication sharedApplication] openURL:url];

 

利用UIWebView显示pdf文件、网页。。。

webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

[webView setDelegate:self];

[webView setScalesPageToFit:YES];

[webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

[webView setAllowsInlineMediaPlayback:YES];

[self.view addSubview:webView];

NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@”ojc” ofType:@”pdf”];

NSURL *url = [NSURL fileURLWithPath:pdfPath];

NSURLRequest *request = [NSURLRequest requestWithURL:url

cachePolicy:NSURLRequestUseProtocolCachePolicy

timeoutInterval:5];

[webView loadRequest:request];

 

 

[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:

@”http://www.cnblogs.com/tracy-e/”]]];

 

NSString *errorString = [NSString stringWithFormat:@”<html><center><font size=

+5 color =\’red\’>An Error Occurred:<br>%@</fone></center></html>”,error];

[myWebView loadHTMLString:errorString baseURL:nil];

//Stopping a load request when the view is to disappear

– (void)viewWillDisappear:(BOOL)animate{

if ([myWebView loading]){

[myWebView stopLoading];

}

myWebView.delegate = nil;

[UIApplication shareApplication].networkActivityIndicatorVisible = NO;

}

 

汉字转码

NSString *oriString = @”\u67aa\u738b”;

NSString *escapedString = [oriString

stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

 

 

Checking for background support on earlier versions of iOS

UIDevice *device = [UIDevice currentDevice];

BOOL backgroundSupported = NO;

if ([device respondsToSelector:@selector(isMultitaskingSupported)]){

backgroundSupported = device.multitaskingSupported;

}

 

Being a Responsible,Multitasking-Aware Application

# Do not make any OpenGL ES calls from your code.

# Cancel any Bonjour-related services before being suspended.

# Be prepared to handle connection failures in your network-based sockets.

# Save your application state before moving to the background.

# Release any unneeded memory when moving to the background.

# Stop using shared system resources before being suspended.

# Avoid updating your windows and views.

# Respond to connect and disconnect notification for external accessories.

# Clean up resource for active alerts when moving to the background.

# Remove sensitive information from views before moving to the background.

# Do minimal work while running in the background.

 

Handing the Keyboard notifications

//Call this method somewhere in your view controller setup code

– (void) registerForKeyboardNotifications{

 

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWasShown:)

name:UIKeyboardDidShowNotification

object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWasHidden:)

name:UIKeyboardDidHideNotification

object:nil];

 

}

 

//Called when the UIKeyboardDidShowNotification is sent

– (void)keyboardWasShown:(NSNotification *) aNotification{

if(keyboardShown)

return;

NSDictionary *info = [aNotification userInfo];

 

//get the size of the keyboard.

NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];

CGSize keyboardSize = [aValue CGRectValue].size;

//Resize the scroll view

CGRect viewFrame = [scrollView frame];

viewFrame.size.height -= keyboardSize.height;

 

//Scroll the active text field into view

CGRect textFieldRect = [activeField frame];

[scrollView scrollRectToVisible:textFieldRect animated:YES];

 

keyboardShown = YES;

}

 

//Called when the UIKeyboardDidHideNotification is sent

– (void)keyboardWasHidden:(NSNotification *) aNotification{

NSDictionary *info = [aNotification userInfo];

//Get the size of the keyboard.

NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

CGSize keyboardSize = [aValue CGRectValue].size;

 

//Reset the height of the scroll view to its original value

CGRect viewFrame = [scrollView Frame];

viewFrame.size.height += keyboardSize.height;

scrollView.frame = viewFrame;

keyboardShown = NO;

}

 

点击键盘的next按钮,在不同的textField之间换行

//首先给不同的textField赋不同的且相邻的tag

– (BOOL)textFieldShouldReturn:(UITextField *)textField

{

if ([textField returnKeyType] != UIReturnKeyDone)

{

NSInteger nextTag = [textField tag] + 1;

UIView *nextTextField = [[self tableView] viewWithTag:nextTag];

[nextTextField becomeFirstResponder];

}

else {

[textField resignFirstResponder];

}

return YES;

}

 

Configuring a date formatter

– (void)viewDidLoad {

[super viewDidLoad];

dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setGeneratesCalendarDates:YES];

[dateFormatter setLocale:[NSLocale currentLocale]];

[dateFormatter setCalendar:[NSCalendar autoupdatingCurrentCalendar]];

[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];

[dateFormatter setDateStyle:NSDateFormatterShortStyle];

DOB.placeholder = [NSString stringWithFormat:@”Example: %@”,[dateFormatter stringFromDate:[NSDate date]]];

}

 

– (void)textFieldDidEndEditing:(UITextField *)textField{

[textField resignFirstResponder];

if ([textField.text isEqualToString:@””])

return;

switch (textField.tag){

case DOBField:

NSDate *theDate = [dateFormatter dateFromString:textField.text];

if (theDate)

[inputDate setObject:theDate forKey:MyAppPersonDOBKey];

break;

default:

break;

}

}

 

 

tableViewcell高度除了在delegate中指定外,还可以在任意位置以[tableView setRowHeight:44]的方式指定

 

[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];

 

– (void)setEditing:(BOOL)editing animated:(BOOL)animated{

[super setEditing:editing animated:animated];

if (editing){

……

}

else{

……

}

}

 

One added a subview to a view, release the subview to avoid the extra retain count of it, Because when you insert a view as a subview using addSubview:, the subview is retained by its superview. When you remove the subview from its superview using the removeFromSuperview: method, subview is autoreleased.

版权声明:本文为tracy-e原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/tracy-e/archive/2010/11/15/1805503.html