Detailed tutorial on how to obtain objects under the iPhone (1)

Source: Internet
Author: User

DetailsIPhoneHowGet objectThe tutorial is the content to be introduced in this article, mainly aboutIPhoneHowGet objectHandle and its parent object handle. Let's take a look at it in detail.IphoneFirst, let's look at the details.

GeneralIPhoneThe program object structure is as follows:

 
 
  1. Number of objects object type
  2.  
  3. 1 UIApplication
  4. 1 UIApplicationDelegate/subclass
  5. 1, N UIViewController/subclass
  6. 1, N UIView/subclsss

Although some books say that you can bypass UIViewController to operate UIView directly, I personally think this layer is used to manage views and view relationships.

The following describes the object types of the above hierarchies. Note: The following learning is only related to the problem and will not fully learn various methods and attributes in the class.

UIApplication inherits from UIResponder: NSObject

Framework: UIKit. framework

Header file: UIApplication. h

Each application has a UIApplication or its subtype instance. When the program is loaded, the function method UIApplicationMain is called for execution. It creates a single-piece mode UIApplication object. Then you can access it by executing the sharedApplication class method.

Look at the main Function

 
 
  1. int main(int argc, char *argv[]){      
  2. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  3.     int retVal = UIApplicationMain(argc, argv, nil, nil);  
  4.     [pool release];  
  5.   return retVal;  

It can be seen that the main function is used to call the UIApplicationMain method to create a UIApplication object instance and manage the memory release of such instances.

The code for obtaining the UIApplication object instance is as follows:

Assume that the inheritance class of the UIApplicationDelegate protocol is XXXXAppDelegate.

 
 
  1. UIApplication *app = [UIApplication sharedApplication];  
  2. XXXXAppDelegate *d = (XXXXAppDelegate *)app.delegate;  
  3. //Test these code ,it is ok; 

Create a UIApplication instance in the main function and bind XXXXAppDelegate

Look at the prototype

 
 
  1. int UIApplicationMain ( int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName );  
  2.  
  3. This function is called in the main entry point to create the application object and the application delegate and set up the event cycle.  
  4.  
  5. argc: The count of arguments in argv; this usually is the corresponding parameter to main.  
  6.  
  7. argv: A variable list of arguments; this usually is the corresponding parameter to main.  
  8.  
  9. principalClassName: The name of the UIApplicationclass or subclass.  
  10.  
  11. delegateClassName: The name of the class from which the application delegate is instantiated. 

UIApplication:

 
 
  1. UIApplicationMain-> 
  2.     UIApplication 

UIApplicationDelegate implementation class

 
 
  1. UIApplication-> 

The sharedApplication class method obtains the UIApplication instance of the current program.

Gets an instance of the UIApplicationDelegate implementation class using the delegate attribute

Windows properties to obtain the array of window classes involved in the current program

Obtain the key window of the current program using the keyWindow attribute

Now that we have the implementation class of the UIApplicationDelegate protocol, how can we initialize the UIViewController/subclass?

There are two ways to initialize the Controller class:

 
 
  1. application:didFinishLaunchingWithOptions:  
  2. applicationDidFinishLaunching: 

These two methods are available in earlier versions. After iOS3.0 and later, use the former to start the process. XCode4 runs application: didfinishlaunchingwitexceptions:
Of course, you can also delete application: didfinishlaunchingwitexceptions: and add the applicationDidFinishLaunching method. This operation is not recommended.

Let's see how to compile the implementation class of UIApplicationDelegate.

 
 
  1. # Import <UIKit/UIKit. h>
  2. @ Class NavSmallPhoneViewController;
  3. @ Interface NavSmallPhoneAppDelegate: NSObject <UIApplicationDelegate> {
  4. }
  5. @ Property (nonatomic, retain) IBOutlet UIWindow * window;
  6. @ Property (nonatomic, retain) IBOutlet NavSmallPhoneViewController * viewController;
  7. @ End
  8.  
  9. # Import "NavSmallPhoneAppDelegate. h"
  10. # Import "NavSmallPhoneViewController. h"
  11. @ Implementation NavSmallPhoneAppDelegate
  12. @ Synthesize window = _ window;
  13. @ Synthesize viewController = _ viewController;
  14. @ Synthesize info;
  15. -(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {
  16. // Override point for customization after application launch.
  17. Selfself. window. rootViewController = self. viewController;
  18. [Self. window makeKeyAndVisible];
  19. // The Todo code is not tested and indicates a problem: Where is the source of the window attribute in the current UIApplicationDelegate implementation class?
  20. UIWindow * w = [UIApplication sharedApplication]. keyWindow;
  21. NSLog (@ "% @ \ n % @", w, self. window );
  22. If (w = self. window)
  23. NSLog (@ "AppDelegate 'window is UIApplication current keyWindow! ");
  24. Return YES ;}
  25. /* For ease of reading, only the methods to be viewed are retained here. Other automatically generated code is removed here. You can use XCode4 to automatically generate the code */
  26. -(Void) dealloc {
  27. [_ Window release];
  28. [_ ViewController release];
  29. [Super dealloc];
  30. }
  31. @ End

Looking at the header file, you find that the implementation class has two attributes: window and viewController; that is, the window and view controller associated with the current implementation class are saved.

Run the command to output "AppDelegate 'window is UIApplication current keyWindow !", The window attribute of the implementation class is the current keyWindow attribute of the UIApplication instance.

That is to say, if the UIApplication instance has only one UIWindow instance, the window attribute of its UIApplicationDelegate implementation class must point to the same UIWindow instance.

The implementation class of UIApplicationDelegate defines these two attributes to facilitate the use of UIWindow and UIViewController. The function is to establish a tree-like relationship between objects for mutual calls and implementation.

Here, we should know that UIApplication and UIViewController are associated through UIWinodw, although a viewController attribute is defined in the UIApplicationDelegate implementation class. Modify the diagram above:

The general iPhone program object structure is as follows:

 
 
  1. Number of objects object type
  2.  
  3. 1 UIApplication
  4. 1 UIApplicationDelegate/subclass
  5. 1 UIWindow
  6. 1. rootViewController attributes
  7. 1, N UIViewController/subclass
  8. 1, N UIView/subclsss

According to the current object structure, we can know that the UIWindow instance serves only as a link.

UIWindow inherits UIView: UIResponder: NSObject

Now you can see UIViewController


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.