IOS programming UITabBarController, uitabbarcontroller

Source: Internet
Author: User

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.

When a user's action can lead to other view controller presentations, view controller will become more interesting.

 

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 has a view controller column. It also maintains a tab bar for each view controller at the bottom of the screen.

Tapping on a tab results in the presentation of the view controller associated with that tab.

Click a tab to display the view controller associated with the tab.

 

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 is a subclass of UIViewController. UITabBarController's view is a view containing two subviews. A subview is a tab bar. The other is the view of the selected view controller.

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.

Each tab on the tab bar displays a title and an image. Therefore, each view controller has a tabBarItem attribute.

(1) Drag these files into the images set list on the left side of the Asset Catalog.

Put the image in asset catalog.

(2) In BNRHypnosisViewController. m, override UIViewController's designated initializer,

Modify the following in the specified Initialization Method of 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.

When you override the initWithNibName method in uiviewcontroller, you write init in delegate. Still initialized. This is because initWithNibName: bundle is the specified initialization method.

Sending init to a view controller CILS initWithNibName: bundle: and passes nil for both arguments.

Sending init initialization will be passed to initWithNibName: Both parameters of bundle are 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.

When the view controller uses nil for initialization, it searches for the NIB file and finds the nib with the same name as the class method.

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.

A local notification is a user of application allert, even when the application is not running.

 

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.

Create a UILocalNotification and give it some text and date. Then we plan to use shared applicaton for notification, which is also a single example of 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.

By default, tab bar controller loads the view of the first view controller in its column.

 

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.

Sometimes you want to initialize the subview defined in the xib file.

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

You cannot perform this operation in the initialization method of 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 has es 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.

There are two options for you to obtain a subview. First, you can reload them in the viewDidLoad method. View controller receives this message. After loading the nib file of view controller, all the pointers of view controller point to the appropriate position.

The second option is another UIViewController method viewWillAppear:. The view controller has es this message just before its view is added to the window.

The second option is that you can accept the message before viewWillAppear:. adds the view of the view controller to the window.

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.

The two differences are that viewdidload is configured to run once after the app is run. Viewwillappear is configured every time the view controller appears on the screen.

 

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 :.

Every view appears, it must be rewritten. Therefore, viewWillAppear is used.

 

-(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 indicates whether apparence or disappearance has an animation.

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.

The method to be called in the life cycle of a view controller and view:

(1) application: didFinishLaunchingWithOptions: is where you instantiate and set an application's root view controller.

You can set and initialize the root view controller of the application here.
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.

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.