Mais conteúdo relacionado Semelhante a I os 10 (20) I os 102. 內容⼤大綱
• ViewController
• UIApplication, Window,Views,ViewControllers
• UINavigationController
• QV031:逐步完成導覽列控制器
• Samples:NavBar
• TabbarController
• QV033:Tabbar Controller 標籤列
• UIToolbar 使⽤用及轉場動畫
• QV039:使⽤用根控制器,切換畫⾯面
• QV040:畫⾯面切換的動畫
• 多⾴頁⾯面處理
5. Window,Views,ViewControllers
• Window
• Every application has at least one window
• Blank canvas to host views
• View
• Draw and animate content
• Layout subviews
• Receive and forward events
• View Controller
• Manages a self-contained view hierarchy
6. UIApplication
• Set up main run loop
• Manages event queue
• Custom behavior through delegation
• UIApplicationDelegation
7. View Controllers
• Every screen should have its own controller
• UIViewController implements much of the iOS
standard interface behavior
• Loading nib file
• Working with navigation, tab, and tool bars
• Composing multiple view controllers
• Handling events and memory warnings
• Managing interface orientation change
14. Type of View Controllers
• Custom view controller
• Directly express content on the screen
• Container view controller
• Manage other view controllers
• Usually does not express content directly
• Modal view controller
• Any view controller can be presented modally
28. AppDelegate.h (1) 注意是寫在AppDelegate.h 及
AppDelegate.m上
@interface ViewController : UIViewController
{
UINavigationController *navController; (2) 先加⼊入導覽列物件
}
@end
(3) 在App初始化時就加⼊入導覽列
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController"
bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// 加⼊入導覽列物件
navController = [[UINavigationController alloc] init];
[self.window addSubview:navController.view];
return YES;
}
34. (1) ⾃自訂類別名稱
(2) 確認UIViewController
(3) 勾選以產⽣生 .xib 檔案
會產⽣生三個檔案
畫⾯面內容可⾃自⾏行設計
35. 新增的 RootView 仍是在
AppDelegate.h AppDelegate 內引⽤用
#import <UIKit/UIKit.h>
#import "RootView.h"
(1) 宣告 RootView
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navController;
}
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
// 部分程式省略
(2) 加⼊入 RootView
// 加⼊入導覽列物件
navController = [[UINavigationController alloc] init]; 並推⼊入導覽列
[self.window addSubview:navController.view];
// 宣告根畫⾯面物件,並將它加⼊入導覽列 (同時會顯⽰示出來)
RootView *rootView = [[RootView alloc] init];
[navController pushViewController:rootView animated:NO];
return YES;
}
40. #import <UIKit/UIKit.h>
RootView.h
#import "Page1View.h"
#import "Page2View.h"
加⼊入兩個⼦子類別
@interface RootView : UIViewController
{
}
- (IBAction)goPage1:(id)sender;
- (IBAction)goPage2:(id)sender;
@end
將⼀一個⼦子畫⾯面類別
RootView.m #import "RootView.h"
推⼊入導覽列中
@implementation RootView
有換場動畫
-(IBAction)goPage1:(id)sender
{
Page1View *page1 = [[Page1View alloc] init];
[self.navigationController pushViewController:page1 animated:YES];
}
-(IBAction)goPage2:(id)sender
{
Page2View *page2 = [[Page2View alloc] init];
[self.navigationController pushViewController:page2 animated:YES];
}
44. RootView.m
-(IBAction)goPage1:(id)sender
{
Page1View *page1 = [[Page1View alloc] init];
[self.navigationController pushViewController:page1 animated:YES];
page1.navigationItem.title = @"iPhone";
}
-(IBAction)goPage2:(id)sender
{
Page2View *page2 = [[Page2View alloc] init];
[self.navigationController pushViewController:page2 animated:YES];
page2.navigationItem.title = @"iPad";
}
46. AppDelegate.m
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 部分程式省略
// 加⼊入導覽列物件
navController = [[UINavigationController alloc] init];
[navController.navigationBar setTintColor:[UIColor redColor]];
[self.window addSubview:navController.view];
// 設定back的按鈕
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:@"回前⾴頁"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
// 宣告根畫⾯面物件,並將它加⼊入導覽列 (同時會顯⽰示出來)
RootView *rootView = [[RootView alloc] init];
[navController pushViewController:rootView animated:NO];
rootView.navigationItem.title = @"蘋果產品";
rootView.navigationItem.backBarButtonItem = backButton;
return YES;
}
50. 參考解答
RootView.m
-(IBAction)goPage1:(id)sender
{
Page1View *page1 = [[Page1View alloc] init];
page1.navigationItem.title = @"iPhone";
[self.navigationController pushViewController:page1 animated:YES];
// 設定back的按鈕
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:@"Go Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
}
54. 範例練習:
開啟新專案
『Tabbed Application』
57. 說明
T Bar 控制器簡介
ab
在某畫⾯面之後新增 T Bar Controller
ab
增加 T Bar 切換畫⾯面
ab
⾃自訂 T Bar 按鈕外觀
ab
59. 預先準備好各畫⾯面
注意此範例將UITabBarController 置於
ViewController畫⾯面 (如要⼀一開始就出現,
則可置於 AppDelegate 類別內
62. #import "ViewController.h"
#import "Page1View.h" ViewController.m
#import "Page2View.h"
#import "Page3View.h"
#import "Page4View.h"
@implementation ViewController (2)按鈕按下時
才顯⽰示出來
- (IBAction)showTabBar:(id)sender
{
[self.view addSubview:tabBarController.view];
}
- (void)viewDidLoad
{
[super viewDidLoad];
Page1View *page1 = [[Page1View alloc] init]; (1)置⼊入標籤列
Page2View *page2 = [[Page2View alloc] init];
Page3View *page3 = [[Page3View alloc] init];
Page4View *page4 = [[Page4View alloc] init];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:
page1, page2, page3, page4, nil];
}
64. - (void)viewDidLoad
{
[super viewDidLoad]; ViewController.h
Page1View *page1 = [[Page1View alloc] init];
Page2View *page2 = [[Page2View alloc] init];
Page3View *page3 = [[Page3View alloc] init];
Page4View *page4 = [[Page4View alloc] init];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:
page1, page2, page3, page4, nil];
// 加⼊入TabBar項⺫⽬目的圖⽂文 (系統內建的)
UITabBarItem *item1 = [[UITabBarItem alloc]
initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0];
UITabBarItem *item2 = [[UITabBarItem alloc]
initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
UITabBarItem *item3 = [[UITabBarItem alloc]
initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0];
UITabBarItem *item4 = [[UITabBarItem alloc]
initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];
// 指定到各個ViewController物件 (注意:不是指定在UITabBarController)
page1.tabBarItem = item1;
page2.tabBarItem = item2;
page3.tabBarItem = item3;
page4.tabBarItem = item4;
}
65. 系統內建的標籤項⺫⽬目
typedef enum {
UITabBarSystemItemMore,
UITabBarSystemItemFavorites,
UITabBarSystemItemFeatured,
UITabBarSystemItemTopRated,
UITabBarSystemItemRecents,
UITabBarSystemItemContacts,
UITabBarSystemItemHistory,
UITabBarSystemItemBookmarks,
UITabBarSystemItemSearch,
UITabBarSystemItemDownloads,
UITabBarSystemItemMostRecent,
UITabBarSystemItemMostViewed,
} UITabBarSystemItem;
66. ⾃自訂標籤項⺫⽬目⽂文字
// ⾃自⾏行設定⽂文字 (無圖⽚片)
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"藍⾊色" image:nil tag:0];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"綠⾊色" image:nil tag:0];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"紅⾊色" image:nil tag:0];
UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:@"⿈黃⾊色" image:nil tag:0];
⾃自訂標籤項⺫⽬目⽂文字及圖⽚片
// ⾃自⾏行設定⽂文字及圖⽚片
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"藍⾊色"
image:[UIImage imageNamed:@"Tab_B.png"] tag:0];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"綠⾊色"
image:[UIImage imageNamed:@"Tab_G.png"] tag:0];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"紅⾊色"
image:[UIImage imageNamed:@"Tab_R.png"] tag:0];
UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:@"⿈黃⾊色"
image:[UIImage imageNamed:@"Tab_Y.png"] tag:0];
68. 另⼀一種寫法:⾃自訂標籤項⺫⽬目⽂文字及圖⽚片
// 指定到各個ViewController物件 (注意:不是指定在UITabBarController)
/*
page1.tabBarItem = item1;
page2.tabBarItem = item2;
page3.tabBarItem = item3;
此⽅方法不需要指定建⽴立UIT abBarItem物件
page4.tabBarItem = item4;
*/
// 也可以⽤用另⼀一種⽅方式,直接指定各Viewcontroller的title屬性
page1.title = [[NSString alloc] initWithFormat:@"藍⾊色"];
page2.title = [[NSString alloc] initWithFormat:@"綠⾊色"];
page3.title = [[NSString alloc] initWithFormat:@"紅⾊色"];
page4.title = [[NSString alloc] initWithFormat:@"⿈黃⾊色"];
page1.tabBarItem.image = [UIImage imageNamed:@"Tab_B"];
page2.tabBarItem.image = [UIImage imageNamed:@"Tab_G"];
page3.tabBarItem.image = [UIImage imageNamed:@"Tab_R"];
page4.tabBarItem.image = [UIImage imageNamed:@"Tab_Y"];
70. 有建議將ViewController的狀態列
設為無,但似乎無效!!!
必須增加這⼀一列程式
{
[super viewDidLoad];
self.wantsFullScreenLayout = YES; // 避免TabBar往下偏移
Page1View *page1 = [[Page1View alloc] init];
Page2View *page2 = [[Page2View alloc] init];
Page3View *page3 = [[Page3View alloc] init];
Page4View *page4 = [[Page4View alloc] init];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:
page1, page2, page3, page4, nil];
79. AppDelegate.h
AppDelegate.m
BlueViewController.h
BlueViewController.m
SwitchViewController.h
BlueViewController.xib
SwitchViewController.m
SwitchViewController.xib
YellowViewController.h
YellowViewController.m
YellowViewController.xib
81. AppDelegate.m
#import "AppDelegate.h"
#import "SwitchViewController.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize switchViewController;
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]
bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//[self.window addSubview:switchViewController.view]; // 不能使⽤用
self.switchViewController = [[SwitchViewController alloc]
initWithNibName:@"SwitchViewController" bundle:nil];
self.window.rootViewController = self.switchViewController;
[self.window makeKeyAndVisible];
return YES;
}
82. ViewController.h
#import <UIKit/UIKit.h>
@class BlueViewController;
@class YellowViewController;
@interface SwitchViewController : UIViewController
{
BlueViewController *blueViewController;
YellowViewController *yellowViewController;
}
@property (retain,nonatomic) BlueViewController *blueViewController;
@property (retain,nonatomic) YellowViewController *yellowViewController;
- (IBAction)switchViews:(id)sender;
@end
84. #import "SwitchViewController.h"
#import "BlueViewController.h" ViewController.m (1/3)
#import "YellowViewController.h"
@implementation SwitchViewController
@synthesize blueViewController;
@synthesize yellowViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
blueViewController = [[BlueViewController alloc]
initWithNibName:@"BlueViewController" bundle:nil];
[self.view insertSubview:blueViewController.view atIndex:0];
// [super viewDidLoad];
} 這裡不能⽤用addSubView,為什麼?
85. ViewController.m (2/3)
- (IBAction)switchViews:(id)sender
{
if(self.yellowViewController.view.superview==nil)
{
if(self.yellowViewController==nil)
{
yellowViewController = [[YellowViewController alloc]
initWithNibName:@"YellowViewController" bundle:nil];
}
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
}
else
{
if(self.blueViewController==nil)
{
blueViewController = [[BlueViewController alloc]
initWithNibName:@"BlueViewController" bundle:nil];
}
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:blueViewController.view atIndex:0];
}
}
為何要這麼⿇麻煩!為何不⼀一開始就將藍⿈黃
兩個ViewController都叫出來再切換順序?
86. ViewController.m (3/3)
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
if(self.blueViewController.view.superview==nil)
self.blueViewController = nil;
else
self.yellowViewController = nil;
}
多個畫⾯面的程式中,記憶體管理變得更加重要
91. - (IBAction)switchViews:(id)sender ViewController.m
{
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
if(self.yellowViewController.view.superview==nil)
{
if(self.yellowViewController==nil)
{
yellowViewController = [[YellowViewController alloc]
initWithNibName:@"YellowViewController" bundle:nil];
}
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self.view cache:YES];
[blueViewController viewWillAppear:YES];
[yellowViewController viewWillDisappear:YES];
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
[yellowViewController viewDidDisappear:YES];
[blueViewController viewDidAppear:YES];
}
else
{
// 部分程式省略
} 僅在ViewController.m中增加程式
[UIView commitAnimations];
}
95. 多⾴頁⾯面處理的演進
• 各⾃自為政
• ⼀一個 ViewController 控制全部的項⺫⽬目
• 各個 ViewController 控制各⾃自的 View
• 建置更上層的控制器,管理各個 ViewController
• UIToolbarController
• UITabbarController
• UINavigationController
• 更複雜的 iPad
• UISplitViewController
• UIPopViewController
100. 不⼀一定要弄成許多個
ViewController
• UITableView Controller
• UIPageView Controll
• UIScrollView Controller
• ......