iphone开发笔记(三)
2010-08-21 20:35 Tracy E 阅读(1366) 评论(4) 编辑 收藏 举报自定义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;
}
}
tableView的cell高度除了在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.