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.
Related
I'm working on a nvaigation-based application and I want to use "global variables" in it.
I used the AppDelegate class, in each view I want to access the variables with I do like the following:
.h
#interface Main : UIViewController{
iMEAppDelegate *datacenter;
}
#property (nonatomc, retain) iMEAppDelegate *datacenter;
#end
.m
#synthesize dataCenter;
-(void)viewDidLoad{
[super viewDidLoad];
dataCenter = (iMEAppDelegate *)[[UIApplication sharedApplication] delegate];
}
Now the first time the view appears it's working fine but when navigating to another
one and return back and repress the button that has the below code, the EXC_BAD_ACCESS error is thrown:
if ([dataCenter.userName isEqualToString:#""]){
SingIn *obj = [[SingIn alloc] initwithNimName:#"SingIn" bundle:nil];
[dataCenter.navController pushViewController:obj animated:YES];
[obj release];
I'm not sure why is this happening, I'm thinking it may be a memory management issue but I couldn't find a solution to it.
Please help.
From the code you provided, my guess for the reason of your EXC_BAD_ACCESS is that you have forgotten to retain your AppDelegate's properties, e.g. dataCenter.userName.
how to call an AppDelegate method from RootViewController without passing a delegate?
Just wondering whether there is a way to do this? (or do I need to create a delegate object in the RootViewController to hold a reference to the AppDelegate)
[[[UIApplication sharedApplication] delegate] someMethod];
Works like a charm!
You can get access to the app delegate from any controller using
MyDelegate* aDelegate = (MyDelegate *)[[UIApplication sharedApplication] delegate];
This happens so frequently, that I always add a method to MyCustomAppDelegate to do this for me (so I don't have a lot of casting in my code.
#implementation MyCustomAppDelegate ()
- (MyCustomAppDelegate *)appDelegate {
return (MyCustomAppDelegate *)[[UIApplication sharedApplication] delegate];
}
#end
now anywhere i can call
[MyCustomAppDelgate 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.
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
Sorry for the newbie question. I have a UITabBar in my main window view as well as an array of UINavigationControllers for each Tab. The structure is similar to the iPod app in that the main views can be seen by selecting TabBar items and then the user can drill down further with the NavigationController by pushing views to the stack.
What I would like to be able to do is to do the equivalent of pressing a TabBar button at the bottom from any of the subviews in code (i.e., change the selected property of the TabBar and display launch the first view controller for the tab).
Any help would be greatly appreciated.
Dave
[myTabBarController setSelectedIndex:index]
EDIT: Answering the part 2 question from the comment:
You can define a method in AppDelegate for switching to a different tab.
And you can get hold of appdelegate from anywhere and send a message..
something like:
MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
[appDelegate SwitchToTab:index]
alternatively...
[self.parentViewController.tabBarController setSelectedIndex:3];
I'd like to reply to Prakash, but can't figure out how. Maybe I'm blocked until my score goes up.
Anyhow, I hope this helps someone:
I was doing what Prakash said, and nothing was happening. It's because to get a pointer to my app delegate, I was doing this:
AppDelegate_Phone *appDelegate = [[AppDelegate_Phone alloc] init];
When I should have been doing this:
AppDelegate_Phone *appDelegate = (AppDelegate_Phone *) [[UIApplication sharedApplication] delegate];
Newbie mistake.
For this, You just need to take UITabBar controller -
.h File -
UITabBarController *_pTabBarController;
#property (nonatomic, retain) IBOutlet UITabBarController *_pTabBarController;
.m File -
// synthesize it
#synthesize _pTabBarController;
At initial load
// You can write one function to add tabBar -
// As you have already mentioned you have created an array , if not
_pTabBarController = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
UINavigationController *theNavigationController;
_pController = [[Controller alloc] initStart];
_pController.tabBarItem.tag = 1;
_pController.title = #"Baranches";
theNavigationController = [[UINavigationController alloc] initWithRootViewController:_pController];
theNavigationController.tabBarItem.tag = 1;
theNavigationController.tabBarItem.image = [UIImage imageNamed:#"icon_branches.png"];
[localViewControllersArray addObject:theNavigationController];
[theNavigationController release];
than you can set index as per your needs
self._pTabBarController.selectedIndex = 0; // as per your requirements
[self.parentViewController.tabBarController setSelectedIndex:3];
Selected the index for me but it just highlighted the navbarcontroller's index as the active index, but while it highlighted that index it was actually on a different viewcontroller than was suggested by the tabbarmenu item.
Just wanted to add that I used this from my view controller, and it performed like someone actually pressed the menuitem; from code:
UITabBarController *MyTabController = (UITabBarController *)((AppDelegate*) [[UIApplication sharedApplication] delegate]).window.rootViewController;
[MyTabController setSelectedIndex:1];
Thank you for this post/answers it helped out a lot in my project.
I wanted to do something similar but for XCode 6.4 iOS (8.4) setSelectedIndex by itself won't do it.
Add the view controllers of the tab bar to a list and then use something like the following in some function and then call it:
FirstViewController *firstVC = [[self viewControllers] objectAtIndex:0];
[self.selectedViewController.view removeFromSuperview]
[self.view insertSubview:firstVC.view belowSubview:self.tabBar];
[self.tabBar setSelectedItem:self.firstTabBarItem];
self.selectedViewController = firstVC;
You might have similar code already inside your didSelectedItem..
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
if (item == self.firstTabBarItem)
// Right here
}
else if ...
}