IPhone Development (3) custom UIViewController case implementation

Source: Internet
Author: User

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.

 
 
  1. @interface CustomViewController : UIViewController {  
  2. }  
  3. @end 

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

 
 
  1. @class CustomViewController;  
  2.  
  3. @interface CustomViewControllerAppDelegate : NSObject  {  
  4.     UIWindow *window;  
  5.     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:

 
 
  1. - (void)applicationDidFinishLaunching:(UIApplication *)application {  
  2.     viewController = [[CustomViewController alloc]init];  
  3.     [window addSubview:viewController.view];  
  4.     [window makeKeyAndVisible];  
  5. }  
  6.  
  7. - (void)dealloc {  
  8.     [window release];  
  9.     [controller release];  
  10.     [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.

 
 
  1. @interface CustomViewController : UIViewController {  
  2. }  
  3. @end  
  4.  
  5. @implementation CustomViewController  
  6. - (void)viewDidLoad {  
  7.     [super viewDidLoad];  
  8.     self.view.backgroundColor = [UIColor blueColor];  
  9. }  
  10. @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.

 
 
  1.  @implementation CustomViewController  
  2.  - (void)viewDidLoad {    
  3.    [super viewDidLoad];      
  4.    self.view.backgroundColor = [UIColor blueColor];     
  5.     UIButton* button = [UIButton buttonWithType:UIButtonTypeInfoLight];      
  6.     button.frame = CGRectMake(100,100,100,100);      
  7.     [self.view addSubview:button];  
  8. }  
  9. @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

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.