iOS programming UITabBarController,uitabbarcontroller
iOS programming UITabBarController
1.1
View controllers become more interesting when the user's actions can cause another view controller to be presented.
當使用者的action 能導致其他view controller 展現,view controller將會變得更有趣。
UITabBarController keeps an array of view controllers. It also maintains a tab bar at the bottom of the screen with a tab for each view controller in this array.
UITabBarController 擁有一列的view controller .它還為每一個view controller在螢幕下方保持一個tab bar。
Tapping on a tab results in the presentation of the view of the view controller associated with that tab.
點擊一個tab 將會呈現與這個tab 關聯的view controller 的view。
UITabBarController *tabBarController = [[UITabBarController alloc] init]; tabBarController.viewControllers = @[hvc, rvc];
self.window.rootViewController = tabBarController;
UITabBarController is itself a subclass of UIViewController. A UITabBarController's view is a UIView with two subviews: the tab bar and the view of the selected view controller
UITabBarController是UIViewController的一個子類。UITabBarController's的view 是一個含有兩個subview的view。一個subview是tab bar。另一個是被選中的view controller 的view。
1.2 Tab bar items
Each tab on the tab bar can display a title and an image. Each view controller maintains a tabBarItem property for this purpose.
在tab bar上每個tab 都能展現一個title和一張image。為此,每個view controller 有一個tabBarItem 屬性。
(1)Drag these files into the images set list on the left side of the Asset Catalog.
把圖片放到asset catalog中。
(2)In BNRHypnosisViewController.m, override UIViewController's designated initializer,
在UIViewController的指定初始化方法中修改如下:
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.title=@"Hypnotize";
UIImage *i=[UIImage imageNamed:@"Hypno.png"];
self.tabBarItem.image=i;
}
return self;
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.title=@"Reminder";
UIImage *i=[UIImage imageNamed:@"Timer.png"];
self.tabBarItem.image=i;
}
return self;
}
1.3 UIViewCotroller Initializers
When you created a tab bar item for BNRHypnosisViewController, you overrode initWithNibName:bundle:.However, when you initialized the BNRHypnosisViewController instance in BNRAppDelegate.m, you sent it init and still got the tab bar items.
This is because initWithNibName:bundle: is the designated initializer of UIViewController.
當你在uiviewcontroller 裡重寫initWithNibName方法,你雖然在delegate中寫的是init 。仍然被初始化。這是因為initWithNibName:bundle是指定的初始化方法。
Sending init to a view controller calls initWithNibName:bundle: and passes nil for both arguments.
發送init初始化將傳遞給initWithNibName:bundle 的兩個參數都是nil;
What happens if you send init to a view controller that does use a NIB file?
. When a view controller is initialized with nil as its NIB name, it searches for a NIB file with the name of the class. Passing nil as the bundle means that the view controller will look in the main application bundle.
當view controller 用初始化是nil的時候,它將搜尋這個NIB file ,並找到與類方法相同名子的nib。
1.4 Adding a local Notification
. A local notification is a way for an application to alert the user even when the application is not currently running.
一個本地通知是application allert 使用者,甚至在application 沒有啟動並執行時候。
Getting a local notification to display is easy. You create a UILocalNotification and give it some text and a date. Then you schedule the notification with the shared application – the single instance of UIApplication.
建立一個UILocalNotification,給他一些文字和日期。然後規劃notification 用shared applicaton ,也是UIApplication的單例。
UILocalNotification *note=[[UILocalNotification alloc]init];
note.alertBody=@"Hypnotize me";
note.fireDate=date;
[[UIApplication sharedApplication] scheduleLocalNotification:note];
1.5 Loaded and Appearing Views
When the application launches, the tab bar controller defaults to loading the view of the first view controller in its array, the BNRHypnosisViewController. This means that the BNRReminderViewController's view is not needed and will only be needed when (or if) the user taps the tab to see it.
tab bar controller 預設載入在它的列中第一個view controller 的view。
1.6 accessing subviews
you will want to do some extra initialization of the subviews that are defined in the XIB file before they appear to the user.
有時候想做在xib檔案定義的subview的別的初始化工作。
However, you cannot do this in the view controller's initializer because the NIB file has not yet been loaded. If you try, any pointers that the view controller declares
that will eventually point to subviews will be pointing to nil
你不能在view controller 的初始化方法中進行。
So where can you access a subview? There are two main options, depending on what you need to do. The first option is the viewDidLoad method that you overrode to spot lazy loading.The view controller receives this message after the view controller's NIB file is loaded, at which point all of the view controller's pointers will be pointing to the appropriate objects.
有兩種選擇供你擷取 一個subview.第一你可以在viewDidLoad方法中重載。view controller 接受到這個訊息在view controller 的nib檔案載入後,這個時候view controller 所有得指標都指向了適當的位置。
The second option is another UIViewController method viewWillAppear:. The view controller receives this message just before its view is added to the window.
第二個選擇是你可以在viewWillAppear:.在view controller 的view添加到window 之前,view controller 接受這個訊息。
What is the difference? You override viewDidLoad if the configuration only needs to be done once during the run of the app. You override viewWillAppear: if you need the configuration to be done and redone every time the view controller appears on screen.
兩個的區別是viewdidload 用在app運行之後,配置運行一次。而viewwillappear 是每次view controller 出現在螢幕上就配置一次。
This is something that will need to be done every time the view appears, not just once after the view is loaded, so you are going to override viewWillAppear:.
每次view出現,都要重寫,因此用viewWillAppear.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:60];
}
It indicates whether the appearance or disappearance transition is animated or not.
animated 指明apparence or disappearance 是否有動畫。
1.7 Interacting with View controllers and Their views
Let's look at some methods that are called during the lifecycle of a view controller and its view.
在一個view controller 和view的生命週期內要調用的方法:
(1)application:didFinishLaunchingWithOptions: is where you instantiate and set an application's root view controller.
你在這裡設定和初始化應用程式的root view controller.
This method gets called exactly once when the application has launched. Even if you go to another app and come back, this method does not get called again. If you reboot your phone and start the app again, application:didFinishLaunchingWithOptions: will get called again.
(2) initWithNibName:bundle: is the designated initializer for UIViewController.
initWithNibName:bundle: is the designated initializer for UIViewController.
When a view controller instance is created, its initWithNibName:bundle: gets called once. Note that in some apps, you may end up creating several instances of the same view controller class. This method will get called once on each as it is created.
(3)loadView: is overridden to create a view controller's view programmatically.
(4)viewDidLoad can be overridden to configure views created by loading a NIB file. This method gets called after the view of a view controller is created.
(5)viewWillAppear: can be overridden to configure views created by loading a NIB file.
This method and viewDidAppear: will get called every time your view controller is moved on screen. viewWillDisappear: and viewDidDisappear: will get called every time your view controller is moved offscreen. So if you launch the app you are working on and hop back and forth between Hypnosis and Reminder, BNRReminderViewController's viewDidLoad method will be called once, but viewWillAppear: will be called dozens of times.