IOS學習日誌(三)------檢視控制器
本節要點:(重點:1、導航控制器,2、標籤控制器)
瞭解幾種常用的檢視控制器
理解檢視控制器的作用
理解和掌握導航控制器及標籤控制器的用法
1、常見的檢視控制器
--UIViewController
UIViewController是檢視控制器的父類,其他檢視控制器類都繼承它。每個檢視控制器都有一個單獨的主檢視,這是它獨有的檢視,用view屬性來表示。
--UINavigationController
UINavigationController允許在樹狀檢視層次結構間上下導航。它維護一個檢視控制器的棧,任何型別的檢視控制器都可以放入棧中。
--UITabBarController
當應用程式需要許多不同的互動模式或者不同的方式來檢視相同的資料時,我們就可以考慮使用標籤欄。
--UITableViewController
UITableViewController --表格檢視控制器簡化了iphone專案中的表檢視的使用。它提供了一個標準的已連線UITableView例項並自動將委託和資料來源設為指向自己,只需要提供這些委託和資料來源方法,用資料填充表格並相應使用者單擊即可。
---地址薄控制器
地址薄使用者介面框架提供懶人幾個檢視控制器,允許從地址薄中選擇某個人,檢視他的詳細資訊並新增新聯絡人或修改現有聯絡人條目。
--UIImagePickerController
影象選取器介面是通過名為UIImagePickerConttroller的模式控制器類執行的
。
-- 郵件撰寫
MFMailComposeViewController允許建立使用者可以直接在程式中定製郵件訊息。
--GKPeerPickerController
儘管該控制器是GameKit的一部分,但其技術也很適合用於非遊戲用途,不如檔案傳輸、訊息傳遞等。
--Media Player 控制器
Media Player 框架提供了幾個控制器,允許選擇和播放音樂、電影。
檢視控制器的作用
-- 檢視控制器的主要職責
管理其檢視的顯示,對事件做出響應,例如,iPhone顯示方向或低記憶體情況等事件。
充當使用者介面和應用程式資料模型之間的協調者(後面我們將詳細介紹IOS中的MVC模式)。
-- 介紹幾個使用檢視控制器時必須瞭解的函式:
(1)loadView (2)viewDidLoad (3)viewDidUnload (4)viewWillAppear
(5)viewDidAppear (6)viewWillDisappear (7)viewDidDisappear
檢視控制器的主要職責
--- 響應方向變化
當iPhone 旋轉的時候,立即呼叫shouldAutorotateToInterfaceOrientation:方法
--處理低記憶體情況
當一個檢視沒有父檢視或者當釋出一條記憶體警告的時候,每個檢視控制器通過didReceiveMemoryWarning方法接受到一條通知。
使用導航控制器新增事件,響應一個自定義方法,程式碼:
//在.h中宣告一個方法
-(IBAction)pressToNextPage:(id)sender;
//在.m檔案中實現這個方法
-(IBAction)pressToNextPage:(id)sender
{
//建立SecondViewController例項物件
SecondViewController* secondVC =
[[[SecondViewController alloc]init] autorelease];
//設定該檢視的標題
secondVC.title = @"第二頁";
//將例項物件secondVC壓入導航控制棧中
[self,navigationController pushViewController:secondVC animated:YES];
}
使用導航控制器
-- 壓入導航棧中程式碼如下:
[self.navigationController pushViewController:檢視例項物件animated:YES];
使用標籤控制器
--我們可以使用UITabBarController類來建立多頁的介面。通過從位於螢幕底部的一個標籤頁欄選擇一個標籤頁來顯示頁面。
--標籤欄控制器的每個頁面都是一個檢視控制器。
在BoAppDelegate.h檔案中:
#import <UIKit/UIKit.h>
@classBoRootViewController;
@interface BoAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (retain, nonatomic) BoRootViewController *mRootVC;
@end
在BoAppDelegate.m檔案中
#import "BoAppDelegate.h"
#import "BoRootViewController.h"
@implementation BoAppDelegate
- (void)dealloc
{
[_mRootVC release];
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColorwhiteColor];
BoRootViewController *pRootVC = [[BoRootViewControlleralloc]initWithNibName:nilbundle:nil];
self.mRootVC = pRootVC;
[pRootVC release];
UINavigationController *pNav = [[UINavigationControlleralloc]initWithRootViewController:self.mRootVC];
self.window.rootViewController = pNav;
[pNav release];
[self.windowmakeKeyAndVisible];
//設定應用程式的小徽章
[UIApplicationsharedApplication].applicationIconBadgeNumber = 2;
returnYES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
@end
在BoRootViewController.h檔案中
#import <UIKit/UIKit.h>
@interface BoRootViewController : UIViewController
@end
在BoRootViewController.m檔案中
#import "BoRootViewController.h"
#import "BoNextViewController.h"
@interfaceBoRootViewController ()
@end
@implementation BoRootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.navigationItem.title = @"根檢視";
[self creatBarItem:nil];
//設定返回按鈕的隱藏
//[self.navigationController setHidesBottomBarWhenPushed:YES];
[selfcreatTitleView:nil];
}
returnself;
}
//建立自定義titleView
- (void)creatTitleView:(id)sender
{
UIView *pTitleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
[pTitleView setBackgroundColor:[UIColorredColor]];
self.navigationItem.titleView = pTitleView;
}
//建立自定義按鈕(barButtonItem)
- (void)creatBarItem:(id)sender
{
UIBarButtonItem *pBar = [[UIBarButtonItemalloc]initWithTitle:@"NextVC"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(goToNextVC:)];
self.navigationItem.rightBarButtonItem = pBar;
}
- (void)goToNextVC:(id)sender
{
BoNextViewController *pNextVC = [[BoNextViewControlleralloc]initWithNibName:nilbundle:nil];
[self.navigationControllerpushViewController:pNextVC animated:YES];
}
- (void)viewDidLoad
{
[superviewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
}
@end
在BoNextViewController.h檔案中
#import <UIKit/UIKit.h>
@interface BoNextViewController : UIViewController
@end
在BoNextViewController.m檔案中
#import "BoNextViewController.h"
@interfaceBoNextViewController ()
@end
@implementation BoNextViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.navigationItem.title = @"第二級檢視";
}
returnself;
}
- (void)viewDidLoad
{
[superviewDidLoad];
[self creatBtn:nil];
}
- (void)creatBtn:(id)sender
{
UIButton *pBtn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[pBtn setFrame:CGRectMake(100, 100, 100, 40)];
[pBtn setTitle:@"TouchMe"forState:UIControlStateNormal];
[pBtn addTarget:selfaction:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pBtn];
}
#pragma mark-----SEL:btnPressed-----
- (void)btnPressed:(id)sender
{
self.navigationController.toolbarHidden = !self.navigationController.toolbarHidden;
[sender setTitle:self.navigationController.toolbarHidden?@"Show":@"hidden"forState:UIControlStateNormal];
self.navigationItem.prompt = @"載入中";
[selfperformSelector:@selector(setPromptNil:) withObject:nilafterDelay:2];
}
- (void)setPromptNil:(id)sender
{
self.navigationItem.prompt = nil;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end