Developing Mac Apps with Swift (8)

Source: Internet
Author: User

attention to detail

We still have some details to note about the user experience. For example: Run the app, do not select any insects, click "Delete" or "Change Picture" button, nothing will happen, why?

As a programmer, of course you know that when the user doesn't have a choice, nothing should be done, but for the user, the situation is still unfriendly:


We solve this problem in the following ways:

· If a user selects a cell, we make the delete button, change Picture button, text box, and rating view available.

· If the user does not select any rows, we disable the above controls and users will not be able to interact with them.

Open masterviewcontroller.xib, select the Delete button, and in the Properties panel, remove the check before the Enabled property.


Repeat the above steps on the Changepicture button, text field.

This way, the above controls will be disabled when the program is just started. Then we need to enable them after the user selects the cells in the table. To achieve this, we first need to create iboutlet for them.

Open Assistanteditor Ensure that the currently edited file is Masterviewcontroller.swift.

Select the Delete button and right-drag it to the masterviewcontroller.swift file.


In the pop-up exit, select Connection as "Outlet", enter DeleteButtonin the Name field, and click Connect.


Repeat the above steps to create a iboutlet for the changepicture button named Changepicturebutton.

Open Masterviewcontroller.swift, and in Tableviewselectiondidchange (_:), add the following code, which is updateDetailInfo(selectedDoc) after one line:

Enable/disable buttons based on the selection
 Let = (!=nil)
=
=
=
= buttonsenabled

We first determine whether the control needs to be enabled, which is determined by whether the user selects the cell. If Selecteddoc is empty, it means that no row is selected, indicating that the control should be disabled, or the control will be enabled.

In addition, Ratingview is enabled by default, so we also need to

loadView()disable it in. Find this line of statement:

 Self = true

Revision changed to

 Self = false

Run the program.

Note : You can also hide the entire detail page when the user does not select a valid line, but it is entirely up to you.

Save Data

Just like the Ios,mac app is also available NSUserDefaults , so we can store the data there.

First we have to get the model class to implement the NSCoding protocol. Define an extension in scarybugdata.swift :

MARK:-nscoding
Extension scarybugdata:nscoding{
func encodewithcoder(coder:nscoder){
Coder.encodeobject(self title, Forkey:"title")
Coder.encodeobject(Double(Self rating), Forkey:  "rating")
}
}

First we let scarybugdata implement the methods in the Nscoding protocol encodeWithCoder . This method is used to encode a custom class.

A corresponding initialization method is also required. The difference is that we cannot define the required Init method in the extension, so it must be defined in the class code:

Required convenience init (coder Decoder:nscoder) {
Self.title = Decoder.decodeobjectforkey ("title") as String

init(coder:)Method and encodeWithCoder method are reversed, used to read data from a file and deserialize it as an object.

Then define an extension implementation protocol in Scarybugdoc.swift NSCoding :

MARK:-nscoding
Extension Scarybugdoc:nscoding{
func encodewithcoder(coder:nscoder){
Coder.encodeobject(self data, forkey:"Data")
Coder.encodeobject(self thumbimage, forkey:"Thumbimage")
Coder.encodeobject(self fullimage, forkey:"Fullimage")
}
}

Then define the Init method in the class code (not in the extension definition):

Init (coder Decoder:nscoder) {
Self. Init ()
 Self = Decoder.decodeobjectforkey("data") as Scarybugdata
 Self = Decoder.decodeobjectforkey("Thumbimage")asnsimage ?
 Self = Decoder.decodeobjectforkey("Fullimage")asnsimage
}

Next, save the model data to NSUserDefaults . Add a method in masterviewcontroller.swift :

func savebugs(){
 Let = nskeyedarchiver. Archiveddatawithrootobject(self bugs)
nsuserdefaults. Standarduserdefaults(). SetObject(data, Forkey:"Bugs" )
nsuserdefaults. Standarduserdefaults(). Synchronize()
}
This method will first Bugs arrays are constructed as a NSData object, and then save to the

NSUserDefaults. NSKeyedArchiver . Of course, all objects in the array are implemented NSCoding .

Open appdelegate.swift, add in Applicationwillterminate ():

Masterviewcontroller.savebugs()

This saves all insect data to the app before exiting NSUserDefaults .

Loading Data

Found it in appdelegate.swift. applicationDidFinishLaunching

Masterviewcontroller.setupsamplebugs()

Replaced by

if  Let = nsuserdefaults. Standarduserdefaults(). Objectforkey("bugs")as? NSData {
= nskeyedunarchiver. Unarchiveobjectwithdata(data)as[ Scarybugdoc]
}else{
Masterviewcontroller.setupsamplebugs()
}

Run the program, add, delete, and edit insect data, and then exit the program. After restarting the app, all of the last changes were retained.

Note : If the application does not exit normally, the saveBugs() method does not call-please exit the program with COMMAND-Q instead of terminating the program from Xcode. To solve this problem, you can call the Savebug () method at some appropriate time in the Masterviewcontroller-as long as the user has made new, deleted, and edited operations.



Developing Mac Apps with Swift (8)

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.