iOS programming 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。 

iOS programming UITabBarController_第1张图片

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 属性。iOS programming UITabBarController_第2张图片

(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;

}

 

 

 

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.

 

 

 

 

 

你可能感兴趣的:(programming)