2013年4月2日 星期二

通知

//點擊後取消所有的編輯狀態
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//可以直接寫在UIViewController當中
{
 [self.view endEditing:YES];
}

 //利用textField的delegate Implement UITextFieldDelegate: @interface yourClass : UIViewController Use the delegate method: 
- (BOOL)textFieldShouldReturn:(UITextField *)textField{ 

 //do whatever you need here 
 //maybe you have several textFields, so first check which one was hit the return key: 
 if(textField == outlet_to_your_textField_1){ //do this }
 else if(textField == outlet_to_your_textField_2){ //do that }
 else if .... blablabla and so on return YES; 
}

連線到含有中文的網址

//要把整個網址轉碼成UTF8的格式,降子在eventLocationTextField輸入中文到網址中才不會錯 - (IBAction)callWebMap:(id)sender {

 NSString* txt = [[NSString alloc] initWithString:self.eventLocationTextField.text];
 NSString* query = [NSString stringWithFormat:@"http://maps.apple.com/maps?q=%@",txt]; 
NSString* strUrl = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSURL* url = [NSURL URLWithString:strUrl]; [[UIApplication sharedApplication] openURL:url]; 
[txt release];
 [query release];
 [strUrl release];
 [url release];

}

在tableCell中加入button的 方式

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //在cell.accessoryView加入button UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(tipAction:) forControlEvents:UIControlEventTouchDown]; button.tag=indexPath.row+1; [button setTitle:@"Show View" forState:UIControlStateNormal]; button.frame = CGRectMake(20.0, 20.0, 60.0, 40.0); cell.accessoryView = button; //在cell.contentView加入button UIButton *infoButton = [UIButton buttonWithType:102]; [infoButton setFrame:CGRectMake(10,10,infoButton.frame.size.width, infoButton.frame.size.height)]; [infoButton addTarget:self action:@selector(infoButtonTapped) forControlEvents:UIControlEventTouchUpInside]; [cell.contentView addSubview:infoButton]; //在cell.imageView加入button [cell.imageView setImage:[UIImage imageNamed:@"info-button.png"]]; [cell.imageView setUserInteractionEnabled:YES]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(infoImageTapped:)]; [tap setNumberOfTapsRequired:1]; [cell.imageView setGestureRecognizers:[NSArray arrayWithObject:tap]]; [tap release]; }

2013年4月1日 星期一

循環uiscroll﹠page view

1,在viewcontroller先宣告 
2,在viewdidload //新增自己的View 
 MyView=self.advertisementView; //MyView = [[[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, self.view.frame.size.width, 180.f)]autorelease]; self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10.f, 10.f, self.view.frame.size.width-20.f, 150.f) ]; self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0.f, 160.f, self.view.frame.size.width, 15.f)]; // _colors = [[NSArray alloc]initWithObjects: [UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor yellowColor], [UIColor orangeColor], nil]; self.pagesArray = [[NSMutableArray alloc] initWithArray:_colors]; //ScrollView 的內容總尺吋 : 原寬 x 有幾個分頁, 高不變 self.scrollView.backgroundColor = [UIColor clearColor]; self.scrollView.delegate = self; self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 3, self.scrollView.frame.size.height); //設定是否啟動分頁機制 : 如不啟動,則會一直滑動不停 ; 如啟動,會一格一格的分頁顯示 self.scrollView.pagingEnabled = YES; self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.showsVerticalScrollIndicator = NO; //設定 PageControl 的樣式 self.pageControl.backgroundColor = [UIColor clearColor]; self.pageControl.numberOfPages = self.pagesArray.count; self.pageControl.currentPage = 0; //設定 PageControl 的分頁數 self.pageControl.numberOfPages = self.pagesArray.count; self.pageControl.currentPage = 0; MyView.backgroundColor = [UIColor clearColor]; CGRect frame1; //定位每一個分頁 View 在 ScrollView 裡的位置( X 軸 ) frame1.origin.x = self.scrollView.frame.size.width * 0; // Y 軸不變 frame1.origin.y = 0; frame1.size = self.scrollView.frame.size; subview1 = [[UIView alloc] initWithFrame:frame1]; subview1.backgroundColor = [self.colors objectAtIndex:4]; CGRect frame2; //定位每一個分頁 View 在 ScrollView 裡的位置( X 軸 ) frame2.origin.x = self.scrollView.frame.size.width * 1; // Y 軸不變 frame2.origin.y = 0; frame2.size = self.scrollView.frame.size; subview2 = [[UIView alloc] initWithFrame:frame2]; subview2.backgroundColor = [self.colors objectAtIndex:0]; CGRect frame3; //定位每一個分頁 View 在 ScrollView 裡的位置( X 軸 ) frame3.origin.x = self.scrollView.frame.size.width * 2; // Y 軸不變 frame3.origin.y = 0; frame3.size = self.scrollView.frame.size; subview3 = [[UIView alloc] initWithFrame:frame3]; subview3.backgroundColor = [self.colors objectAtIndex:1]; //使用QuartzCore.framework替UIView加上圓角 //[subview1.layer setCornerRadius:15.0]; [_scrollView addSubview:subview1 ]; [_scrollView addSubview:subview2 ]; [_scrollView addSubview:subview3 ]; [self.scrollView scrollRectToVisible:CGRectMake(300.f, 0.f, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:NO]; [MyView addSubview:self.scrollView]; [MyView addSubview:self.pageControl]; 

 3,實作uiscrollviewdelegate
 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ NSLog(@"%d",self.pageControl.numberOfPages); 
 if (self.scrollView.contentOffset.x == 0.f) { 
 if (self.pageControl.currentPage==0) { self.pageControl.currentPage = self.pageControl.numberOfPages-1; }else{ self.pageControl.currentPage --; } }else if(self.scrollView.contentOffset.x == 600.f){ if (self.pageControl.currentPage==self.pageControl.numberOfPages-1) { self.pageControl.currentPage = 0; }else{ self.pageControl.currentPage++; } } int lastPage,currentPage,nextPage; currentPage = self.pageControl.currentPage; if (self.pageControl.currentPage==self.pageControl.numberOfPages-1) { lastPage = currentPage -1; nextPage = 0; }else if(self.pageControl.currentPage==0){ lastPage = self.pageControl.numberOfPages-1; nextPage = currentPage +1; }else{ lastPage = currentPage -1; nextPage = currentPage +1; } NSLog(@"next %d",nextPage); NSLog(@"current %d",currentPage); NSLog(@"last %d",lastPage); subview1.backgroundColor = [_colors objectAtIndex:lastPage]; subview2.backgroundColor = [_colors objectAtIndex:currentPage]; subview3.backgroundColor = [_colors objectAtIndex:nextPage]; NSLog(@"%d",self.pageControl.currentPage); [self.scrollView scrollRectToVisible:CGRectMake(300.f, 0.f, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:NO]; }

.h宣告的變數,在.m要alloc跟init不然會出錯

.h
@property(retain,nonatomic)NSArray *colors;

.m
_colors = [[NSArray alloc]initWithObjects: [UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor yellowColor], [UIColor orangeColor], nil];
 可以使用不會出錯 但若沒有使用alloc init

_colors = [NSArray ArrayWithObjects: [UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor yellowColor], [UIColor orangeColor], nil];
那只有在單一function可以用, 在其他的function呼叫_colors就會SIGABRT

實作APP開場影片

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. NSLog(@"didFinishLaunchingWithOptions"); NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"newhouse" ofType:@"MOV"]]; self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url]; //播放器參數設定 self.moviePlayer.controlStyle = MPMovieControlStyleNone; self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill; self.moviePlayer.repeatMode = MPMovieRepeatModeNone; self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin; //設定播放器大小 [self.moviePlayer.view setFrame:self.window.rootViewController.view.bounds]; [self.window.rootViewController.view addSubview:self.moviePlayer.view]; //播放 [self.moviePlayer play]; //註冊通知中心,當影片播放完畢,進行回呼方法(playbackStateDidFinish) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackStateDidFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer ]; //Hide video controls self.moviePlayer.controlStyle = MPMovieControlStyleNone; //Play as soon as loaded self.moviePlayer.shouldAutoplay = YES; return YES; } - (void)playbackStateDidFinish{ //註銷通知中心 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer]; //停止播放 [self.moviePlayer stop]; //移除播放器 [self.moviePlayer.view removeFromSuperview]; }

Could not find a storyboard named 'MainStoryboard' in bundle NSBundle

去Info.plist把Main storyboard file base name改為現在的storyboard名稱即可