我最近一直都有在看關于IOS項目的知識,今天也總算是迎來了我的第一個IOS項目。不巧這個項目并不是從頭開始開發,而是在基礎上維護并添加一些模塊。
噗~不管怎么樣,還是來分析分析一下defaultimage.png
"]; } if (image) { CGRect frameImage=CGRectMake(172 *i+70,10, 112, 112); [[UIColor whiteColor]set]; [[UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1]set]; [[UIColor clearColor] set]; [image drawInRect:CGRectInset(frameImage,0,0)]; } [[UIColor colorWithRed:116/255.0 green:109/255.0 blue:88/255.0 alpha:1]set]; NSString *china=[NSString stringWithFormat:@"%@",[dict objectForKey:@"name"]]; [china drawInRect:CGRectMake(172 *i+30,125, width, 30) withFont:[UIFont boldSystemFontOfSize:15]
看到這里我已經完全暈掉了,完全不知這個是用來干什么的?算了還是回到原來的路去吧,之前我們是看到了FFHomePageViewController.h。FFHomePageViewController.h是實現了FFHomeViewControllerDelegate接口,所以我們才會看到上述兩個文件的。那現在我們來看下FFHomePageViewController.m的構造函數吧~
FFHomePageViewController.m ---> init構造函數(從這個構造函數可以發現bookData是用來存儲bookMenu.plist加載到內存中的鍵值對的)
-(id)init { if(self= [super init]){ NSString *contentPath = [NSString stringByAppendDocumentDirectory:@"bookMenu.plist"]; bookData=[[NSMutableArray alloc] initWithContentsOfFile:contentPath]; } return self;}
FFHomePageViewController.m ---> loadView方法
//每次訪問UIViewController的view(比如controller.view、self.view)而且view為nil,loadView方法就會被調用。- (void)loadView { [super loadView]; //指定view的背景顏色 self.view.backgroundColor=[UIColor colorWithRed:235/255.0 green:232/255.0 blue:222/255.0 alpha:1]; //Logo圖片 UIImageView *upImage=[[UIImageView alloc] initWithFrame:CGRectMake(0,0, 768, 88)]; upImage.tag=51; [upImage setImage:[UIImage imageNamed:@"Ipadup"]]; [self.view addSubview:upImage]; [upImage release]; //TableView放一些菜單圖標 UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 88, 768, 830-5) style:UITableViewStylePlain]; [tableView setDelegate:self]; [tableView setDataSource:self]; [tableView setTag:111110]; tableView.separatorStyle=UITableViewCellSeparatorStyleNone; tableView.separatorColor=[UIColor clearColor]; [tableView setBackgroundColor:[UIColor clearColor]]; [self.view addSubview:tableView]; [tableView release]; }
話說FFHomePageViewController.h是實現UITableViewDataSource接口和UITableViewDelegate接口的,又通過[tableView setDelegate:self],[tableView setDataSource:self]語句,表示只要在FFHomePageViewController.m當前類重寫那些方法就可以了。
UITableViewDataSource,主要為UITableView提 供顯示用的數據(UITableViewCell),指定UITableViewCell支持的編輯操作類型(insert,delete和 reordering),并根據用戶的操作進行相應的數據更新操作,如果數據沒有更具操作進行正確的更新,可能會導致顯示異常,甚至crush。
UITableViewDelegate,主要提供一些可選的方法,用來控制tableView的選擇、指定section的頭和尾的顯示以及協助完成cell的刪除和排序等功能。
接下來看下渲染每一個cell的方法
FFHomePageViewController.m ---> - (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
//初始化每一行會調用該方法- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; static NSString *kCellTextField1 = @"Cell"; cell = [tableView1 dequeueReusableCellWithIdentifier:kCellTextField1]; if (cell==nil) { if (indexPath.section==0) { //初始化Image UIImage * bgImage = nil; bgImage = [[UIImage imageNamed:@"shuguiback.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:34]; cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellTextField1] autorelease]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.accessoryType=UITableViewCellAccessoryNone; //自定義的FFHomeView類構造函數返回自己本身 FFHomeView *drawFriends=[[FFHomeView alloc]initWithFrame:CGRectMake(0, 0,320, 160)]; [drawFriends setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; [drawFriends setTag:1]; //FFHomeView的delegate就是FFHomeViewControllerDelegate //而當前類就是實現FFHomeViewControllerDelegate接口的。就是把FFHomeView中要做的事,交給當前類 [drawFriends setDelegate:self]; [drawFriends setUserInteractionEnabled:YES]; [cell.contentView addSubview:drawFriends]; [drawFriends release]; cell.backgroundView=[[UIImageView alloc]initWithImage:bgImage]; } } //這里還沒怎么看的明白 if (indexPath.section==0) { if (bookData) { NSUInteger count = [bookData count]; NSUInteger length = indexPath.row * ROWCALUE_NUMBER + ROWCALUE_NUMBER > count?count - indexPath.row * ROWCALUE_NUMBER:ROWCALUE_NUMBER; NSArray * array = [bookData subarrayWithRange:NSMakeRange(indexPath.row * ROWCALUE_NUMBER , length)]; if (array) { FFHomeView * view1 = (FFHomeView * )[cell.contentView viewWithTag:1]; view1.bookArray=array; //setNeedsDisplay會調用自動調用drawRect方法 [view1 setNeedsDisplay]; } } } return cell;}
看了這個方法后差不多也明白了之前的FFHomeView.m文件做的是什么事情了,按照java的理解方式是這樣的。就相當于在FFHomeView類中寫了一個事件接口FFHomeViewControllerDelegate,在該類的touchesEnded事件函數中回調FFHomeViewControllerDelegate接口里的getPerSonProduct方法。而getPerSonProduct方法真正的實現而在FFHomePageViewController類當中。
然后調用順序就是:
當我們觸發菜單欄的圖標--->調用FFHomeView里的touchesEnded事件--->再調用到FFHomePageViewController里的getPerSonProduct事件
FFHomePageViewController.m ---> getPerSonProduct方法
-(void)getPerSonProduct:(FFHomeView *)controllerRenqi{ //在FFHomeView內部調用該函數的時候把自己本身傳入,而自己本身包含perDict鍵值對 //<dict><key>url</key><string>/index.php/cms/item-hjindex</string></dict> NSString *stringURL=[controllerRenqi.perDict objectForKey:@"url"]; if ([stringURL isEqualToString:@"exit"]) { //若退出,顯示登陸界面 FFLrcController *publish = [[FFLrcController alloc]init]; UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:publish]; [self presentModalViewController:nav animated:NO]; //每次使用完controller都釋放掉 [publish release]; return; } NSString *createurl=[controllerRenqi.perDict objectForKey:@"createurl"]; //WebViewController FFFFWebViewController * web=[[FFFFWebViewController alloc] init:stringURL createURL:createurl]; NSString *name=[controllerRenqi.perDict objectForKey:@"name"]; web.TT=name; web.hidesBottomBarWhenPushed=YES; [self.navigationController pushViewController:web animated:YES]; [web release]; }
看完上面這些代碼后,發現接下來的視圖傳入的是WebViewController,參數是url。我想這個一定是傳說中的app中加個webview。大部分操作可能會由webview來實現。
另外我想你會跟我一樣有這樣的一些問題,createurl是什么?干什么用?不急不急,接下來來看看FFFFWebViewController 是怎么實現的。
#import <UIKit/UIKit.h>#import "FFFFSwitchViewController.h"//發現這個類是繼承UIViewController,并實現了UIWebViewDelegate接口@interface FFFFWebViewController : UIViewController <UIWebViewDelegate>{ //瀏覽器 UIWebView * webView; //UIActivityIndicatorView實例提供輕型視圖,這些視圖顯示一個標準的旋轉進度輪。 UIActivityIndicatorView *activityIndicator; NSString *stringUrl; NSString *createUrl; NSString *TT;}@property(nonatomic,retain)NSString *TT; @property(nonatomic,retain)NSString *stringUrl;@property(nonatomic,retain)NSString *createUrl;-(id)init:(NSString *)string createURL:(NSString*)curl;//-(void)loadWebView:(NSString*)string;@end
FFFFWebViewControlle.h主要就是實現了 UIWebViewDelegate委托接口,UIWebViewDelegate主要有下面幾個方法。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; //加載前
//第一個參數為:要顯示的地址-(id)init:(NSString *)string createURL:(NSString*)curl{ if(self=[super init]){ self.stringUrl=[[NSString stringWithFormat:@"%@%@", @"http://xxxxx/hjjd", string]
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; self.createUrl = curl; } return self;}
FFFFWebViewController.m ---> loadView方法
- (void)loadView { [super loadView]; self.view.backgroundColor=[UIColor colorWithRed:235/255.0 green:232/255.0 blue:222/255.0 alpha:1]; //存放logo UIImageView *uv=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 768, 60)]; uv.tag=1; [uv setImage:[UIImage imageNamed:@"Ipadnavigation"]]; [self.view addSubview:uv]; [uv release]; //返回按鈕 UIButton *check = [UIButton buttonWithType:UIButtonTypeCustom]; check.tag=2; check.frame =CGRectMake(10, 22, 48, 30); [check.titleLabel setFont:[UIFont boldSystemFontOfSize:14]]; [check setTitle:@"返回" forState:UIControlStateNormal]; //點擊后出發goBack事件 [check addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchDown]; [check setBackgroundImage:[UIImage imageNamed:@"IpadnClose"] forState:UIControlStateNormal]; [self.view addSubview:check]; //新建按鈕 if(![self.createUrl isEqualToString:@"null"]){ UIButton *upButton = [UIButton buttonWithType:UIButtonTypeCustom]; upButton.tag=3; upButton.frame =CGRectMake(768-58, 22, 48, 30); [upButton setTitle:@"新建" forState:UIControlStateNormal]; [upButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14]]; //點擊后觸發resum事件 [upButton addTarget:self action:@selector(resum) forControlEvents:UIControlEventTouchDown]; [upButton setBackgroundImage:[UIImage imageNamed:@"IpadnClose"] forState:UIControlStateNormal]; [self.view addSubview:upButton]; } //標題 UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,23,768,30)]; titleLabel.tag=4; [titleLabel setFont:[UIFont boldSystemFontOfSize:20]]; [titleLabel setTextAlignment:UITextAlignmentCenter]; titleLabel.lineBreakMode=UILineBreakModeWordWrap; titleLabel.numberOfLines=0; titleLabel.text =TT; [titleLabel setTextColor:[UIColor whiteColor]]; [titleLabel setBackgroundColor:[UIColor clearColor]]; [self.view addSubview:titleLabel]; [titleLabel release]; webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 60, 768, 1024-20-88-88+60)]; webView.scalesPageToFit = YES; //設置delegate為本身,因為該類實現UIWebViewDelegate接口 webView.delegate = self; webView.backgroundColor = [UIColor clearColor]; [self.view addSubview:webView];
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 15, 50, 50)]; [activityIndicator setCenter:CGPointMake(768/2,1024/2)]; [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray]; [self.view addSubview:activityIndicator]; //加載鏈接地址 [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.stringUrl]]];}
FFFFWebViewController.m ---> resum事件
-(void)resum{ FFcreateViewContonroller *matchset=[[FFcreateViewContonroller alloc]init:self.createUrl]; matchset.TT=[NSString stringWithFormat:@"新建%@",TT]; [self presentModalViewController:matchset animated:YES]; [matchset release];}
FFFFWebViewController.m ---> goBack事件
- (void)goBack { //假如瀏覽器可以goBack就先goBack if([webView canGoBack]){ [webView goBack]; }else{ //否則彈出controller [self.navigationController popViewControllerAnimated:YES]; }}
從上面的代碼就可以看出createurl是用來做什么的了,假如我們點擊右上角的新建按鈕,createurl可以讓webview轉跳到自己定義的頁面當中。另外假如自己需要controller之間的轉跳,則只要在新建點擊后判斷createurl的內容,就可以任意轉跳到自己的地方了。到這里差不多可以看懂原作者的app設計思路是怎么樣的了。這里點擊新建后會進入
新聞熱點
疑難解答