Global variables access problem iphone - iphone

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.

Related

Objective-c / xcode - Access button state in different class

So I tried this in many different ways but I can't get it to work. Im trying to change the state of a UIbutton in a different class.
class1.h
#property (strong, nonatomic) IBOutlet UIButton *monthly;
class2.m
- (void)viewDidLoad
{
ViewController *vc = [[ViewController alloc] init];
vc.monthly.enabled = NO;
}
Whatever I try and where ever I put the code, the button state is not changing. When I log the state in class2.m:
NSLog(vc.monthly.enabled ? #"Yes" : #"No");
It always returns No, even if I just stated it as YES in my class2.m. Long story short: My button property is not updating from a different class. Please tell me if you need to see any more code and i'll update asap.
i think problem is with class instance. the following line create new instance
ViewController *vc = [[ViewController alloc] init];
that's why your button state is not changing you have to get reference of your previously created intstace no need to create new instance.
for this you can use AppDelegate file to declare property of class1.
see following code
AppDelegate.h
#Property(nonatomic, ratain) ViewController *vc;
AppDelegate.m
#synthesize vc;
now alloc & initialize vc whenever you need it like following.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication
sharedApplication] delegate];
appDelegate.vc=[[ViewController alloc] init];
also don't forgot to import AppDelegate.h file where you write above code.
now using appDelegate.vc you can use all property of View Controller in all classes of you project.
The main problem is you are creating new instance each time when you are going to check the button state. But the button state is for the button which you have created in class1.h. So you have need that you should create button in Appdelegate class and fetch the instance from Appdelegate in the class where you are checking the status of button and check the status of button. I think it will help.
Access the button using the object of the ViewController class that is already in the stack. No need to creat a new object like ViewController *vc = [[ViewController alloc] init];. When you are doing this it creates a new object so you are not getting the write thing.

load data into uipickerview with NSMutableArray located in app delegate

basically i have parsed some data from XML into a NSMutableArray that is shared in the appDelegate.
in my secondViewController i have a uiPickerView that i am wanting to load the details of the array into it.
My question is... how?
i have briefly worked with uiPickerView's before and had to load the data in first to assign to the uiPickerView like so:
titleDB = [[NSMutableArray alloc] init];
[titleDB addObject:#"MR"];
[titleDB addObject:#"Mrs"];
[titleDB addObject:#"Ms"];
[titleDB addObject:#"Miss"];
[titleDB addObject:#"DR"];
[titlePickerView selectRow:1 inComponent:0 animated:YES];
but since the data is coming from the appDelegate i don't know how i should load it into the uiPickerview, is it something to do with the datasource?
I'm asking to throw code at me I'm just asking for the best way to do it.
Any help on this would be great
Thanks
Jonn4y
This is a common pattern. You will want to access UIApplication's sharedApplication instance. So assuming your appDelegate class is named YourAppDelegate, the array ivar in YourAppDelegate and viewController is titleDB then you could do this in your viewController's viewDidLoad method
YourAppDelegate *appDelegate=(YourAppDelegate *)[[UIApplication sharedApplication] delegate];
// assuming you are using #property and #synthesize for your ivars
self.titleDB=appDelegate.titleDB;
Good luck
Basically, you want to access the appDelegate object from random places in code. This is not an unusual request. Remember that Objective C is a superset of the C language. And as such, you can use a global variable. What more natural variable would there be in a Cocoa program than the app delegate, for the reasons stated above. So, in your appDelegate .h file, add:
<MyAppDelegateClass> * appDelegate;
Substitute MyAppDelegateClass for the name of your appDelegate class name. Then just include your appDelegate's .h file anywhere you want to use the appDelegate variable, and just use (in your example):
[appDelegate titleDB]
or create a local iVar:
NSMutableArray * titleDB = [appDelegate titleDBData];
Then in your app delegate method didFinishLaunchingWithOptions, add the following line:
appDelegate = self;

iPhone - self dismissing a viewController

I have a view controller that was presented using
[self presentModalViewController:myVC animated:YES];
this VC has several declared retained properties (#property) that I have to release on its dealloc.
The variables are declared as
#property (nonatomic,retain) myClass1 *myProperty;
#property (nonatomic,retain) myClass2 *myProperty2;
// etc... and then synthesized on .m
The problem is that when I dismiss the viewController using
[self dismissModalViewControllerAnimated:YES];
it crashes on the dealloc, when releasing the retained properties I have declared, with the error *modifying layer that is being finalized *
Apparently the the viewController is gone at the time its own dealloc runs and then it crashes.
How do I solve that? Thanks in advance.
edit
the code that presents the viewController is on the rootViewController and is this:
UIViewController *myVC = [[UIViewController alloc] init];
myVC.delegate = self;
UINavigationController *navigator = [[UINavigationController alloc] initWithRootViewController:myVC];
[self presentModalViewController:navigator animated:YES];
[navigator release];
[myVC release];
and this is the what the dealloc code on myVC contains
- (void) dealloc {
[myProperty1 release]; // see this properties at the beginning of this question
[myProperty1 release]; // if I comment these 2 relesases it stops crashing
[super dealloc];
}
myVC is dismissed from inside itself, but that's fine according to the docs. I have also tried to dismiss it from the rootviewController but it continues to crash. The only way to stop crashing is to disable the release lines on the dealloc.
How do you set the properties?
self.myProperty =
? My guess is that you do not retain them. Are you doing
myProperty = ...
by any chance, with neither a retain or self.? The setter you synthesize needs a chance to actually retain your newly created object...
So in full it should read something like this:
MyClass1 *aProperty = [[MyClass1 alloc] init];
self.myProperty1 = aProperty;
[aProperty release];
...i asked where you allocate and init "myProperty1" and "myProperty2"...
i'm afraid that you make confusion thinking that this:
#property (nonatomic,retain) myClass1 *myProperty;
#property (nonatomic,retain) myClass2 *myProperty2;
need that you release myProperty and myProperty2
well, you are wrong!
you are just declaring the kind of objects you are going to use, not allocating them
you need to release them just if you alloc them someWhere...

Creation of new Nav views, which one should be the best ? iPhone

I have been wondering which is the best way to load a navigation view. I have found that there are 3 ways I can do it without having major errors
What I was wondering is which one is best for memory and as a recommended practice ??
1)
no declaration in .h file (the code below IS NOT writen in the .h file)
#interface companyViewController : UIViewController {
EmployeeViewController *employeeDetailViewController;
}
#property (nonatomic, retain) EmployeeViewController *employeeDetailViewController;
then no #syntesize in .m file, no release in dealloc and no nil in viewDidUnload and when I call the new view I do:
EmployeeViewController *employeeController = [[EmployeeViewController alloc]
initWithNibName:#"EmployeeViewController" bundle:nil];
[self.navigationController pushViewController:employeeController animated:YES];
[employeeController release];
2)
I create it in the .h file (the code below IS written in the .h file)
#interface companyViewController : UIViewController {
EmployeeViewController *employeeDetailViewController;
}
#property (nonatomic, retain) EmployeeViewController *employeeDetailViewController;
then I #syntesize in .m file, with a release in dealloc and a nil in viewDidUnload and when I call the new view I do:
EmployeeViewController *employeeController = [[EmployeeViewController alloc]
initWithNibName:#"EmployeeViewController" bundle:nil];
employeeDetailViewController = employeeController;
[self.navigationController pushViewController:employeeController animated:YES];
[employeeController release];
3)
I do like 2 but I call the new view like this
employeeDetailViewController = [[EmployeeViewController alloc]
initWithNibName:#"EmployeeViewController" bundle:nil];
[self.navigationController pushViewController:employeeController animated:YES];
I feel like #3 is wrong because from what I understand in the memory management, I allocate it once in the #property (nonatmoic, retain) and I also retain it when I alloc it when I decide to call it. This will make the view have a retain count of 1 and lead to leaks.
To make sure I do not create an excessive amount of new views and get EXC_BAD_ACCESS or memory leaks, which one should be best ?
Thanks for the help
In none of these examples are you actually using the property you declare. I'll go through them in turn.
You created a property called employeeDetailViewController, but you never synthesize it or store anything in it. Your view controller is only ever stored in a local variable before passing it off to the navigation controller.
You create an instance variable called employeeDetailViewController, and a property also called employeeDetailViewController. However, you never store anything in the property, you only access the instance variable directly. Since you don't retain the view controller (it doesn't happen automatically when you use instance variables), you've got a situation where you might over-release.
This one will actually crash. You told it to pushViewController:employeeController without having defined employeeController.
So, let's consider what's right. There are two issues here: firstly, how to use properties, and secondly, whether you need an instance variable/property to refer to the view controller at all.
Considering properties:
To access a property, you use self.propertyName. If you just use propertyName directly, you're writing directly into the instance variable, and the memory management stuff (like automatically retaining stuff that's put in the property) is completely bypassed. Generally, you should only ever do that in your init or dealloc method: everywhere else you should access the property properly by self.propertyName.
Do you need an instance variable/property for the view controller?
I would say you don't actually need an instance variable or property for the view controller you're pushing. Once it's been handed off to the navigation controller, in general you won't need to access it again. My version of your code would be:
aViewController = [[EmployeeViewController alloc]
initWithNibName:#"EmployeeViewController" bundle:nil];
[self.navigationController pushViewController:aViewController animated:YES];
Unless you're wanting to refer to that particular view controller from elsewhere in your code, this is all you need. Nothing in the header, no property anywhere.
Property access is more like
self.employeeDetailViewController = [[[EmployeeViewController alloc] initWithNibName:#"EmployeeViewController" bundle:nil] autorelease];
And then,
-(void)dealloc {
self.employeeDetailViewController = nil;
[super dealloc];
}

How to fix: unrecognized selector sent to instance

I am having a problem that may be simple to fix, but not simple for me to debug. I simple have a button inside a view in IB; the file's owner is set to the view controller class I am using and when making the connections, everything seems fine, ie. the connector is finding the method I am trying to call etc.
however, I am receiving this error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIApplication getStarted:]: unrecognized selector sent to instance 0x3d19130'
My code is as follows:
RootViewController.h
#interface RootViewController : UIViewController {
IBOutlet UIButton* getStartedButton;
}
#property (nonatomic, retain) UIButton* getStartedButton;
- (IBAction) getStarted: (id)sender;
#end
RootViewController.m
#import "RootViewController.h"
#import "SimpleDrillDownAppDelegate.h"
#implementation RootViewController
#synthesize getStartedButton;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction) getStarted: (id)sender {
NSLog(#"Button Tapped!");
//[self.view removeFromSuperview];
}
- (void)dealloc {
[getStartedButton release];
[super dealloc];
}
#end
Seems simple enough...any thoughs?
It looks like you have released the RootViewController after add it to your window.
Post here the piece of code where you add RootViewController to your window.
BTW, try to comment the line where you do the release. So, instead of use:
RootViewController *viewController = [[RootViewController alloc] init];
[window addSubview:viewController.view];
[viewController release];
Do it:
RootViewController *viewController = [[RootViewController alloc] init];
[window addSubview:viewController.view];
//[viewController release];
Your method "getStarted" should work after that.
Cheers,
VFN
You're sending getStarted to UIApplication and not RootViewController. Double check to make sure the button is hooked up properly to the view controller in Interface Builder. Your code looks fine.
I have the same problem, and finally found the answer at this forum post:
http://iphonedevbook.com/forum/viewtopic.php?f=25&t=706&start=0
When you instantiate your view controller class to be pushed into the navigation controller or similar, you have to make sure you instantiate it with your custom class name, and not UIViewController. This is easy to miss, and difficult to debug.
For example:
// this is wrong
RootViewController *viewController = [[UIViewController alloc] initWithNibName:#"TestView" bundle:nil];
// It should be
RootViewController *viewController = [[RootViewController alloc] initWithNibName:#"TestView" bundle:nil];
Hope it helps!
PS: Oops. Just noticed that your error says UIApplication, and not UIViewController, unlike mine. So probably like what Marc W said, somewhere you have used UIApplication where you should have used your custom class. It is probably something like my mistake.