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;

}

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.

iOS programming UITabBarController的更多相关文章

  1. Head First iOS Programming

    内部分享: Head First iOS Programming http://www.slideshare.net/tedzhaoxa/head-first-ios-programming-4606 ...

  2. iOS Programming Recipe 6: Creating a custom UIView using a Nib

    iOS Programming Recipe 6: Creating a custom UIView using a Nib JANUARY 7, 2013 BY MIKETT 12 COMMENTS ...

  3. iOS Programming Autorotation, Popover Controllers, and Modal View Controllers

    iOS Programming Autorotation, Popover Controllers, and Modal View Controllers  自动旋转,Popover 控制器,Moda ...

  4. iOS Programming Controlling Animations 动画

    iOS Programming Controlling Animations 动画 The word "animation" is derived from a Latin wor ...

  5. iOS Programming UIStoryboard 故事板

    iOS Programming UIStoryboard In this chapter, you will use a storyboard instead. Storyboards are a f ...

  6. iOS Programming NSUserDefaults

    iOS Programming NSUserDefaults  When you start an app for the first time, it uses its factory settin ...

  7. iOS Programming Localization 本地化

    iOS Programming Localization 本地化 Internationalization is making sure your native cultural informatio ...

  8. iOS Programming State Restoration 状态存储

    iOS Programming State Restoration 状态存储 If iOS ever needs more memory and your application is in the ...

  9. iOS Programming UISplitViewController

    iOS Programming UISplitViewController  The iPad, on the other hand, has plenty of screen space to pr ...

随机推荐

  1. 2016/3/20 数组定义 数组遍历 超全局数组 数组元素设置(in_array() 、array_reverse()、count()、array_unique()、unset()、array_values、array_merge、array_push) 列表实例

    一.数组定义 php数组与其他语言的数组的不同: 其他例如java语言 :同一种类型数据的集合. php:数组可以存储任何类型的数据.同一个数组中可以放int类型也可以放string类型 ①索引数组的 ...

  2. linux下jiffies定时器和hrtimer高精度定时器【转】

    本文转载自:http://blog.csdn.net/dosculler/article/details/7932315 一.jiffies定时器,HZ=100,精度只能达到10ms. 注:采用jif ...

  3. 使用cgroups限制MongoDB的内存使用

    cgroups,其名称源自控制组群(control groups)的简写,是Linux内核的一个功能,用来限制,控制与分离一个进程组群的资源(如CPU.内存.磁盘输入输出等). 这个项目最早是由Goo ...

  4. 【转载】CAS操作

    [本文转载]http://blog.csdn.net/hsuxu/article/details/9467651 CAS CAS:Compare and Swap, 翻译成比较并交换. java.ut ...

  5. Android中string.xml中的的标签xliff:g(转载)

    转自:http://blog.csdn.net/xuewater/article/details/25687987 在资源文件中写字符串时,如果这个字符串时动态的,又不确定的值在里面,我们就可以用xl ...

  6. 利用插件(jQuery-ui.js)实现表格行的拖拽排序

    template 模板(html) 首先要引入jQuery-ui.js的文件.import './../../scripts/base/jquery/jquery-ui.min.js';<tab ...

  7. python 面向对象一 OOP

    一.面向对象和面相过程 面向对象编程——Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面 ...

  8. bzoj 4540: [Hnoi2016]序列【单调栈+线段树】

    强烈安利:http://blog.csdn.net/qq_34637390/article/details/51313126 这篇讲标记讲的非常好,这个标记非常神奇-- 首先last表示扫描到last ...

  9. Linux下firefox安装flash player插件

    下载插件 解压插件 使用命令tar -zxvf install_xxxxxx libflashplayer.so 拷贝目录 然后把切换到root用户,把文件夹拷贝到/usr/lib/mozilla/p ...

  10. [App Store Connect帮助]八、维护您的 App(4.3)回复顾客评论(iOS、macOS 或 watchOS)

    您可以公开回复顾客评论,但在您的 App Store 产品页上每条评论仅会显示一条回复.您可以回复评论.编辑回复,以及删除回复. 在回复和编辑显示在 App Store 上之前(可能需要至多 24 小 ...