The next step is to implement edstarrating editing. In the Loadview method, we have configured the delegate property of Edstarrating, and we just need to implement the relevant delegate method.
Open Masterviewcontroller.swift Add the following method to the edstarratingprotocol extension:
func starsselectionchanged(control: edstarrating!, rating: Float ) { if let Selecteddoc = selectedbugdoc() { selectedDoc.data.rating = Double(self. bugrating.rating) } } |
Almost the same as before: Get the user-selected insect model and assign it with the modified value.
Run the program. It is important to note that this value is persisted after the user has set a new rating, even if you switch to other insects and then switch back.
Get local pictures
Open masterviewcontroller.xib, and drag a "Push button" control to the bottom of the image view.
The title of the Modify button is "Change Picture":
Like the plus and minus buttons, create a ibaction for the Change Picture button, named changepicture.
This action is called when the button is clicked.
OS X has a unique control called Ikpicturetaker, which allows the user to select a picture from the computer or capture a picture from the camera.
When the user selects the picture, the control invokes the specified delegate method.
Open Masterviewcontroller.swift to add the following import statement:
This image picker belongs to the Quartz framework.
In the changepicture method, add the code:
if let Selecteddoc = selectedbugdoc () { ikpicturetaker () . Beginpicturetakersheetforwindow withdelegate: , didendselector: Span style= "color: #BF1D1A;" > "PictureTakerDidEnd:returnCode:contextInfo:" , contextinfo< Span style= "color: #002200;" >: nil ) } |
Let's first check if the user has selected a valid insect, and if so, display the picture taker control.
Then implement the Picturetakerdidend (_:returncode:contextinfo:) method:
func picturetakerdidend(picker: ikpicturetaker, ReturnCode: nsinteger , ContextInfo: unsafepointer<void>) { let image = picker.outputimage() if image! = nil && returncode = = Nsokbutton { self. Bugimageview.image = image if let Selecteddoc = selectedbugdoc() { Selecteddoc.fullimage = image Selecteddoc.thumbimage = image.imagebyscalingandcroppingforsize(cgsize(width : () reloadselectedbugrow () } } } |
First check whether the user clicked OK (Nsokbutton) and whether the selected picture is valid.
If yes, get the insect model selected by the user, modify the insect's picture and thumbnail, and update the cell.
Run the program, select an insect, Click on Change picture, get an image from a local file or camera, and the image will be updated in the selected cell immediately.
Some details on the problem
When you run the program, the view changes the window size and you will find that the control does not automatically fit the size.
This is the effect after the window is dragged large.
pplns:o= "Urn:schemas-microsoft-com:office:office" xmlns:w= "Urn:schemas-microsoft-com:office:word" xmlns:m= "http ://SCHEMAS.MICROSOFT.COM/OFFICE/2004/12/OMML "xmlns=" HTTP://WWW.W3.ORG/TR/REC-HTML40 ">
This is the effect of shrinking the window.
In addition, we have not persisted the app data. Once the app restarts, the user's additions and modifications to the data will be lost.
Open masterviewcontroller.xibto reduce the size of the view to a minimum enough to show all controls.
In, 3 buttons are placed in the same row. In the right-hand detail display area, all controls are left-aligned and have the same width (except for the Changepicture button).
Then, we add a split line in the middle. Drag a verticalline to the center of the view.
Resiliency operations
The resiliency action is used to restore the data to its original state. Drag a push button below the table view to modify its title to reset. Then open Assistant Editor and create a ibaction for the button named ResetData (confirm that the currently open source file is masterviewcontroller.swift ).
The ResetData () method adds the following code:
Setupsamplebugs() Updatedetailinfo(nil) Bugstableview.reloaddata() |
setupSampleBugs()
The method call restores all model data. Calling the method with nil as updateDetailInfo
the parameter value clears all the detail fields. Then refresh the table View.
Run programs, add, delete, or modify arbitrary data. Then click the Reset button and all the data is back to the original.
Developing Mac Apps with Swift (6)