Basic interaction with iPhone development applications

Source: Internet
Author: User

IPhone DevelopmentBasic Application ProcessingInteractionIs the content to be introduced in this article, MVC: Model View controller Model, View, controller) objective: to implement three types of code as different as possible, any written object should be clearly divided into which class, and most of its functions do not belong to or completely do not belong to the other two classes.

MVC can help ensure maximum reusability.

Model: The class that stores application data. Design some OBjective-C classes to save application data.

View: displays and interacts with elements of windows, controls, and other users.

Controller: bind the model and view together to determine how to process the application logic of user input. Consists of classes created by developers and application-specific classes.

Four files in the Classes Folder: two pairs of. m and. H files)

IP_03buttonfunViewController will be responsible for managing this view.

 
 
  1. #import <UIKit/UIKit.h> 
  2.  
  3. @interface IP_03buttonfunViewController : UIViewController {  
  4. }  
  5. @end 

This is a subclass of UIViewController. UIViewController situation a general controller, which is part of UIKit)

IP_03buttonfunAppDelegate implements application delegation. A class that processes specific tasks for another object. Through application delegation, we can handle tasks for the UIApplication at a predefined time.

Output port OutLet ):

The Outlet is a special instance variable. The controller class can use it to reference objects in nib. The output port can be seen as a pointer to an object in nib. For example, if you want to create a text tag in IB and modify it in code, you need to declare an input port and direct it to the tag, in this way, you can modify the label by modifying the input port.

The output port is an instance variable declared using the keyword IBOutlet.

The IBOutlet keyword is defined as follows:

 
 
  1. #ifndef IBOutlet  
  2. #define IBoutlet  
  3. #endif 

For the compiler, IBoutlet does not perform any operations, but only tells IB that the instance variable will be connected to the object in nib.

Action ):

An operation is a method in the Controller class and is declared through IBAction. This keyword tells IB that this method is an operation and can be triggered by a control.

The operation declaration is as follows:

 
 
  1. -IBAction)doSomething:id)sender; 

No return value. The Operation Method accepts a parameter of id type. The name is set to sender, and the control that triggers the operation will reference itself using the sender parameter.

@ Property declaration:

There has been no answer to this problem in previous operations. When I read this chapter today, I will explain it in detail and study it well.

@ Property declares some attributes. This is also a new feature of Objective-C2.0. Just like set and get in. NET. Adding an attribute to Objective-C usually includes setting and retrieving attributes. In this case, the method is used as the access method and modification method. For example, the following format:

 
 
  1. -(Id) foo // access method
  2. {
  3. Return foo;
  4. }
  5.  
  6. -(Id) setFoo :( id) aFoo
  7. {
  8. If (aFoo! = Foo)
  9. {
  10. [AFoo retain];
  11. [Foo release];
  12. Foo = aFoo;
  13. }
  14. }

@ Property is used to say goodbye to this boring access method and modification method. We can use the combination of @ property and @ synthesize to notify the compiler to create and set methods during compilation. The following example is used:

 
 
  1. @property retain,nonatomic) UILabel *statusText; 

The parameter retain informs the compiler to send a reserved message to the object assigned to this attribute, so that it is not clear from the memory. This is essential. Because the default behavior assign needs to be used with garbage collection. Garbage collection is not currently in use on the IPhone.

The second optional attribute nonatomic will change the access method and the generation method of the modification method.

The Objective-C attribute also has another feature, that is, dot notation.

 
 
  1. MyVar = [someObject foo]; or myVar = someObject. foo;
  2. SomeObject. foo = myVar; equivalent to [someObject setFoo: myvar]

Obtain the Title of the Button:

 
 
  1. NSString *title = [sender titleForState:UIControlStateNormal]; 

When requesting the button title, we need to provide the control status. Four possible states are:

A: normal) indicates that the control is active but is not currently in use.

B: highlight highlighted) indicates that the control is being held down or used.

C: Disable disabled) indicates that the button is not enabled and cannot be used.

D: Select selected. Only the specified control has this status, indicating that the control has been selected.

 
 
  1. NSString *newText = [NSString stringWithFormat:@"%@ button pressed.",title]; 

Equivalent

 
 
  1. NSString *newText = [[NSString allac] initWithFormat:@"%@ button pressed.", title]; 

The functions are the same, but the first method is not recommended. The first method is a simple or factory method that returns an automatically released object. After the object is used, it will be retained for a period of time, which wastes memory and is not recommended.

[StatusText release]; since we have not instantiated this object, why is there a release?

Because we implement the attributes of each output port and specify the retain parameter for this attribute, we must release it.

Example:

Open Xcode. File-> New Project-> Iphone Application-> View-Based Application-> IP_03buttonfun

Modify IP_03buttonfunViewController.h:

 
 
  1. #import <UIKit/UIKit.h> 
  2.  
  3. @interface IP_03buttonfunViewController : UIViewController {  
  4.  IBOutlet UILabel *statusText;  
  5. }  
  6. @property (retain,nonatomic) UILabel *statusText;  
  7. -(IBAction)buttonPressed:(id)sender;  
  8. @end 

Modify IP_03buttonfunViewController.m:

 
 
  1. #import "IP_03buttonfunViewController.h"  
  2.  
  3. @implementation IP_03buttonfunViewController  
  4. @synthesize statusText;  
  5.  
  6. -(IBAction)buttonPressed:(id)sender  
  7. {  
  8.  NSString *title=[sender titleForState:UIControlStateNormal];  
  9.  NSString *newText=[[NSString alloc]initWithFormat:@"%@ button pressed!",title];  
  10.  [statusText setText:newText];  
  11.  [newText release];  
  12. }  
  13.  
  14. - (void)didReceiveMemoryWarning {  
  15.     [super didReceiveMemoryWarning];  
  16. }  
  17.  
  18. - (void)dealloc {  
  19.  [statusText release];  
  20.     [super dealloc];  
  21. }  
  22. @end 

After the code is created, You can associate it with IB.

Drag two buttons to the View. Modify Button to Lift and Right, Label to null, and adjust the appropriate size.

The control has been created.

Select the Button Lift, and then Apple + 2 to see a small circle behind the Touch Up inside. Drag the mouse to the File's Ouner. Similarly, the Button Right operation is the same.

Select the Label, Apple + 2 will see statusText, and drag it to File's Ouner like above.

After the program is completed, Build and go run the following results.

Summary:IPhone DevelopmentBasic Application ProcessingInteractionI hope this article will help you!

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.