[Cocoa] core data (4) of cocoa-use binding

Source: Internet
Author: User

Deep Dive into cocoa core data (4)-use binding

Luo chaohui (http://www.cnblogs.com/kesalin)

This article follows the "signature-non-commercial use-consistency" creation public agreement

We have explained the core data framework and compiled it manually.CodeDemonstrate the operation process of core data. Next we will demonstrate how to use core data with xcode's powerful visual editing and cocoa key-value encoding and binding mechanism. With the powerful tools mentioned above, in this example, we do not need to write nsmanagedobjectmodel code or nsmanagedobjectcontext, and the Engineering Template has done these things for us.

The example to be completed today has two entity: studententity and classentity, each with an attribute named name. Studententity is associated with classentity through a relationship named inclass, and classentity also has a relationship named students associated with studententity, which is a one-to-many relationship. In addition, classentity has a relationship named monitor associated with studententity, indicating the shift leader.

Download Code: Click here to download

========================================================== ======================================

First half

 

7. Create an nsarraycontroller and associate the object

Return to XIB, select studentview. XIB, and set the class of the file's owner of studentview to studentviewcontroller. Use Control-drag to direct the view of file's owner to custom view.

Drag two nsarraycontroller: classpopup and students.
Set the object controller mode of classpopup to entity name, Object Name: classentity, and check prepare content.
Set the object controller mode of students to entity name, Object Name: studententity, and check prepare content.

 

In the above operations, classpopup arraycontroller manages the data of classentity, and students arraycontroller manages the data of studententity. Later we will bind the control with these arraycontroller. Next, we will bind the managedobjectcontext parameter of the two nsarraycontroller to the managedobjectcontext in the managedviewcontroller (File's owner), so that the nsmanagedobjectcontext of nsdocuments will be applied to the arraycontroller. The following demonstrates only classpopup. Complete students binding on your own:

 

Previously, we created an iboutlet contentarraycontroller in managedviewcontroller. Now it is time to associate it. Use Control-drag to associate the contentarraycontroller of file's owner to students.

Repeat the preceding process, select classview. XIB, set the class of file's owner to classviewcontroller, and direct its view to custom view.

Drag three nsarraycontroller: classes, monitorpopup, and students to it.
Set the object controller mode of classes to entity name, Object Name To classentity, and select prepare content.
Bind the managedobjectcontext parameter of classes to the managedobjectcontext in managedviewcontroller (File's owner.

Note: monitorpopup and students are not modified here.
Use Control-drag to associate the contentarraycontroller of the file's owner to classes.

Bind the content set of students and monitorpopup to the model Key Path: Students of classes, indicating that the two arraycontrollers manage the students corresponding to classentity.

 

Now, the model and arraycontroller are ready. Now we bind the control to these objects. The above is tedious enough. We should be more careful and error-prone.

Select studentview. XIB and expand tableview in custom view until we can see the name and Class Two table columns.
Select the name column and bind its value to students. The model Key Path is: name, indicating that the first column displays the name of the student;

Select the class column. Note that this column is a popup button cell,
Bind its content to classpopup;
Bind its contentvalues to classpopup, and the model Key Path is: name, indicating that the option in the second column is the class name;
Bind the selected object to students. The model Key Path is "inclass", indicating that the student is added as a member of the selected class;

Select + button, use Control + drag to drag it to students, and select Add: Action Association;
Select-button and drag it to students using control + drag. select Remove: Action Association;
Select-button and bind its eanbled to students. The ctroller key is canremove;

The above operation is to add and delete students directly bound to students arraycontroller, no need to write a little code!

 

Select classview. XIB

Expand the class table in the Custom view until we can see the class table column: select the class column and bind its value to classes. The model Key Path is: Name, indicates that the class name is displayed in this column;
Select box, bind its title to classed, set model Key Path to: name, and set no selection placeholder below to: no selection, and null placeholder to: Unnamed class. It indicates that the information displayed in box is the information of the selected class. If no class is selected, no selection is displayed.

 

Expand box

Select pop up button
Bind its content to monitorpopup;
Bind its contentvalues to monitorpopup, and the model Key Path is: name, indicating that the option is the name of the student in the class;
Bind the selected object to classes, and the model Key Path is: monitor. This indicates that the selected student is treated as the class leader;

Expand the student tabel view until we can see the student table column.
Select the student column and bind its value to students. The model Key Path is: name, indicating that the student List displays the names of all students in the class.

Select + button, use Control + drag to drag it to classes, and select Add: Action Association;
Select-button, use Control + drag to drag it to classes, and select Remove: Action Association;
Select-button and bind its eanbled to classes. The ctroller key is canremove;

The above operation is to add and delete the class directly with classes arraycontroller.

 

Now, binding is successful. If yourProgramThe operation is incorrect. Most of the reasons are that the association and binding are incorrect. Go back to this section and check each item carefully.


8. display. Switch to view.

Now, when setting the main interface, modify the code in mydocument. h as follows:

 
# Import <Cocoa/cocoa. h>

@ Class managedviewcontroller;

@ Interface mydocument: nspersistentdocument {
@ Private
Nsbox * box;
Nspopupbutton * popup;

Nsmutablearray * viewcontrollers;
Nsinteger currentindex;
}

@ Property (nonatomic, retain) iboutlet nsbox * box;
@ Property (nonatomic, retain) iboutlet nspopupbutton * popup;

-(Ibaction) changeviewcontroller :( ID) sender;
-(Void) Displayviewcontroller :( managedviewcontroller *) MVC;

@ End

 

Modify the code in mydocument. m as follows:
# Import "  Mydocument. h  " 
# Import " Classviewcontroller. h "
# Import " Studentviewcontroller. h "

@ Implementation mydocument

@ Synthesize popup;
@ Synthesize box;

-(ID) Init
{
Self = [Super init];
If (Self ){
// Create view Controllers
//
Viewcontrollers = [[nsmutablearray alloc] init];

Managedviewcontroller * MVC;
MVC = [[classviewcontroller alloc] init];
[MVC setmanagedobjectcontext: [self managedobjectcontext];
[Viewcontrollers addobject: MVC];
[MVC release];

MVC = [[studentviewcontroller alloc] init];
[MVC setmanagedobjectcontext: [self managedobjectcontext];
[Viewcontrollers addobject: MVC];
[MVC release];
}
Return Self;
}

-( Void ) Dealloc
{
Self. Box = nil;
Self. popup = nil;
[Viewcontrollers release];

[Super dealloc];
}

-(Nsstring *) windownibname
{
// Override returning the NIB file name of the document
// If you need to use a subclass of nswindowcontroller or if your document supports multiple nswindowcontrollers, you shoshould remove this method and override-makewindowcontrollers instead.
Return @" Mydocument " ;
}

-(Void ) Windowcontrollerdidloadnib :( nswindowcontroller *) acontroller
{
[Super windowcontrollerdidloadnib: acontroller];

// Init popup
//
Nsmenu * menu = [popup menu];
Nsinteger itemcount = [viewcontrollers count];

For (Nsinteger I = 0 ; I <itemcount; I ++ ){
Nsviewcontroller * Vc = [viewcontrollers objectatindex: I];
Nsmenuitem * Item = [[nsmenuitem alloc] initwithtitle: [VC title]
Action: @ selector (changeviewcontroller :)
Keyequivalent: @"" ];
[Item settag: I];
[Menu additem: item];
[Item release];
}

// Display the first Controller
//
Currentindex = 0 ;
[Self displayviewcontroller: [viewcontrollers objectatindex: currentindex];
[Popup selectitematindex: currentindex];
}

# Pragma Mark-
# Pragma Mark change views

-(Ibaction) changeviewcontroller :( ID) sender
{
Nsinteger tag = [Sender tag];
If (TAG = currentindex ){
Return ;
}

Currentindex = tag;
Managedviewcontroller * MVC = [viewcontrollers objectatindex: currentindex];
[Self displayviewcontroller: MVC];
}

-( Void ) Displayviewcontroller :( managedviewcontroller *) MVC
{
Nswindow * window = [box window];
Bool ended = [Window makefirstresponder: Window];
If (! Ended ){
Nsbeep ();
Return ;
}

Nsview * mvcview = [MVC view];

// Adjust window's size and position
//
Nssize currentsize = [[Box contentview] frame]. size;
Nssize newsize = [mvcview frame]. size;
Float Deltawidth = newsize. Width-currentsize. width;
Float Deltaheight = newsize. Height-currentsize. height;

Nsrect windowframe = [Window Frame];
Windowframe. Size. Width + = deltawidth;
Windowframe. Size. height + = deltaheight;
Windowframe. Origin. Y-= deltaheight;

[Box setcontentview: Nil];
[Window setframe: windowframe display: Yes Animate: Yes];

[Box setcontentview: mvcview];

// Add viewcontroller to the responder-chain
//
[Mvcview setnextresponder: MVC];
[MVC setnextresponder: Box];
}

@ End


In mydocument, we have created two managedviewcontrollers and passed in the managedobjectcontext. The two viewcontrollers represent the class and student interfaces respectively, and then switch between them through the popup button. In displayviewcontroller, we also adjust the size of the main interface based on the size of the current interface. In this case, we need to set the automatic box size on the main interface. Open mydocument. XIB and make the following settings:


Then, use Control + drag to associate the popup and popup buttons of the file's owner, and the box and box, set the action of the popup button to the-(ibaction) changeviewcontroller (ID) sender of the file's owner.

So far, all the work has been completed. Compile and run the program. If there is no accident, we should be able to add students, classes, and set the students, class leaders, and other information.

 

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.