IPhone development (5) --- programming custom UIViewController

Source: Internet
Author: User

As shown in the third lecture, even if the XIB file is not used, you can rewrite the viewDidLoad function to configure any view or Controller. Here we will look at how to program and customize such views and controllers.

First, if the XIB file cannot be found in the init method of UIViewController, A UView object is automatically created and you can use viewDidLoad to log on to the system. Therefore, we can implement the viewDidLoad method when customizing the UIViewController and use the view as the subview.

In this example, the background of the view is blue and a UIButton is set on it.

Step 1: Define the CustomViewController class in the CustomViewControllerAppDelegate. m file.

@ Interface CustomViewController: UIViewController {
}
@ End


At the same time, this instance is implemented in the CustomViewControllerAppDelegate. h file.

@ Class CustomViewController;

@ Interface CustomViewControllerAppDelegate: NSObject {
UIWindow * window;
CustomViewController * controller;
}

@ Class CustomViewController is similar to class declaration in C ++.
Because access to external objects is not required, there is no @ property declaration.


The mviewcontroller instance is generated in the member function applicationDidFinishLaunching of the CustomViewControllerAppDelegate class, and then added the view in the CustomViewController instance with addSubview. Finally, when CustomViewControllerAppDelegate is released (dealloc), its instance is released. The Code is as follows:

-(Void) applicationDidFinishLaunching :( UIApplication *) application {
ViewController = [[CustomViewController alloc] init];
[Window addSubview: viewController. view];
[Window makeKeyAndVisible];
}

-(Void) dealloc {
[Window release];
[Controller release];
[Super dealloc];
}

Use window addSubview to represent the original view.


Then, declare and implement CustomViewController in the following simple way. In the viewDidLoad function of CustomViewController, set the background color to blue.

@ Interface CustomViewController: UIViewController {
}
@ End

@ Implementation CustomViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
Self. view. backgroundColor = [UIColor blueColor];
}
@ End


Compile and execute the command. The following result is displayed.

 

Next we will add a button. We will dynamically generate a UIButtonTypeInfoLight button. After setting the frame of the button, we will add it to the view with addSubview.
@ Implementation CustomViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
Self. view. backgroundColor = [UIColor blueColor];
UIButton * button = [UIButton buttonWithType: UIButtonTypeInfoLight];
Button. frame = CGRectMake (100,100,100,100 );
[Self. view addSubview: button];
}
@ End


The final effect is as follows:

 

Next let's talk about the specific button actions

Author: Yi Feiyang

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.