1 Create and run the first App1.1 question
Use Xcode to create an app project that displays the Hello World tag on the interface, running results in the emulator-1:
Figure-1
1.2 Solutions
Analysis Figure 1, first use Xcode to create a single Viewapplicaton app, named Myfirstapp,-2:
Figure-2
Then delete the Xcode navigation bar to keep only TRAppDelegate.h files and trappdelageat.m files, other code files, storyboard files, and xib files deleted,-3 as shown:
Figure-3
Then select the TRAppDelegate.h file, the Code Editor on the right is visible trappdelegate inherits to the Uiresponder class, and follows the Uiapplicationdelegate protocol, and there is a UIWindow Type Property window, which is the main window of the program View, the code looks like this:
- #Import<uikit/uikith>
- @interfaceTRAppDelegate : Uiresponder<uiapplicationdelegate>
- @Property (Strong, nonatomic) uiwindow *window;
- @end
Select the TRAPPDELEGATE.M file, the Code Editor on the right shows a method of the code shown below, which is a method in the Uiapplicationdelegate protocol, which is the method that is called when the app is launched successfully, this method is the entry of the program, so it is necessary to write the content Code of the page on this side Extrajudicial
- - (BOOL) application:(uiapplication *)application didfinishlaunchingwithoptions:( Nsdictionary *) launchoptions {
- }
1.3 Steps
The implementation of this case needs to follow the steps below.
Step one: Create a Window object, that is, apply the main Window object
First create a Window object that is the same size as the screen, and let the Window property of the Trappdelegate class point to the Window object and set the window's background color to white, as shown in the following code:
- Creating a Window Object
- Frame is a property that represents the window origin coordinates and the width height, [[[Uiscreenmainscreen] bounds] is used to get the size of the screen and assigns the obtained value to the Frame property of the window
- Self. Window=[[uiwindowalloc]initwithframe: [[[Uiscreenmainscreen] bounds]];
- Set the background color of a Window object
- BackgroundColor is the property that window uses to represent the background color, and the value of the property is set to White
- Self. Window. backgroundcolor = [Uicolorwhitecolor];
Step Two: Create a label Uilabel object
The Uilabel class is a label control that is often used to display various text messages, create an object of the Uilabel class, set the displayed text information and the label's position size by the value of the Uilabel Text property and the Frame property, and add the Label object to the application's main window. The code looks like this:
- Create a Label object
- UILabel *label = [[Uilabelalloc]init];
- Sets the text information for the label object, which is used to record the text you want to display
- Label. Text = @"Hello world." ;
- Sets the label's position and size, and the label's frame property is used to record the origin coordinate position and width height relative to the parent view
- CGRect frame = {, +, + , + };
- Label. Frame = frame;
- Add a label to the main window interface
- [Self. Windowaddsubview: Label];
Step three: Set the Window object to the main window and display
The Window object is set to the main window and displayed, so that you can interact with the user only if you become the primary window. The code looks like this:
- Let the window object become the main window and display it
- [Self. windowmakekeyandvisible];
1.4 Full Code
The complete code for this case is as follows:
Code
2 Create an app with views and controllers and add a label to make a sub-view 2.1 issue
In the actual development is usually not used in the above way to write code, because the above method can only be implemented very simple, only one interface of the application, but the actual development of the application is to require a multi-interface to switch, the above code is not possible, so we need to use the view and view controller, Usually the window shows an entire view, does not put a single control (such as Uilabel) in the window to display, the first view into the application show is called the root view, the view controller is used to control the view display of the content, usually the view controller and view are used in conjunction.
2.2 Solutions
First use Xcode to create a single Viewapplicaton app, and delete other files, just the TRAppDelegate.h, and TRAPPDELEGATE.M files, as in the first case.
Next, right-click on the window shown in 4, select New file to create a new Trmyfirstviewcontroller class, which inherits from Uiviewcontroller,-5 as shown:
Figure-4
Figure-5
Select the trmyfirstviewcontroller.m file, the code editing area on the right is shown in the following method, where Viewdidload is called once when the view page has been successfully loaded into memory, typically the initial display of the interface is designed in this method, so the content code shown in the previous case needs to be written in In this method, but be aware that the view needs to be loaded by the parent class before loading the current view presentation:
- - (void) viewdidload {
- [Superviewdidload];
- }
Finally select the TRAPPDELEGATE.M file, import the header file of the Trmyfirstviewcontroller class, and in the Didfinishlaunchingwithoptions method, create an object of the Trmyfirstviewcontroller class as The root view controller for the main window of the application, as shown in the following code:
- Trmyfirstviewcontroller *myfirstviewcontroller = [[Trmyfirstviewcontrolleralloc]init];
- Self. Window. Rootviewcontroller = Myfirstviewcontroller;
2.3 Steps
The implementation of this case needs to follow the steps below.
Step One: Create a label Uilabel object
Create a Uilabel object in the Viewdidload method, set its display text information, position size, and background color as in the previous case, and add the Uilabel object to the current view, as shown in the following code:
- - (void) viewdidload
- {
- [Superviewdidload];
- 1. Create a Uilabel object
- UILabel *label = [[Uilabelalloc]init];
- 2. Setting related properties
- Label. Text = @"Hello world." ;
- Label. Frame = cgrectmake(. );
- Label. BackgroundColor = [Uicolorgreencolor];
- 3. Add an object to the current view
- [Self. Viewaddsubview: Label];
- }
Step Two: Create the main window's root View Controller object
In the Didfinishlaunchingwithoptions program entry method of the TRAPPDELEGATE.M file, create an object of the Trmyfirstviewcontroller class as the root view controller for the main window of the application, as shown in the following code:
- - (BOOL) application:(uiapplication *)application didfinishlaunchingwithoptions:( Nsdictionary *) launchoptions
- {
- Self. Window = [[uiwindowalloc] initwithframe: [[[Uiscreenmainscreen] bounds]];
- Self. Window. backgroundcolor = [Uicolorwhitecolor];
- Create a root View Controller object
- Trmyfirstviewcontroller *myfirstviewcontroller = [[Trmyfirstviewcontrolleralloc]init];
- Self. Window. Rootviewcontroller = Myfirstviewcontroller;
- [Self. windowmakekeyandvisible];
- return YES;
- }
2.4 Full Code
In this case, the complete code in the TRMYFIRSTVIEWCONTROLLER.M file looks like this:
Code
The complete code in the TRAPPDELEGATE.M file is as follows:
Code
3 Adding a button sub-view in the App View 3.1 issue
Create a button (UIButton Class), add a button to the interface, add a click event to the button to modify the display of a label, 6 and Figure 7:
Figure-6
Figure-7
3.2 Solutions
First, like the second case, use Xcode to create a single View application, preserve the Trappdelegate class, and create the Trviewcontroller view Controller class to complete the code in the TRAPPDELEGATE.M file.
Then, add buttons and tags to the interface, and the code is written in the Viewdidload method in the Trviewcontroller.m file.
Finally, add a click event to the button to modify the display in the label.
3.3 Steps
The implementation of this case needs to follow the steps below.
Step One: Create the button buttons object and set the related properties
Use the factory method to create a button object for the UIButton class, and to set the individual property settings for the button to add to the parent view. The code looks like this:
- 1. Create a UIButton object
- UIButton *button = [uibuttonbuttonwithtype: Uibuttontypesystem];
- 2. Set the individual properties of a button
- [Buttonsettitle: @"OK" forstate: UIControlStateNormal];
- Button. Frame = CGRectMake (+,- 280,+ );
- Button. backgroundcolor = [Uicolorlightgraycolor];
- 3. Join to Parent View
- [Self. Viewaddsubview: Button];
Step Two: Create a label label object and set related properties
Create a Label object for the Uilabel class label, and label the property settings, add to the parent view, where the label object needs to be used throughout the Trviewcontroller class, so you need to set the properties of the Trviewcontroller class. The code looks like this:
- @Interfacetrviewcontroller ()
- @Property (Strong, nonatomic) UILabel *label;
- @end
The code that creates the label object and property settings is written in the Viewdidload method, as shown in the following code:
- UILabel *label = [[Uilabelalloc]init];
- Self. Label = label;
- Label. Text = @"content to modify";
- Label. Frame = CGRectMake(280, + );
- Label. Font = [uifont systemfontofsize:+];
- [Self. Viewaddsubview: Label];
Step three: Add a click event to the button
Use the AddTarget:action:forControlEvents: method to add a click event to the button.
Target: Represents the target object, that is, when the button fires an event, which object is sent a message, often using self, to send a message to the current object.
Action: Represents the action, which is the message sent to the target object, here is the method Tapbutton.
Events: Represents an event that, when an event of a button object is triggered, sends an action message to target, using the UIControlEventTouchUpInside event, which is the Click event inside the button.
The code looks like this:
- [Buttonaddtarget: Self
- Action: @selector(Tapbutton)
- forControlEvents: UIControlEventTouchUpInside];
Step four: Implementation method Tapbutton
When clicking the button to start the event, will need to complete the things in the Tapbutton method inside the implementation, where the need to complete is to modify the label label display inside the same, the code is as follows:
- - (void) Tapbutton
- {
- Self. Label. Text = @"This is the modified content";
- }
3.4 Full Code
In this case, the complete code in the TRVIEWCONTROLLER.M file looks like this:
Code
4 Create an app with Xib and access the object created in Xib in program code 4.1 issues
In the actual development in addition to the interface can be used in the operation of the code, you can also use the Visual programming tool Xib to build the interface to improve the efficiency of development, this case using the Xib component interface and in code to access Xib created by the various controls, click the button to modify the display content in the label. The interface effect runs in the emulator-8 shows:
Figure-8
4.2 Solutions
Xib was previously a standalone visual programming tool that was integrated into Xcode from XCODE4, facilitating the use of programmers and improving development efficiency.
How Xib works: The creation of controls (or views) that are needed on the interface, the setting of properties, the relationships between them, are saved to an XML file, and when the program is run, the configuration of all controls (or views) is read from the XML file, and the objects are automatically created and added to the parent view.
First create a view controller Trviewcontroller with a xib file, usually the Xib file and the name of the view controller associated with it, where the view controller class name is Trviewcontroller, The Xib file name is shown in trviewcontroller.xib,-9.
Figure-9
Next, select the desired control from the object library (Figure 10) in the right column, drag and drop it into the Xib interface, and in the inspector in the right column (Figure 11) You can set various properties of the control, drag and drop a button control and a label control to the Xib interface in this case.
Figure-10
Figure-11
Then, when you create the Trviewcontroller object in the TRAPPDELEGATE.M file, use the initialization method Initwithnibname: The method creates the Trviewcontroller object from the Xib file. Here the Nibname parameter is the name of the Xib file, as shown in the following code:
- Trviewcontroller *VC = [[Trviewcontrolleralloc]initwithnibname: @"Trviewcontroller" Bundle: Nil];
Finally, the controls and code that are dragged and dropped in xib are associated, and the label and button created by the code is accessed xib.
4.3 steps
The implementation of this case needs to follow the steps below.
Step one: Create a xib file
Start with the first case using Xcode to create a single View Applicaton app and delete other files, leaving only TRAppDelegate.h, and TRAPPDELEGATE.M files.
Create the Trviewcontroller class file, this time when creating the view controller, you need to tick the box before also create XIB file to create a XIB file associated with it when creating the Trviewcontroller object, as shown in 12.
Figure-12
Create the generated Xib file as shown in 13:
Figure-13
Step Two: Drag and drop the button into the Xib interface
Select the button control from the object library and drop it into the Xib interface to set the title property of the button control in the inspector in the right column, as shown in-14 and Figure 15:
Figure-14
Figure-15
Step three: Drag and drop the label into the Xib interface
Select the Label control from the object library and drop it into the Xib interface, and set the Text property of the label control in the inspector in the right column, as shown in-16 and Figure 17:
Figure-16
Figure-17
The last completed Xib interface-18 shows:
Figure-18
Step Four: Associate a label in Xib with the code
To access the objects created by Xib in your program code, you need to use a connection outlet (Iboutlet). A Iboutlet is a property that points to an object in Xib.
To associate the label object in Xib with the private property of the Trviewcontroller class, click the Secondary Editor button in the upper-right corner of the Xcode toolbar to open the Auxiliary editor for Xcode, The secondary editing interface displays the Trviewcontroller.m file, which is shown in 19:
Figure-19
With the label in Xib selected, the Control key, drag the label to the class extension in the trviewcontroller.m file, release the mouse, and the window shown in 20 will appear:
Figure-20
In the pop-up window, select Outlet in the connection bar, and the output is named mylabel,-21 as follows:
Figure-21
By clicking the Connect button, a line of code is automatically generated in the class extension, and a solid circle appears before this code, indicating that the label object in Xib has been associated successfully, and if the association succeeds, it displays a hollow circle, 22:
Figure-22
Step Five: Add a click event to the button in Xib
To add a click event to a button in xib, you need to use the action ibaction. Ibaction is a method that connects to an event on a control in Xib. This method is called when the user triggers this event for the control.
Open the auxiliary editor for Xcode as in the previous step, The secondary editing interface displays the TRVIEWCONTROLLER.M file, selects the button object in the Xib, and control-drags the button to the trviewcontroller.m file, releasing the Mouse, which pops up the window shown in 23:
Figure-23
In the pop-up window, name the action Tapaction,type Select to Uibutton,arguments as None, indicating a method without parameters, as shown in 24:
Figure-24
Click the Connect button to generate the method in the TRVIEWCONTROLLER.M. 25 file, and a solid circle appears before this method, indicating that the click event of the button object in Xib is added successfully, and if it is not associated successfully a hollow circle is displayed, 25 as shown:
Figure-25
Finally, this method is implemented, as shown in the following code:
- - (ibaction) Tapbutton
- {
- Self. MyLabel. Text = @"haha, I modified successfully";
- }
4.4 Full Code
In this case, the complete code in TRAPPDELEGATE.M is as follows:
Code
In this case, the complete code in TRVIEWCONTROLLER.M is as follows:
Code
Introduction to learning methods and stages, introduction of iOS interface development, construction of the first app, view controllers and views, controls and events, Interfacebuilder