IPhone DevelopmentMedium programming CustomizationUIViewControllerThe case implementation is what we will introduce in this article. As you can see in the previous section, even if you do not use the XIB file, 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, ifUIViewControllerIf the XIB file cannot be found in the init method of, 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, dealloc is released when CustomViewControllerAppDelegate 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:
Summary:IPhone DevelopmentProgramming CustomizationUIViewControllerThe implementation of the case is complete. I hope this article will help you! For more information, see the following articles:
IPhone Development (advanced tutorial) Implementation of iPhone application project composition
IPhone Development 2) iPhone application startup process
IPhone Development (Advanced 4) UIButton Case Study