iPhone編程規則是: 一個窗口,多個視圖 。UIView是iPhone屏幕上很多控件的基礎類。每個iPhone用戶界面都是由顯示在UIWindow(這其實也是個特殊的UIView)內的眾多UIView及其專門化子類構建的。
顯示數據的視圖:
- UITextView
- UILabel
- UIImageView
- UIWebView
- MKMapViews
- UIScrollView
可選擇的視圖 (以下兩個視圖類實例都是模態顯示的) :
- UIAlertView
- UIActionSheet?
所有控件 (控件是將用戶觸摸轉換為回調觸發器的屏幕對象。) :
- UIButton
- UISegmentedControl
- UISwitch
- UISlider
- UIPageControl
- UITextField
表格和拾取器:
- UITableView (對于表格的行有一個UITableViewCell類的表格單元項)
- UIPickerView
各種欄樣式:
- UINavigationBar (對于導航欄有一個UINavigationItem類的導航項)
- UITabBar
- UISearchBar
- UIToolBar
在所有iPhone欄樣式視圖中,只有UIToolBar類可以直接使用。其它三個都是通過對應的控制器類來完成工作,而不是直接構建和管理視圖。?
進度與活動:
- UIActivityIndicatorView
- UIProgressView?
?
?
控制器類
?
UIViewController類
? ? ? UIViewController負責創建其管理的視圖及在低內存的時候將它們從內存中移除。還為標準的系統行為進行響應。從語法上說,UIViewController是視圖控制器的父類。視圖控制器類是沒有可視化表示的抽象類,只有它管理的視圖才提供可視畫布。記住:1. 視窗UIWindow雖然也是UIView,但不能用處理普通UIView的方式來設置; 2. UIView視圖使用initWithFrame:初始化,而其UIViewController則使用init類方法直接初始化。3. UIViewController實例負責設置視圖的外觀和它顯示的子視圖。
?
UINavigationController類
1. 導航控制器使用內置動畫在視圖之間切換; 2. 導航控制器自動構建并處理Back按鈕; 3. 導航控制器提供簡單的菜單欄可幫助用戶進行自定義控件。4. 每一個導航控制器都擁有一個根視圖控制器RootViewController,它是“導航彈出棧”的基礎,是根。5. 通過調用initWithRootViewController:方法可以將特定控制器設置為根。如常見的:
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[MyViewController alloc] init]]; [window addSubview:nav.view];?
?
UINavigationController如何推入和彈出視圖控制器?
?
1. 使用 pushViewController: animated: 可推入一個新的控制器,從而增加新的項到導航棧。(記住:導航欄控制器不添加一個視圖進去,這個導航欄是沒有意義的!)
//創建一個導航控制器 UINavigationController *aNav = [[UINavigationController alloc] init]; //創建一個要被推入的視圖控制器,然后添加此視圖控制器到導航欄并予以顯示 UIViewController *aView = [[UIViewController alloc] initWithNibName:(*xib文件名*)]; [aNav pushViewController:aView animated:NO]; //這里假定是導航欄的第一個視圖,所以不要動畫化。?
推入時,新的視圖控制器從右方滑入屏幕(假定animated:YES)。向左指的Back后退按鈕出現,可返回到上一步,且Back按鈕用的是上一個視圖控制器的標題。
2. 作為彈出回上一級視圖控制器的Back按鈕是自動產生并處理的,無需用戶干預。
3. 可使用 popViewControllerAnimated:BOOL 彈出當前視圖控制器并向左顯示前一個視圖。
4. 可使用 popToViewController: animated: 彈出到指定的視圖控制器,即不一定是上一個。
5. 可使用 popToRootViewControllerAnimated:BOOL 直接彈出到根視圖控制器。
6. 設置導航欄的按鈕并不是去設置導航欄本身。一切都是在被推入的UIViewController子類內部執行推入請求和相關導航欄的定制(如:右鍵按鈕)。
?
導航欄可以通過直接訪問 navigationBar 屬性,改變欄的風格或者它的顏色:
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
?
提示:UIViewController 有一個屬性是navigationController,如果當前ViewController在某個NavigationController的堆棧中 的話(即是被推送過來的),則視圖控制器的這個屬性就指向那個NavigationController,否則為nil。
?
要添加或修改導航欄按鈕,使用UINavigationItem抽象類。其描述了導航欄上顯示的內容,而正好UIViewController另有一導航項屬性 navigationItem 包括左欄按鈕( leftBarButtonItem )、右欄按鈕( rightBarButtonItem )和欄標題( title )、用于顯示標題的視圖(titleView),以及用于從當前視圖向后導航的Back按鈕( backBarButtonItem )和隱藏后退按鈕( hidesBackButton )。
?
注意:對于導航欄定制,對定制實際標題的最簡單方式時使用子視圖控制器而不是導航項的title屬性:
self.title = @"Hello"; self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]; //可使標題自動反映運行的應用程序名稱?
?導航控制器加載模態視圖控制器:
?通常情況下,模態視圖控制器用于選取數據。在發送 presentModalViewController: animated: 消息后,一個新的視圖控制器(其實是個ModalViewController)會滑動到屏幕并掌握控制權,直到使用 dismissModalViewControllerAnimated: 將它隱藏才重新得到控制權。
?
對于導航控制器來說,viewWillAppear方法是沒有結果的,要實現UINavigationControllerDelegate委托方法willShowViewController: animated:達到目的。
?
UITabBarController類
? ? ? 選項卡類允許用戶在多個視圖控制器之間移動并在屏幕的底部可定制該欄。向不同的視圖同時提供一次單擊訪問,向用戶選擇的屏幕和編輯底欄的屏幕同時提供More按鈕。
? ? ? 選項卡類的方便之處就是
不需要象導航欄那樣以棧的方式推入和彈出視圖的操作
,而是組建一系列的控制器(它們各自可以是UIViewController、UINavigationController、UITableViewController或其他任何類型的視圖控制器),并通過設置欄的
viewControllers屬性
將其添加到選項卡欄,使每個選項卡對應一個試圖控制器。
一、創建UITabBarController
-
常見的在應用程序委托類的applicationDidFinishLaunching:方法中創建,這通常可為應用窗口提供根視圖。
// 創建一個UITabBarController對象 UITabBarController *tabBarController = [[UITabBarController alloc] init]; // tabBarController.delegate = self; // 創建一系列要添加到每個具體Tab卡的視圖控制器 MyViewController *vc1 = [[MyViewController alloc] init]; MyOtherViewController *vc2 = [[MyViewController alloc] init]; // 將創建好的這些視圖控制器先添加到一個Array對象中,再將此數組分配給Tab Bar Controller的viewControllers屬性 NSArray *controllers = [NSArray arrayWithObjects:vc1,vc2,nil]; tabBarControllers.viewControllers = controllers; /* 也或者 NSMutableArray *controllers =[[NSMutableArray alloc] initWithCapacity:2]; [controllers addObject:vc1]; [controllers addObject:vc2]; */ // 將TabBar控制器的當前視圖添加到窗口 [window addSubview:tabBarController.view];
-
在實現UITabBarControllerDelegate委托的視圖控制器中
重寫init方法
來自定義UITabBarItem條目。
代碼一 是由initWithNibName:方法來加載具體的某個視圖控制器并自定義該控制器中TabBarItem樣式外觀等。
- (id)init { if(self = [super initWithNibName:@"MyViewController" bundle:nil]) { self.title = @"My View Controller"; UIImage *anImage = [UIImage imageNamed:@"MyImage.png"]; UITabBarItem *theItem = [[UITabBarItem alloc] initWithTitle:@"Home" image:anImage tag:0]; self.tabBarItem = theItem; [theItem release]; } return self; }
? 代碼二 我們也可以直接在具體的視圖控制器內部重寫init方法。- (id)init { if([super init] != nil) { UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Home" image:[UIImage imageNamed:@"MyImage.png"] tag:0]; self.tabBarItem = item; [item release]; } return self; }
-
實現所需的委托方法,以讓UITabBarController能夠正常觸發這些回調方法。
tabBarController:didSelectViewController: 是當用戶選擇一個新的選項卡時,控制器會發送這個消息。
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { // 捕捉selectedIndex,可確定當前所選選項卡 NSNumber *tabNumber = [NSNumber numberWithInt:[tabBarController selectedIndex]]; // 使用iPhone內置用戶默認系統NSUserDefaults,利用setObject:forKey:為關鍵字設定值 [[NSUserDefaults standardUserDefaults] setObject:tabNumber forKey:@"selectedTab"]; [[NSUserDefaults standardUserDefaults] synchronize]; // 選項卡圖標項右上角紅色小紅圈數字提示 viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",80]; }
?
來源: http://www.cnblogs.com/lovecode/archive/2012/01/03/2310866.html
?
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
