iphone app开发经验(一)
1.在viewDidLoad时对属性的附值要注意在属性前加self,比如[self xxx]=jjj;
2.从文件读取文本
NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; // encoding:NSUTF8StringEncoding error:nil 这一段一定要加,不然中文字会乱码
NSString *filename; while (filename = [direnum nextObject]) { if([[filename pathExtension] hasSuffix:@"jpg"]){ [files addObject:filename]; } }
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
NSURL *url = [[NSURL alloc] initWithString: @"http://www.oreilly.com/" ]; [ [ UIApplication sharedApplication ] openURL: url ];
- (void)applicationWillResignActive:(UIApplication *) application { NSLog(@"About to be suspended"); /* Code to prepare for suspend */ } - (void)applicationDidBecomeActive:(UIApplication *) application { NSLog(@"Became active"); /* 为恢复做准备的代码 */ }
NSURL *url = [ [ NSURL alloc ] initWithString: @"tel:212-555-1234" ]; [ [ UIApplication sharedApplication ] openURL: url ];
[UIView beginAnimations:@"test" context:NULL]; [UIView setAnimationDuration:0.4];// 这里放代码,减速左右,做到淡入淡出 [UIView commitAnimations];
// With some valid UIView *view:for(UIView *subview in [view subviews]) { [subview removeFromSuperview];}
- (void) createProgressionAlertWithMessage:(NSString *)message withActivity:(BOOL)activity{UIAlertView *progressAlert = [[UIAlertView alloc] initWithTitle: message message: @"Please wait..." delegate: self cancelButtonTitle: nil otherButtonTitles: nil];// Create the progress bar and add it to the alertif (activity) {UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];activityView.frame = CGRectMake(139.0f-18.0f, 80.0f, 37.0f, 37.0f);[progressAlert addSubview:activityView];[activityView startAnimating];} else {UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(30.0f, 80.0f, 225.0f, 90.0f)];[progressAlert addSubview:progressView];[progressView setProgressViewStyle: UIProgressViewStyleBar];}[progressAlert show];[progressAlert release];}
[ UIApp openURL: [ NSURL URLWithString:@"http://www.apple.com" ] ];
[ UIApp openURL: [ NSURL URLWithString:@"mailto:apple@mac.com?Subject=hello" ] ];
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{if (self.bannerIsVisible) { [UIView beginAnimations:@"animateAdBannerOff" context:NULL];// assumes the banner view is at the top of the screen. banner.frame = CGRectOffset(banner.frame, 0, -50); [UIView commitAnimations]; self.bannerIsVisible = NO; }}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
- (void) postToTwitter{ // Since this will be launched in a separate thread, we need // an autorelease pool NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:@"http://TWITTER_ACCOUNT:PASSWORD@twitter.com/statuses/update.xml"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; // The text to post NSString *msg = @"testing"; // Set the HTTP request method [request setHTTPMethod:@"POST"]; [request setHTTPBody:[[NSString stringWithFormat:@"status=%@", msg] dataUsingEncoding:NSASCIIStringEncoding]]; NSURLResponse *response; NSError *error; if ([NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] != nil) NSLog(@"Posted to Twitter successfully."); else NSLog(@"Error posting to Twitter."); // Release pool [pool release]; } [NSThread detachNewThreadSelector:@selector(postToTwitter) toTarget:self withObject:nil];