I have tab bar navigation application and The problem is that in my root controller I am able to set its NSContextManagedObject to the app delegates...However when I try to do the same on the other controller the application freezes...
This only happens in the ViewDidLoad but thats where I need to set it so I can fetch the data
Im accessing it like this but it works on the RootviewController:
MyAppDelegate appD = (MyAppDelegate)[[UIApplication sharedApplication] delegate];
managedObjectContext = appD.managedObjectContext
Just does not work when I use it on other view controllers
I faced the same problem on my application. And I solved this by adding the following line that configures the managedObjectContext on my application delegate.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
viewController.managedObjectContext=self.managedObjectContext;
}
and on the viewController viewDidLoad you want to use the managedObjectContext:
- (void)viewDidLoad {
[super viewDidLoad];
MyAppDelegate *delegate=[[UIApplication sharedApplication]delegate];
self.managedObjectContext=delegate.managedObjectContext;
}
hope this helps,
Sarah
Related
I have an iPhone Application with multiple view controllers , in all view controller's header is common.
I dont want to use the same method and create common view in all controllers.
So My questions is how do i use this common view in all controllers.
Use initWithNibNamed:"name of your xib" when you alloc your new view controller. It's simple.
Same but we can avoid defining a variable -
[self.view addSubview:[[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] view]];
ok so you have to create it in Application Delegate once.
in .h
#property(nonatomic,strong) uiviewcontroller headerview;
in .m
#synthesize headerview=_headerview;
then alloc it in "didFinishLaunchingWithOptions" in appdelegate as singleton
self.headerview = [[headerview alloc] initWithNibName:#"headerview" bundle:nil];
So every time you want to add it to your view.
Create object from application delegate in your class after import it.
applicationdelegate app = [uiapplication sharedapplication]delegate];
[self.view addsubview:app.headerview.view];
I am using a delegate to store data for my app. I have a tabviewcontroller with a navigationviewcontroller as the first view controller. When i set the delegate using
delegate = [[UIApplication sharedApplication] delegate];
the delegate is null outside of the init method.
It however works fine when places in viewDidLoad. Any ideas why this could be?
UPDATE
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
delegate = [[UIApplication sharedApplication] delegate];
}
return self;
}
Is this view controller in your MainWindow.xib file (or whatever the main .xib in your project is called)? If so, the app delegate is likely also created when that file is loaded, and the app delegate probably is nil at time your view controller is created. If your app delegate has a reference to this view controller, have it set itself as the view controller's delegate in -applicationDidFinishLoadin:withOptions:, or just connect the app delegate to the view controller's -delegate outlet in the .xib file.
You shouldn't use -init method. Sometimes it's not being called. Use theese methods for additional initialization:
- (void)viewDidLoad {
[super viewDidLoad];
// do something
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// do something
}
The nib file was calling the initWithCoder method, not init.
I need to share data from a "SampleAppdelegate" to two view controller "ContactViewController.m" and "DrinksViewController.m".
In SampleAppdelegate:
#interface SampleAppdelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate , UINavigationBarDelegate>
So I've included below code in respective view controller.
In ContactViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
SampleAppDelegate *appDelegate = (SampleAppDelegate *)[[UIApplication sharedApplication] delegate];
contactDatabaseObject = (ContactDatabaseObject *)[appDelegate.contactDetails objectAtIndex:0];
NSLog(#"%#", contactDatabaseObject.contactName);
[appDelegate release];
}
In DrinksViewController.m
(void)viewDidLoad
{
[super viewDidLoad];
SampleAppDelegate *appDelegate = (SampleAppDelegate *)[[UIApplication sharedApplication] delegate];
drinksDatabaseObject = (DrinksDatabaseObject *)[appDelegate.drinkDetails objectAtIndex:0];
NSLog(#"%#", drinksDatabaseObject.drinkName);
[appDelegate release];
}
The problem is "sharing application delegate is is both of the code not working at the same time. ( that is, if I comment above in a view controller then other is working fine as as expected).
Actually i'm using a tab-bar with two item as above viewControllers. So loading of the first controller happens fine. But when i click the second view controller the iphone-simulator is terminated with any warning. ( And at the same time the code works normal if I comment it in any one of the view controller). Please help me to resolve above issues or suggest a method to accomplish above feature.
Thanks in advance !
You should read ...
http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html
You're not owner of appDelegate, no need to release it.
P.S. If you're asking for help and writing something about warning, you should always copy warning here to shed more light on your problem.
You should not be releasing the appDelegate.
i am using a UINavigationController in my application.
in the first view i am displaying some information and have a button for loading an picture from the camera. this works fine so far. when the picture was loaded, i want to display the picture in a UIImageView within a second view. Therefore i am using the following code.
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
CorrectedImageController *imageController = [[CorrectedImageController alloc] initWithNibName:#"CorrectedImageView" bundle:nil];
self.correctedImageViewController = imageController;
[imageController release];
[delegate.navController pushViewController:imageController animated:YES];
The second view is loaded and the picture is shown. But i get an EXC_BAD_ACCESS message in the Debugger Console and my Application blocks the UI.
In the second view the picture is loaded as follows:
- (void)viewDidLoad {
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[imageView setImage:delegate.correctedImage];
[super viewDidLoad];
}
Can anybody tell me, what i did wrong ?
My nib file looks:
-File's Owner (Corrected Image Controller)
-First Responder (UIResponder)
-View (UIView)
--ImageView (UIImageView)
The File's owner view property is connected to the view.
And the imageView property is connected to the ImageView.
Can anybody tell me, what i did wrong ?
BR,
Martin
I believe this is because you are calling release on the imageController (line 5) and then trying the pushModalView the object you just released (line 6). If your self.correctedImageViewController is retaining the imageController, you could push the self.correctedImageViewController or release the imageController after the push.
Creating a new app based on the SplitViewController template and it works fine in Split View.
My main screen is to be a non-splitview 'menu'. I'm trying to figure out the best practice for
adding this 'mainMenu' modally above the splitViewController. (Then either push other non-split views above the mainMenu or
remove it to reveal and use the UISplitViewController.)
I have tried:
[self.navigationController presentModalViewController:mainMenu animated:NO];
And
[self presentModalViewController:mainMenu animated:NO];
In the viewWillAppear and viewWillLoad methods for rootViewController & detailViewController. In both cases, the code executes without error, but the mainMenu doesn't appear, the regular detailViewController and rootViewControllers appear.
(I did create an outlet from the navigationController in the main.xib file to the detailView navigationController, but that didn't change anything.)
I was able to make this work by using, which works, but seems like it is incorrect.
iPad_Prototype_SplitAppDelegate *delegate = (iPad_Prototype_SplitAppDelegate *) [ [UIApplication sharedApplication] delegate];
[delegate.splitViewController.view addSubview:mainMenu.view];
[delegate.splitViewController.view bringSubviewToFront:mainMenu.view];
I've seen many responses saying to present such a covering view modally, but I can't seem to find the right place or configuration in the splitViewController setup. Thanks for any help or insight.
Finally, is this approach wrong, should I just be swapping out the detailViewController and having it take full screen in portrait mode and not add the menu item for the root controller?
Is your splitViewController in the AppDelegate like the example and will this help?
//AppDelegate.m
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MyController *myCont = [[MyController alloc] initMainMenu];
// mess around with myCont.view.modalPresentationStyle;
[myCont setModalDelegate:self];
// Create a delegate (<ModalControllerDelegate>) to dismiss view when done
[self.splitViewController presentModalViewController:myCont animated:NO];
[myCont release];
}
// for completion sake
-(void)modalViewDismiss:(MyController *)modalView {
[self.splitViewController dismissModalViewController:YES];
}