How do I fix setManagedObjectContext error? - objective-c

Whenever I run my app it always crashes and gives me this error:
2014-08-10 12:50:58.802 EIC Scanner[2184:133860] -[UITabBarController
setManagedObjectContext:]: unrecognized selector sent to instance
0x7ae7b9f0 (lldb)
I have an exception breakpoint so it leads me to this line of code:
rootView.managedObjectContext = self.managedObjectContext;
AppDelegate.h:
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
#end
Part of AppDelegate.m:
#import "AppDelegate.h"
#import "NotesList.h"
#import <CoreData/CoreData.h>
#implementation AppDelegate
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// IMPORTANT: Pass the managedObjectContext to the root view controller
NotesList *rootView = (NotesList *)_window.rootViewController;
rootView.managedObjectContext = self.managedObjectContext;
return YES;
}
As requested NotesList.h:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface NotesList : UIViewController <
UITableViewDelegate,
UISearchBarDelegate, UISearchDisplayDelegate
>
// CoreData properties *******
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (strong, nonatomic) NSMutableArray *notesArray;
//UI VIEWS ***********
#property (retain, nonatomic) IBOutlet UIView *topView;
//TableView ********
#property (retain, nonatomic) IBOutlet UITableView *tableView;
#end
If you know how to fix this, please comment on how. Thanks for the help!

Related

Passing map-data between ViewController

I nearly followed this post. The viewcontroller will push via storyboard. Here the relevant code:
ViewController.m:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"%#", view.annotation.title);
MapPoint *annView = view.annotation;
DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
dvc.title = #"my own Details";
dvc.titleTMP = annView.title;
dvc.subtitleTMP = annView.subtitle;
[self.navigationController pushViewController:dvc animated:YES];
}
MapPoint.h:
#interface MapPoint : NSObject <MKAnnotation>
{
NSString *_title;
NSString *_subtitle;
CLLocationCoordinate2D _coordinate;
UIImage *_storeImage;
}
#property (copy) NSString *title;
#property (copy) NSString *subtitle;
#property (nonatomic, readonly) UIImage *storeImage;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString*)title subtitle:(NSString*)subtitle coordinate:(CLLocationCoordinate2D)coordinate storeImage:(UIImage*)storeImage;
and DetailViewController:
#interface DetailViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *branchImage;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UITextView *textView;
I think my mistake is in the calloutAccessoryControlTapped method but I dont know what is the real reason for this issue.
If you use a storyboard, can use prepareForSegue method and set within a class similarly as you have made.
I post a portion of code
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"Brs"]){
NSLog(#"segue %# ",[segue identifier]);
[segue.destinationViewController setPath:[ris_xml objectAtIndex:index_post-1] ];
}
}
In this example I set attribute "Path" of next UIViewController only if his identifier is "Brs".
For use this method is need set UIviewController identifier into storyboard right panel.
Using this isn't need to instantiate new UIViewController if you have it in storyboard.
I solved it by thinking about loading sequence.
All what to do is create temporary #properties
#interface DetailViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *branchImage;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UITextView *textView;
#property (nonatomic) NSString *titleTMP; //added
#property (nonatomic) NSString *subtitleTMP; //added
and do
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"%#", view.annotation.title);
MapPoint *annView = view.annotation;
DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
dvc.title = #"my own Details";
dvc.titleTMP = annView.title;
dvc.subtitleTMP = annView.subtitle;
[self.navigationController pushViewController:dvc animated:YES];
}
The DetailViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
titleLabel.text = titleTMP;
textView.text = subtitleTMP;
}

Restkit mapping relationship

Here is my json
{
"status":"ok",
"count":1,
"count_total":164,
"pages":164,
"posts":[{"id":2966,
"type":"post",
"title":"title here",
"content":"here content",
"date":"",
"attachments [
"comment_count":0,
"thumbnail":""]
}]
}
one class Attachments and one class articles
Attachments.h
#import <Foundation/Foundation.h>
#interface Attachments : NSObject
#property (strong, nonatomic) NSString *thumbnail;
#end
Attachments.m
#import "Attachments.h"
#implementation Attachments
#synthesize thumbnail;
#end
articles.h
#import <UIKit/UIKit.h>
#import "Attachments.h"
#interface articles : NSObject
#property (strong, nonatomic) NSString *title;
#property (strong, nonatomic) Attachments *attachments;
#property (strong, nonatomic) NSString *date;
#end
articles.m
#import "articles.h"
#implementation articles
#synthesize title;
#synthesize attachments;
#synthesize date;
#end
viewcontroller.h
#import <UIKit/UIKit.h>
#import <RestKit/RestKit.h>
#import "articles.h"
#import "postCell.h"
#interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, RKObjectLoaderDelegate> {
__weak IBOutlet UISearchBar *_searchBar;
__weak IBOutlet UITableView *_tableView;
}
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
#property (strong, nonatomic) NSMutableArray *post;
#end
in viewcontroller.m
RKObjectMapping* attachmentsMapping = [RKObjectMapping mappingForClass:[Attachments class]];
[attachmentsMapping mapKeyPathsToAttributes:#"thumbnail", #"thumbnail", nil];
RKObjectMapping *postMapping = [RKObjectMapping mappingForClass:[articles class]];
[postMapping mapKeyPathsToAttributes:#"title",#"title",#"date",#"date",nil];
[postMapping mapKeyPath:#"attachments" toRelationship:#"attachments" withMapping:attachmentsMapping];
[objectManager.mappingProvider setMapping:postMapping forKeyPath:#"posts"];
[self searchRequest];
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects {
NSLog(#"objects[%d]", [objects count]);
[posts removeAllObjects];
for (articles *t in objects) {
[posts addObject:t];
}
[self.tableView reloadData];
}
i get this error
[RKObjectMappingOperation transformValue:atKeyPath:toType:] Failed
transformation of value at keyPath 'attachments'. No strategy for
transforming from '__NSArrayM' to 'Attachments'
What's wrong?
Thanks
Change
#property (strong, nonatomic) Attachments *attachments;
To
#property (strong, nonatomic) NSMutableArray *attachments;
The relationship mapping returns an array of data, even if there's only one.

Use of undeclared identifier in AppDelegate?

In my AppDelegate implementation file i get an error for an undeclared identifier but it appears to be declared, what am i overlooking?
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize managedObjectContext=__managedObjectContext;
#synthesize managedObjectModel=__managedObjectModel;
#synthesize persistentStoreCoordinator=__persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
- (NSManagedObjectContext *)managedObjectContext
the error occurs at this line "Use of undeclared identifier "managedObjectContext"
In my AppDelegate header file:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, strong) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory;
- (void)saveContext;
#end

Image is not loading on UIImageView

I am showing data in a UITableView with a thumbnail image. Initially, when the cell is clicked on, I would reload the image from the server which would take additional time, resources, and memory. Instead of doing this, I thought of passing a pointer of the thumbnail image to the view that will be on screen and display it immediately, instead of having to redownload it. However, I am running into a problem - nothing loads when I set the imageView on the new view to the old image. Here is my code:
This is in my UITableView Class:
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
NewsViewController *newsViewController = [[NewsViewController alloc] initWithNibName:#"NewsViewController" bundle:nil dictionary: dict type:d numberOfPages:1 image: cell.imageView.image];
NewsViewController.m:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil dictionary:(NSDictionary *)dict type:(NSInteger)type numberOfPages:(NSInteger)pages image:(UIImage *)image{
self = [super initWithNibName:#"NewsViewController" bundle:Nil];
if (self) {
newsText = [dict objectForKey:#"content"];
newsTitle = [dict objectForKey:#"title"];
newsDate = [dict objectForKey:#"pub_date"];
d = type;
numberOfPages = pages;
imageURL = [[NSMutableArray alloc] initWithCapacity:numberOfPages];
mainImage = [[UIImage alloc] init];
mainImage = image;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
labelBody.text= newsText;
titleView.text = newsTitle;
date.text = newsDate;
if (numberOfPages == 1) {
imageScrollView.contentSize = CGSizeMake(320, 303);
pageControl.numberOfPages = 1;
CustomImageView *customImageView = [[CustomImageView alloc] initWithFrame:imageScrollView.bounds];
[imageScrollView addSubview:customImageView];
customImageView.imageView.image = mainImage;
}
}
NewsViewController.h
#import <UIKit/UIKit.h>
#import "AdWhirlView.h"
#import "AdWhirlDelegateProtocol.h"
#class Reachability;
#interface NewsViewController : UIViewController<AdWhirlDelegate, UIScrollViewDelegate>{
AdWhirlView *adView;
IBOutlet UILabel *labelBody;
IBOutlet UILabel *titleView;
IBOutlet UILabel *subTitle;
IBOutlet UILabel *date;
IBOutlet UIScrollView *scrollView;
IBOutlet UIScrollView *imageScrollView;
IBOutlet UIImageView *backgroundImage;
IBOutlet UIImage *mainImage;
IBOutlet UIPageControl *pageControl;
NSInteger numberOfPages;
NSMutableArray *imageURL;
}
#property(nonatomic,retain)AdWhirlView *adView;
#property (nonatomic, retain) NSMutableArray *imageURL;
#property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
#property (nonatomic, retain) IBOutlet UIScrollView *imageScrollView;
#property (nonatomic, weak) IBOutlet UIImageView *backgroundImage;
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIImage *mainImage;
#property (nonatomic, strong) NSString *newsText;
#property (nonatomic, strong) NSString *newsTitle;
#property (nonatomic, strong) NSString *newsDate;
#property (nonatomic, assign) NSInteger numberOfPages;
#property (nonatomic, strong) IBOutlet UILabel *titleView;
#property (nonatomic, strong) IBOutlet UILabel *labelBody;
#property (nonatomic, strong) IBOutlet UILabel *subTitle;
#property (nonatomic, strong) IBOutlet UILabel *date;
#property (nonatomic, retain) Reachability* internetReachable;
#property (nonatomic, assign) BOOL internetActive;
-(void) checkNetworkStatus:(NSNotification *)notice;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil dictionary: (NSDictionary *)dict type:(NSInteger)type numberOfPages:(NSInteger) pages image:(UIImage *)image;
- (BOOL)checkInternet;
- (void)doStuff;
- (IBAction)ad;
#end
I am using mainImage as a (UIImage *) and trying to replace the image in my customImageView class with the main Image but nothing is showing up. What am I missing here?
I guess you'd like to build some sort of caching mechanism for your UIImageView. One of the easiest ways to get all of this working is by making use of the AFNetworking library.
AFNetworking extends UIImageView with some extra methods for loading images from remote sources. AFNetworking has caching build-in, no need to reinvent the wheel :)
Take a look on the following sites:
AFNetworking
UIImageView (AFNetworking)

using appDelegate to share data - getting "request for member in __ not in structure or union"

I've got a simple example where i have a delegate that has a string, along with a property to expose it.
myAppDelegate.h
#import <UIKit/UIKit.h>
#interface myAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UITabBarController *rootController;
NSString* myText;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#property (nonatomic, retain) NSString *myText;
#end
myAppDelegate.m
#import "myAppDelegate.h"
#implementation myAppDelegate
#synthesize window;
#synthesize rootController;
#synthesize myText;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:rootController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[rootController release];
[window release];
[super dealloc];
}
#end
So in my main view controller, I try something like this:
myAppDelegate* ad = (myAppDelegate*)[UIApplication sharedApplication].delegate;
ad.myText = #"blah";
and I get: Request for member 'myText' in something not a structure or union
does anyone know why this is happening?
Have you tried using setter instead of dot notation?
[ad setMyText:#"blah"];