Skip to content

Commit 0f0951b

Browse files
Converted Chinese Documentation (zh)
1 parent 6b7e17a commit 0f0951b

File tree

212 files changed

+37809
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

212 files changed

+37809
-4
lines changed

_data/sidebars/zh_lb2_sidebar.yml

Lines changed: 898 additions & 4 deletions
Large diffs are not rendered by default.

pages/zh/lb2/6094911.md

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
---
2+
title: "Creating a LoopBack iOS app: part two"
3+
lang: zh
4+
layout: page
5+
keywords: LoopBack
6+
tags:
7+
sidebar: zh_lb2_sidebar
8+
permalink: /doc/zh/lb2/6094911.html
9+
summary:
10+
---
11+
12+
* Download the [server side code](https://github.com/strongloop-community/sample-applications/tree/master/BooksServer)
13+
* Download the [iOS client application](https://github.com/strongloop-community/sample-applications/tree/master/BooksClient)
14+
15+
This is the second of a two-part tutorial on creating a simple iOS app that connects to a [LoopBack](http://docs.strongloop.com/display/DOC/LoopBack) server application to perform create, read, update, and delete (CRUD) operations.   
16+
17+
If you haven't already done so, read [part one](https://docs.strongloop.com/display/LB/Creating+a+LoopBack+iOS+app%3A+part+one) before continuing.
18+
19+
## Add navigation control
20+
21+
To add a navigation control to your app:
22+
23+
1. Select Books Collection View Controller.
24+
2. Click **Editor > Embed In > Navigation Controller**. This will add a Navigation Item under the Books Collection View Controller, as shown below.
25+
3. Select the new Navigation Item and name it "Books Collection" in the attribute inspector.
26+
{% include image.html file="6258715.png" alt="" %}
27+
28+
## Implement the Add Book interface
29+
30+
To add the interface elements that enable a user to add a book:
31+
32+
1. From the object library in lower right corner of the XCode window, select **Bar Button** Item.
33+
2. Drag and drop it to the top right corner of the app shown in the XCode storyboard.
34+
3. In the attribute inspector change **Identifier** to **Add**, as shown below.
35+
4. Right click the Add bar button and Control-drag the circle next to action to the Books Collection View Controller. Select 'modal' as action.
36+
{% include image.html file="6258726.png" alt="" %} 
37+
38+
### Add another View Controller
39+
40+
When the user clicks the "+" button you want the app to show a screen to add a book to the collection.  To do that you need to add another View Controller:
41+
42+
1. Drag a **View Controller** element from the Object Library into the storyboard.
43+
2. Name this View Controller "Add Book".
44+
{% include image.html file="6258721.png" alt="" %} 
45+
3. Now connect the "+" button from the Books Collection View Controller to this screen: Control-dragg the "+" button from the **Books Collection View Controller** to the** Add Books View Controller**. This creates a segue. 
46+
4. Select **modal** as segue type. 
47+
5. Implement the segue action by adding the following code to `ViewController.m`:
48+
49+
**ViewController.m**
50+
51+
```
52+
...
53+
@implementation ViewController 
54+
//Add these 3 lines
55+
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
56+
{
57+
}
58+
...
59+
- (void)viewDidLoad
60+
```
61+
62+
### Add navigation to View Controller
63+
64+
Now add navigation to the "Add Books" View Controller as follows:
65+
66+
1. Select the **Add Book View Controller**.  
67+
2. Choose **Editor > Embed In > Navigation Controller**.
68+
3. Name this Navigation Controller "AddBook".
69+
70+
### Add elements to Add Books sceen
71+
72+
Add a Done and Cancel button to the Add Books screen as follows:
73+
74+
1. In the **AddBook** scene, select **Navigation Item**.
75+
2. In the Object Library, drag and drop two **Bar Button Items** to the scene.
76+
3. Select each Bar Button Item and in the attribute inspector change their identifiers to "Done" and "Cancel".
77+
4. Control-drag the **Done** button to the green exit button below the View Controller. 
78+
5. Select **unwindToList** as **Segue Action**. 
79+
6. Repeat the above two steps for the **Cancel** button.
80+
{% include image.html file="6258725.png" alt="" %} 
81+
82+
### Add new class files
83+
84+
Add a new class for the Add Book ViewController as follows:
85+
86+
1. In the XCode menu, choose **File > New > File...**.  
87+
2. Select **Objective-C class** and click **Next**.
88+
3. In the **Choose options for your new file** screen, change **Class name** to "AddNewBookViewController."
89+
4. Click **Create** to add two new files to your project: `AddNewBookViewController.h` and `AddNewBookViewController.m`.
90+
91+
### Connect class to View Controller
92+
93+
Connect the AddNewBookViewController class to the View Controller for Add New Book.
94+
95+
{% include image.html file="6258718.png" alt="" %}
96+
97+
Add Text Fields from the Object Library to the Add View Screen. Add one Text Field for each of the five properties: title, author, description, totalPages and genre. Your screen should look like the screenshot below:
98+
99+
{% include image.html file="6258724.png" alt="" %}
100+
101+
To connect the Text fields to your view controller:
102+
103+
1. Select the **Title** text field in your storyboard.
104+
105+
2. Control-drag from the text field on your canvas to the code displayed in the editor on the right, stopping at the line just below the `@interface` line in `AddNewBookViewController`.m```.`
106+
107+
In the dialog that appears, for Name, type "titleField."
108+
109+
Leave the rest of the options as they are. Your screen should look like this:
110+
111+
{% include image.html file="6258723.png" alt="" %}
112+
113+
Do the same for the other text fields.
114+
115+
To connect the Done button to your view controller:
116+
117+
1. Select the Done button in your storyboard.
118+
119+
2. Control-drag from the Done button on your canvas to the code display in the editor on the right, stopping the drag at the line just below your `textField` properties in `AddNewBookViewController`.m``.
120+
121+
3. In the dialog that appears, for Name, type "doneButton."
122+
123+
Leave the rest of the options as they are. Your dialog should look like this:
124+
{% include image.html file="6258717.png" alt="" %}
125+
126+
Now add functionality to the class to save the book when the user adds one: 
127+
128+
**AddNewBookViewController.h**
129+
130+
```
131+
#import <UIKit/UIKit.h>
132+
#import "ViewController.h"
133+
...
134+
```
135+
136+
**AddNewBookViewController.m**
137+
138+
```js
139+
#
140+
import "AddNewBook.h"#
141+
import "ViewController.h"#
142+
define prototypeName @ "books"
143+
@interface AddNewBook()
144+
@property(weak, nonatomic) IBOutlet UITextField * titleField;
145+
@property(weak, nonatomic) IBOutlet UITextField * authorField;
146+
@property(weak, nonatomic) IBOutlet UITextField * genreField;
147+
@property(weak, nonatomic) IBOutlet UITextField * descriptionField;
148+
@property(weak, nonatomic) IBOutlet UITextField * totalPagesField;
149+
@property(weak, nonatomic) IBOutlet UIBarButtonItem * doneButton;
150+
@end
151+
@implementation AddNewBookViewController
152+
153+
-
154+
(void) prepareForSegue: (UIStoryboardSegue * ) segue sender: (id) sender {
155+
void( ^ saveNewErrorBlock)(NSError * ) = ^ (NSError * error) {
156+
NSLog(@ "Error on save %@", error.description);
157+
};
158+
void( ^ saveNewSuccessBlock)() = ^ () {
159+
UIAlertView * messageAlert = [
160+
[UIAlertView alloc] initWithTitle: @ "Successfull!"
161+
message: @ "You have add a new book"
162+
delegate: nil cancelButtonTitle: @ "OK"
163+
otherButtonTitles: nil
164+
];
165+
[messageAlert show];
166+
};
167+
if (sender != self.doneButton) return;
168+
if (self.titleField.text.length > 0) {
169+
NSLog(@ "%@", self.titleField.text);
170+
LBModelRepository * newBook = [
171+
[AppDelegate adapter] repositoryWithModelName: prototypeName
172+
];
173+
//create new LBModel of type
174+
LBModel * model = [newBook modelWithDictionary: @ {
175+
@ "title": self.titleField.text,
176+
@ "author": self.authorField.text,
177+
@ "genre": self.genreField.text,
178+
@ "totalPages": self.totalPagesField.text,
179+
@ "description": self.descriptionField.text
180+
}];
181+
[model saveWithSuccess: saveNewSuccessBlock failure: saveNewErrorBlock];
182+
} else {
183+
UIAlertView * messageAlert = [
184+
[UIAlertView alloc] initWithTitle: @ "Missing Book Title!"
185+
message: @ "You have to enter a book title."
186+
delegate: nil cancelButtonTitle: @ "OK"
187+
otherButtonTitles: nil
188+
];
189+
[messageAlert show];
190+
}
191+
}
192+
....(ctd)
193+
```
194+
195+
Run the build and try it out. You should be able to add a new book and refresh to fetch all books.

0 commit comments

Comments
 (0)