NSManagedObjectContext starts with value and become null - xcode

I have just started developing iPad apps and I'm struggling with an issue for too long now, so I've decided to go for help.
I have an application for iPad using storyboard and started as a Tabbed application using CoreData. So the issue is, my NSManagedObjectContext starts with a value but when I move to another tab, the managedObjectContext becomes null.
Don't know what to do. Some help would be greatly appreciated.
Thanks
Elkucho

I'm slightly confused...
self.window.rootViewController
should return your instance of UITabBarController, which should not respond to setManagedObjectContext: and therefore should crash.
With this in mind what you need to do it
Get the tabBarController
Cycle through the viewController's that the tabBarController manages
Pass them the managedObjectContext
for (id viewController in self.window.rootViewController.viewControllers) {
[viewController setManagedObjectContext:self.managedObjectContext];
}
Edit
I've taken a quick look.
You rarely need to subclass UITabBarController and you don't really need to in this case.
What you want to do is just get the managedObjectContext to each of the viewControllers in the tabBarController, the tabBarController itself does need to know about it.
I changed your application:didFinishLaunchingWithOptions: to the following and it worked the way it was supposed to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = (id)self.window.rootViewController;
for (id viewController in tabBarController.viewControllers) {
[viewController setManagedObjectContext:self.managedObjectContext];
}
}

Related

How to use NSViewController in an NSDocument-based Cocoa app

I've got plenty of experience with iOS, but Cocoa has me a bit confused. I read through several Apple docs on Cocoa but there are still details that I could not find anywhere. It seems the documentation was written before the NSDocument-based Xcode template was updated to use NSViewController, so I am not clear on how exactly I should organize my application. The template creates a storyboard with an NSWindow, NSViewController.
My understanding is that I should probably subclass NSWindowController or NSWindow to have a reference to my model object, and set that in makeWindowControllers(). But if I'd like to make use of the NSViewController instead of just putting everything in the window, I would also need to access my model there somehow too. I notice there is something called a representedObject in my view controller which seems like it's meant to hold some model object (to then be cast), but it's always nil. How does this get set?
I'm finding it hard to properly formulate this question, but I guess what I'm asking is:how do I properly use NSViewController in my document-based application?
PS: I understand that NSWindowController is generally meant to managing multiple windows that act on one document, so presumably if I only need one window then I don't need an NSWindowController. However, requirements might change and having using NSWindowController may be better in the long run, right?
I haven't dived into storyboards but here is how it works:
If your app has to support 10.9 and lower create custom of subclass NSWindowController
Put code like this into NSDocument subclass
- (void)makeWindowControllers
{
CustomWindowController *controller = [[CustomWindowController alloc] init];
[self addWindowController:controller];
}
If your app has multiple windows than add them here or somewhere else (loaded on demand) but do not forget to add it to array of document windowscontroller (addWindowController:)
If you create them but you don't want to show all the windows then override
- (void)showWindows
{
[controller showWindow:nil]
}
You can anytime access you model in your window controller
- (CustomDocument *)document
{
return [self document];
}
Use bindings in your window controller (windowcontroller subclass + document in the keypath which is a property of window controller)
[self.textView bind:#"editable"
toObject:self withKeyPath:#"document.readOnly"
options:#{NSValueTransformerNameBindingOption : NSNegateBooleanTransformerName}];
In contrast to iOS most of the views are on screen so you have to rely on patterns: Delegation, Notification, Events (responder chain) and of course MVC.
10.10 Yosemite Changes:
NSViewController starting from 10.10 is automatically added to responder chain (generally target of the action is unknown | NSApp sendAction:to:from:)
and all the delegates such as viewDidLoad... familiar from iOS are finally implemented. This means that I don't see big benefit of subclassing NSWindowCotroller anymore.
NSDocument subclass is mandatory and NSViewController is sufficient.
You can anytime access you data in your view controller
- (CustomDocument *)document
{
return (CustomDocument *)[[NSDocumentController sharedDocumentController] documentForWindow:[[self view] window]];
//doesn't work if you do template approach
//NSWindowController *controller = [[[self view] window] windowController];
//CustomDocument *document = [controller document];
}
If you do like this (conforming to KVC/KVO) you can do binding as written above.
Tips:
Correctly implement UNDO for your model objects in Document e.g. or shamefully call updateChangeCount:
[[self.undoManager prepareWithInvocationTarget:self] deleteRowsAtIndexes:insertedIndexes];
Do not put code related to views/windows into your Document
Split your app into multiple NSViewControllers e.g.
- (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:AAPLListWindowControllerShowAddItemViewControllerSegueIdentifier]) {
AAPLListViewController *listViewController = (AAPLListViewController *)self.window.contentViewController;
AAPLAddItemViewController *addItemViewController = segue.destinationController;
addItemViewController.delegate = listViewController;
}
}
Previous code is called on windowcontroller with viewcontroller as delegate (again possible only after 10.10)
I always prefer to use multiple XIBs rather than one giant storyboard/XIB. Use following subclass of NSViewController and always inherit from it:
#import <Cocoa/Cocoa.h>
#interface MyViewController : NSViewController
#property(strong) IBOutlet NSView *viewToSubstitute;
#end
#import "MyViewController.h"
#interface MyViewController ()
#end
#implementation MyViewController
- (void)awakeFromNib
{
NSView *view = [self viewToSubstitute];
if (view) {
[self setViewToSubstitute:nil];
[[self view] setFrame:[view frame]];
[[self view] setAutoresizingMask:[view autoresizingMask]];
[[view superview] replaceSubview:view with:[self view]];
}
}
#end
Add a subclass of MyViewController to the project with XIB. Rename the XIB
Add NSViewController Object to the XIB and change its subclass name
Change the loading XIB name to name from step 1
Link view to substitute to the view you want to replace
Check example project Example Multi XIB project
Inspire yourself by shapeart or lister or TextEdit
And a real guide is to use Hopper and see how other apps are done.
PS: You can add your views/viewcontroller into responder chain manually.
PS2: If you are beginner don't over-architect. Be happy with the fact that your app works.
I'm relatively new to this myself but hopefully I can add a little insight.
You can use the view controllers much as you would in ios. You can set outlets and targets and such. For NSDocument-based apps you can use a view controller or the window controller but I think for most applications you'll end up using both with most of the logic being in the view controller. Put the logic wherever it makes the most sense. For example, if your nsdocument can have multiple window types then use the view controller for logic specific to each type and the window controller for logic that applies to all the types.
The representedObject property is primarily associated with Cocoa bindings. While I am beginning to become familiar with bindings I don't have enough background to go into detail here. But a search through the bindings programming guide might be helpful. In general bindings can take the place of a lot of data source code you would need to write on ios. When it works it's magical. When it doesn't work it's like debugging magic. It can be a challenge to see where things went wrong.
Let me add a simple copy-pastable sample for the short answer category;
In your NSDocument subclass, send self to the represented object of your view controller when you are called to makeWindowControllers:
- (void) makeWindowControllers
{
NSStoryboard* storyboard = [NSStoryboard storyboardWithName: #"My Story Board" bundle: nil];
NSWindowController* windowController = [storyboard instantiateControllerWithIdentifier: #"My Document Window Controller"];
MyViewController* myController = (id) windowController.contentViewController;
[self addWindowController: windowController];
myController.representedObject = self;
}
In you MyViewController subclass of NSViewController, overwrite setRepresentedObject to trap it's value, send it to super and then make a call to refresh your view:
- (void) setRepresentedObject: (id) representedObject
{
super.representedObject = representedObject;
[self myUpdateWindowUIFromContent];
}
Merci, bonsoir, you're done.

Setting tabBarController.selectedIndex/selectedViewController when it's a UINavigationController

I've got 5 views in my tabBarController and all of them are embedded in a separate navigationcontroller, i.e. every view has it's own navigationcontroller. I did this to make it easier to push segues, I know it's probably not the best solution but it works fine. Now to my question:
I'm trying to set the initial view with the following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
tabBarController.selectedIndex = 2;
return YES;
}
However it's not working at all, the app simply starts and index 0 (the left most view).
I've searched through thorough threads like this and tried many different ways to solve this without any success...
Closest I got was when I in MainStoryboard_iPhone.storyboard checked the box "Is initial view controller" in the viewcontroller I want to start with. This way the I got the correct starting viewcontroller but the tabbar wasn't showing.
Since you're using storyboard, do this :
1) Give your tabBarController a storyboard identifier (say tbc);
2) In your appDelegate DidFinishLaunching, do this ::
UITabBarController *tbc = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"tbc"];
[tbc setSelectedIndex:1];
[self.window setRootViewController:tbc];
[self.window makeKeyAndVisible];
return YES;
PS : This is just one of many ways to make it work

Objects releasing on iOS 6 but not in iOS 5

I am having a really weird issue when testing on my 1st gen. iPad (running iOS 5).
I have a UIView that I use as a property (with retain). I nil the property in the parent view's dealloc method. Pretty basic stuff. It works perfect on my iPad 3 running iOS 6, but doesn't get released on my 1st gen.
Any ideas what might be going on?
I'm not using ARC.
If you're retaining it, you have to release it. You can't just nil the instance variable.
So if you're property looks like this:
#property (nonatomic, retain) UIView *myView;
You're dealloc would either look like this:
- (void)dealloc
{
[myView release], myView = nil;
[super dealloc];
}
Or this:
- (void)dealloc
{
[self setMyView:nil];
[super dealloc];
}
Or this:
- (void)dealloc
{
self.myView = nil;
[super dealloc];
}
And your property will properly get released--unless something else is retaining it.
So I figured this out. It seems to be a bug in the iOS 6 SDK or maybe I just don't understand it. I have a UIViewController that presents another vc via presentViewController:animated:completion: —If I dismiss the presented vc then it releases and subsequently all subviews are removed and all is well.
However, if while the presented vc is showing, I remove/destroy the parent vc, the presented vc is deallocated but, its subviews are not told to removeFromSuperview; This doesn't show up as a leak in instruments, BUT it does prevent the subviews from deallocating.
This does not happen on iOS 6, thus I suspect this is a bug in iOS 5. Everything releases/deallocates as one would expect on iOS 6.
If someone has an explanation, or a better understanding of this, I would love to reward the answer to them instead of myself.
A view controller isn't responsible for removing its view from the superview when the view controller is dealloc'ed. The view controller is just responsible for releasing its own reference to it.
For example: you can create a view controller, ask for its view, then add that view to another view and throw away the view controller. In that case, you're just using the view controller as a view builder.
I'm not sure why the behavior is different in iOS 6, but would love to know.

How to navigate b/w three views using IBAction button method and round rectangle button in Xcode?

Opening Xcode 4.2, I chose a empty application template. All im trying to do is make three views, which can be navigated through the help of one button inserted [Round Rectangle Button].
I have made 3 Views with three buttons within them which are
1)Next
2)Next
3)Root
I am relatively new to Xcode. I have imported all the Views into the app delegates. I know i have done everything right. But I think my code for button is wrong.
I inserted a round rectangular button through the object library.
I went in FirstViewController.h, put in -(IBAction)nextbuttonclicked;
I then go in FirstViewController.m, go down, -(IBAction)nextbuttonclicked {
SecondViewController *SVC = [[SecondViewController alloc] init];
[self.window removeFromSuperview];
AppDelegate *appD = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[appD.window addSubview:SVC.view];
}
The error i get is: Thread 1: Program received signal: "EXC_BAD_ACCESS" while
"return UIApplicationMain (argc, argv, nil) NSStringFromClass ([[AppDelegate class]));"
is highlighted hence the thread is coming from here
Someone please help?
Study navigation controller - http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
and use navigation controller's Push and Pop properties for navigations.

How to make UIScrollview with xCode 4.2 and storyboards

I am new to iOS Development and am wondering how to put a scrollview in a storyboard, using Xcode 4.2. I want the content to be 1280 by 460. This code all works well, but when I go to wire up the outlet, there is no file's owner, so i'm stumped. Here is the code I have:
in the .h file-
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIScrollView *scrollView;
}
#end
and the .m, under viewDidLoad:
- (void)viewDidLoad
{
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(1280,460)];
[scrollView setPagingEnabled:YES];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
If anyone could help me, that would be great!
Storyboards do not have a File's Owner. You need to use the View Controller to connect the Outlets instead. I.E. Drag to the View Controller in the same way you used to drag to the File's Owner.
OK, I figured it out. I just had to create a class for my view controller. I was trying to use files that were not part of a class that was a subclass of UIViewController.I had to create a new class, then copy the old code in and make my view controller's class that of the NEW files, and THEN wire up the outlets. I was changing the class to something that was not there. I then wired my outlet up to the scroll view I wanted. Thankfully, that part is finally over! Thanks for the suggestions, guys, I really appreciate it.
A good visual tutorial is in the current Stanford CS193p course for iOS 5 in iTunes U.
This course works mainly with storyboards and they cover among other things UIScrollViews

Resources