Core Data can't save anything - core-data

I have started learning Core Data programming and I have a problem. The book that helped me in my studies is written for XCode3, and I'm using XCode4. I surfed on the Net and found this article . I've done everything just like it's written there and created my entity just like it's written in the book. To enable the save action I was told to CTRL+drag the Save menu item to the AppDelegate in my nib. I did so, but when I make changes in the program and then save it when I reopen it it seems that nothing has saved. How could I solve this?
Thanks in advance.

You need to connect save button with an action handler. It must have this structure:
.h file
#interface YourClass : UITableViewController
//... ivars, properties
-(IBAction)saveButtonPressed:(id)sender;
#end
.m file
-(void)saveButtonPressed:(id)sender {
[self.managedObjectContext save:nil];
}
Now the ctrl-dragging should work. Drag from the button to the controller. In the resulting black popup you should be able to select saveButtonPressed.
Edit
An even better method is to do this: open the split view controller. On the left you have your storyboard / IB with button, on the right the .h file of your view controller class. Now control drag from the button to a space above #end in the .h file. In the popup, select "Action" (not "Outlet"), name it e.g. saveButtonPressed. Press OK and Xcode will generate automatically all of the above. Now just fill in the save instruction in your new button handler.

Related

IBOutlet inside document based application

I've created an application document based but I had problem to handle IBOutlet becouse when I selected some controller inside the document in the first window sometime the other same controller in another window document was selected too...
Maybe I did wrong with strong/weak/readwrite or something I'm not very sure so I've decided to force my application to have just one window document for time.
Now my problem. I've an arrayController and when I launch my application and I do open saved file it close the "blank document" created by default and show the new window document with the data loaded.
The problem is that the arrayController of the second window called inside the windowControllerDidLoadNib is the right pointer (created by makeWindowControllers) but when I use this inside the code it has another pointer that is the old arrayController of the blank document that is been closed!! Why? Do close method release everything? (I use ARC).
Excuse me if my words are confused, I'm newbie. Thank you.
This is my code:
Document* newDoc = [[Document alloc] init];
[[NSDocumentController sharedDocumentController]addDocument:newDoc];
[newDoc makeWindowControllers];
[newDoc showWindows];
I've solved this issue: when you define notification for some object as a nsdocument these are not released after close calls. So you need to remove observer!

Adding UIWebView to second (not first) Storyboard view controller

Getting started with xCode 4.6.3 and I'm running into a hiccup.
I've built a simple app using the UIWebView to display a local HTML file and it's contents. I used that to debug any issues with the HTML and/or image displays and I'm all set.
Now I need to recreate that within a larger project I've built with storyboards for all my navigations between multiple view controllers. My issue comes when I'm trying to control-click drag from the WebView into the ViewController.h code below the #interface like many tutorials show, and that worked fine within my smaller single view controller app. It won't do it. I know I'm missing something obvious here. Am I going to have the set up these screens (I have multiple ones to do this same way) as separate xib files and add them into my main project?
Thanks for any help and clarification.
-Chris
You can create a class called for example myWebViewController and in Interface builder add a UIWebView to it. The control+drag to the header file will work.
Every time you want a UIViewController that has a browser in it, define its class as myWebViewController in Interface Builder.
Try not to repeat code. If you see UIViewControllers or any other UIView...that do the same thing, you can group them into a class that you use over and over.

iPad: Correct approach to custom navigation items in UINavigationController

This relates to a question I asked a few days ago: iOS: Setting text in nib subview from view in UITabBar/UINavigationController application
I need to put the search bar and buttons on the top right of a navigation controller, this is more than the standard single button that UINavigationController.navigationItem.rightBarButtonItem allows so I am using the initWithCustomView: method of UIBarButtonItem to load a view from a nib file.
In my particular case, i've put the view as a seperate item in the main view file for that form
The problem i've got is load and display sequence and I wanted to know if this was the right approach to this?
It seems that the following happens:
viewDidLoad on my main window gets called
viewDidAppear on my main window gets called and I set up rightBarButtonItem
I then want to populate a text field on that search bar but because the loading of the view for the button item happens in the main thread, I don't know when it's appeared.
Would I be better to create a new class with nib for the search bar and buttons which would then have a viewDidLoad/viewDidAppear and I could then create a delegate function so I could 'deQueue' the text to go into the search bar?
Or, am I missing something really simple?

'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name

I have a window based applicaiton with tab bar and navigation bar integrated into it. On the navigation bar have a custom button which opens a table view.
On clicking on the cell of the table I want to open a new table view controller. But as soon as I click on the cell of first table I get an exception that
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'AddForm''
I have checked that AddForm is in right place.
I have tried all the solutions given above and on other thread, but still stuck. I hope someone can help me.
The flow is something like this
ListButton (on click opens a list view) -> a table view opens -> (on click of a cell should open a new table view Controller and fetch data from core data)
My root view controller code is in the second table view controller file... is that what I am doing wrong? I am doing so because I don't need the core data before that....
Please help
Thanks in advance
I Used to have the same problem first of all check in your AppDelegate.m class if the managedObjectContext is created
if it is created check if it is pass it as argument
something like this
MasterViewController *controller = (MasterViewController *)masterNavigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
if that is correct for you main view just be sure that you are passing it through your views something like this
self.detailViewController.managedObjectContext = self.managedObjectContext;
well at least that works for me =)
The most common cause of this problem is misspelling the name of the entity or getting the capitalization wrong. Check that AddForm is spelled exactly the same in the code and the data model editor.

How does MonoTouch autogenerate XIB code behind?

I'm a C# programmer dabbling in a bit of iPhone development using MonoTouch.
I add a new View Interface Definition to my project and double click to open it up in Interface Builder. I add a UIButton. I save the file, and inspect the xib.designer.cs file, and I can see no reference to the new button.
I downloaded the code from http://monotouchexamples.com/ where I could see an example of autogenerated code behind :
[MonoTouch.Foundation.Connect("infoButton")]
private MonoTouch.UIKit.UIButton infoButton {
get {
return ((MonoTouch.UIKit.UIButton)(this.GetNativeField("infoButton")));
}
set {
this.SetNativeField("infoButton", value);
}
}
I opened up MainWindow.xib in interface builder. I notice a few differences. File's Owner is of type UIApplication instead of NSObject. What is the importance of this? There is an App Delegate object of type AppDelegate. I can't add an AppDelegate to my own view, or at least I can't find it in the Library. Do I need to add one? I can see that the existing controls on MainWindow.xib have Referencing Outlets to the App Delegate. I add a new button and I want to hook it up. When I click and drag a New Referencing Outlet to the App Delegate a context menu appears that lists the existing controls. How do I add a new element to this list, or where does this list come from?
I've been spoilt by the Visual Studio world where I just dump a button on a form and start writing code for the click event. Could someone provide some pointers about the steps needed to get this working on MonoTouch?
Thanks,
Patrick
Adding a button by itself is not enough. The button is not accessible outside the Interface Builder. You need add an Outlet, and connect the button with the outlet in Interface Builder.
Remember: Outlets are the members in your Controller class that get a reference to the controls, you can't just access the controls without them.
As Dave says, you need to add an outlet to your controller class, and connect your button to that outlet, before any auto-generated code will appear. This caught me out too initially.
You choose your controller class in the Interface Builder library window, choose 'outlets' in the bottom part of the library, and add an outlet there. You then need to select your button, choose the connections tab of the inspector window, and drag from the "New referencing outlet" circle over to your controller class. Interface Builder will then prompt you to choose an outlet to connect to. Then when you save, you should get the auto-generated code appear in the .xib.designer.cs file, and then you should be able to reference your button via that outlet variable in your .xib.cs file.
It sounds like the project I created is out of date - I remember there were quite a few changes around how the generated buttons are created in the designer file. I will update the project soon for you.
As Dave said, to get the code to be auto generated you need to add an outlet with Interface Builder. There should be an example on this video here - http://bit.ly/aWoItN but the server seems to be down at the moment.
Hope this helps,
ChrisNTR

Resources