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. Django模板语言(一)

    1,Django模板语言 1.1>基础语法:1.1.1,变量相关:{{ 变量名 }},1.1.2,逻辑相关{% ... %} 在Django模板语言中按此语法使用:{{ 变量名 }},当模板引擎 ...

  2. Element is not clickable at point SeleniumWebdriverException

    Element is not clickable at point SeleniumWebdriverException | Selenium Easy http://www.seleniumeasy ...

  3. 深入分析linux调度机制

    一.说明 本文以linux-2.4.10 为例主要分析Linux 进程调度模块中的schedule 函数及其相关的函数.另外相关的前提知识也会说明.默认系统平台是自己的i386 架构的pc. 二.前提 ...

  4. 在 Vim 中优雅地查找和替换 vi【转】

    本文转载自:http://harttle.land/2016/08/08/vim-search-in-file.html 总有人问我 Vim 中能不能查找,当然能!而且是超级强的查找! 这篇文章来详细 ...

  5. [开源下载] 【开源项目】EasySL for Silverlight 4

    [开源下载] [开源项目]EasySL for Silverlight 4  [复制链接]       silverlight 452 主题 7 好友 1万 积分 管理员 贡献 879 原创 0 银元 ...

  6. SPOJ:NT Games(欧拉函数)

    Katniss Everdeen after participating in Hunger Games now wants to participate in NT Games (Number Th ...

  7. 【HDU 1561】 The More,The better

    [题目链接] 点击打开链接 [算法] 树形背包 注意是一棵森林 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN ...

  8. python编写九九乘法表代码

    打印九九乘法表 代码: #!/usr/bin/env python # -*- coding: UTF-8 -*- # 项目二: # 1.要求:编写九九乘法表 # 2.分析: # 根据九九乘法表的样式 ...

  9. win10系统安装loadrunner11提示“为了对电脑进行保护 已经阻止此应用”的解决方案

    在执行loadrunner安装包中的setup.exe时会有如下提示: 解决方法:点击Win+R快捷键打开运行,输入“ gpedit.msc”按下回车键打开组策略编辑: 在左边选择[计算机配置]→[W ...

  10. TCP/IP协议的建立连接与关闭连接过程

    一.建立连接(三次握手) 第一次握手:建立连接时,客户端发送SYN(seq=x)包到服务器,并进入SYN_SENT状态,等待服务器的确认.SYN:同步序列编号(Synchronize Sequence ...