Click Add to display the new TODO list in the list.It's so easy. Let's get started!
Create a projectCreate a new project and select the Tabbed Application template. The project name is MyTodoList. Remember to select Swift as the development language. Xcode creates a Swift project:
ADD management classThe first class we need is a TodoList manager, which is used to store the data in the TODO list and perform some basic operations for adding, deleting, modifying, and querying. We name itTodoManager.
Right-click the folder on the left, select New File, and select Cocoa Class. The Class name isTodoManager, Inherited from NSObject, Xcode will automatically add a TodoManager. swift file for us.
The variables and functions we define in Swift are global attributes, so that we can define a TodoManager object outside the class.todoManager, Simple implementation of the singleton mode:
import UIKitvar todoManager : TodoManager = TodoManager ()class TodoManager: NSObject {}
Next, define a struct to represent a TODO item. It has two attributes: one is the task name and the other is the task description:
struct todo { var name = Un-Named var desc = Un-Described}
Add a todos array to TodoManager to store all tasks:
class TodoManager: NSObject { var todos = [todo]()}
Finally, define a method.addTaskTo add a task:
class TodoManager: NSObject { var todos = [todo]() func addTask(name: String, desc: String) { todos.append(todo(name: name, desc: desc)) }}
OK.TodoManagerEven if it is complete.
DEVELOPMENT INTERFACEReturn to StoryBoard and delete the automatically generated content (several labels) on the page:
Then add a UITableView to FirstViewController:
Select the Tab Bar to edit the display name and image of the Tab Bar:
Next, let's take a look at Second View. Change the Title of the second Tab Bar to Add:
In this way, the basic page is handled.
Data Display First View ControllerUnder the first Tab, move the mouse over UITableView, right-click and drag it to View Controller, and select DataSource and Delegate:
Return to the Code, open the FirstViewController. swift file, and addUITableViewDelegateAndUITableViewDataSourceThe two protocols. Press and hold the Command key and click the protocol name to view the Protocol Declaration, so as to know the methods to be implemented. The method name is exactly the same as that in OC. You only need to convert it to the Swift syntax. After completionFirstViewControllerIt looks like this:
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{ override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // UITableView DataSource func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return todoManager.todos.count; } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: Default) cell.textLabel?.text = todoManager.todos[indexPath.row].name cell.detailTextLabel?.text = todoManager.todos[indexPath.row].desc return cell }}
Second View ControllerDrag some controls to build the basic framework. A Label is used as the title. Fill in the TODO Name and description for the two textfields respectively, and then add the Add button. The basic framework looks like this:
Then we direct the Delegate of the two textfields to the View Controller, because we hope that the keyboard will automatically play back after we input and click Return. InSecondViewController.swiftAddUITextFieldDelegateAnd implementtextFieldShouldReturnDelegate method, in the method, throughresignFirstResponderPlay the keyboard back:
// UITextField Delegatefunc textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder() return true}
We hope that the user can retrieve the keyboard when clicking the background image, and we can rewrite it.touchsBeganMethod, addendEditingMethod:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { self.view.endEditing(true)}
Define two attributes to get the values in the text box, switch to the Assistant view, and right-click and drag to create two variables:
Then we create an IBAction to process the Click Event of the Add button:
In the click event, we want to complete the following tasks:
- Add a TODO item in todoManager.
- Collapse the keyboard
- Clear content in TextField
- Switch the TabBar to the todo label to view the result in real time.
After OKaddBtnClickThe method is as follows:
@IBAction func addBtnClick(sender: AnyObject) { todoManager.addTask(todoText.text, desc: descText.text) self.view.endEditing(true) todoText.text = descText.text = self.tabBarController?.selectedIndex = 0}
In this way, the task of adding TODO is complete.
Delete dataThe interfaces for deleting data and Objective-C are the same.commitEditingStyleMethod implementation. Open the FirstViewController. swift file and add a TableView attribute in the code to refresh the data:
Delete is actually deleted.todoManagerOftodosThe corresponding data in the array. We can useremoveAtIndexImplementation, rememberreloadDataRefresh TableView:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if(editingStyle == UITableViewCellEditingStyle.Delete) { todoManager.todos.removeAtIndex(indexPath.row) } todoTableView.reloadData()}
TestThis is the end of the basic development work. We can run and run the application.
First add a TODO:
Click Add to view the added TODO items in TableView:
Slide to see the delete button:
Click Delete. The deletion is successful:
SummaryI don't know how you feel here. Anyway, I feel like water is exploding! There are no profound technical points or innovative things. It is just a well-regulated small application.
Yes, indeed. However, I hope that you can familiarize yourself with Swift and the new partner ^_^ through such a simple example.
Click here to download the complete project source code. Have fun.