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。
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的更多相关文章
- Head First iOS Programming
内部分享: Head First iOS Programming http://www.slideshare.net/tedzhaoxa/head-first-ios-programming-4606 ...
- 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 ...
- iOS Programming Autorotation, Popover Controllers, and Modal View Controllers
iOS Programming Autorotation, Popover Controllers, and Modal View Controllers 自动旋转,Popover 控制器,Moda ...
- iOS Programming Controlling Animations 动画
iOS Programming Controlling Animations 动画 The word "animation" is derived from a Latin wor ...
- iOS Programming UIStoryboard 故事板
iOS Programming UIStoryboard In this chapter, you will use a storyboard instead. Storyboards are a f ...
- iOS Programming NSUserDefaults
iOS Programming NSUserDefaults When you start an app for the first time, it uses its factory settin ...
- iOS Programming Localization 本地化
iOS Programming Localization 本地化 Internationalization is making sure your native cultural informatio ...
- iOS Programming State Restoration 状态存储
iOS Programming State Restoration 状态存储 If iOS ever needs more memory and your application is in the ...
- iOS Programming UISplitViewController
iOS Programming UISplitViewController The iPad, on the other hand, has plenty of screen space to pr ...
随机推荐
- Using Python with TurboGears A complete web framework integrating several Python projects
Using Python with TurboGears TurboGears is a Python web framework based on the ObjectDispatch paradi ...
- vs2010 创建和发布 webservice
1 打开VS2010,菜单 文件->新建->项目 2 选择[ASP.net 空web应用程序],将其命名为自己想的工程名称. 3 右键点击工程,添加->新建项 选择 web服务 ...
- Entity Framework底层操作封装V2版本号(3)
如今是附加的,组合查询须要的扩展类.大家知道lanmda表达式的组合条件比較麻烦,所以就加了一样一个类,方便进行组合查询: using System; using System.Collections ...
- UITableview控件简单介绍
注意点:数据源方法只能在控制器里设置 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView ...
- 并不对劲的bzoj1305: [CQOI2009]dance跳舞
传送门-> 又是陈年老坑. 听上去不知道从何下[手]?那要是把题目换成“判断这些人能否条x支舞”呢? 这样就变成了一个网络流可以解决的问题,只要把每个人拆成喜欢和不喜欢两点,每个人两点总流量不超 ...
- bzoj1047 [HAOI2007]理想的正方形——二维单调队列
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1047 就是先对行做一遍单调队列,再对那个结果按列做一遍单调队列即可. 代码如下: #incl ...
- 杂项:WebRTC
ylbtech-杂项:WebRTC WebRTC,名称源自网页实时通信(Web Real-Time Communication)的缩写,是一个支持网页浏览器进行实时语音对话或视频对话的技术,是谷歌20 ...
- ubuntu 12.04上安装QQ2013(转载)
转自:http://www.cnblogs.com/wocn/p/linux_ubuntu_QQ_install.html 环境介绍: OS:Ubuntu12.04 64bit QQ:WineQQ20 ...
- Tomcat配置JVM运存
setenv.bat set "JAVA_OPTS=-server -Xms256M -Xmx1024M -XX:PermSize=256m -XX:MaxPermSize=1024m&qu ...
- TCP协议中的三次握手和四次挥手(图解)【转载】
建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的. 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资 ...