how to access NSMutablearray in another UIViewcontroller? - ios5

I have a NSMutable array defined in NSObject class like this
NSMutableArray *allbilltypeArray=[[NSMutableArray alloc] initWithArray:[sqlite executeQuery:#"SELECT * FROM dir_AddBillName"]];
Now I want to get element one by one from this NSMutablearray in another view controller.How can I do this in another view controller.
Thanks in advance,

You want to declare the NSMutableArray as a property of its containing class in the header file, like so:
#property (nonatomic, retain) NSMutableArray *allbilltypeArray;
You should then synthesise the property in the implementation file.
Now if you declare an instance of the class that contains the array in another view controller like so (or similar):
ContainingClass *class = [[ContainingClass alloc] init];
you will be able to access a particular element in the array by doing something like:
Object *o = [class.allbilltypeArray objectAtIndex: i];
Or loop through them all like so:
for (Object *o in class.allbilltypeArray) {
//Do something with object
}
Hope this helps.

Related

How to pass data between not neighbor view controllers in Storyboard

I've stuck in the problem.
I have a storyboard with several view controllers.
What I need to do is:
I need to pass an array from FirstViewController to SecondViewController (they are not neighbors and are not connected via segue) where PikerView will upload the array. After that the picked result should be passed to ThirdViewController.
I have tabbed applicateion where FirstViewController and SecondViewControllers are connected to Tab Bar View Controller and ThirdViewController is connected with SecondViewController via Push Segue.
See how I try to pass data form First to Second
CategoryExpencesViewController.h
#import <UIKit/UIKit.h>
#import "AddCategoryViewController.h"
#import "CategoryPickerViewController.h"
#interface CategoryExpencesViewController : UITableViewController <AddCategoryViewControllerDelegate>
#property(nonatomic, weak) IBOutlet UIBarButtonItem *editButton;
#property(nonatomic, strong) NSMutableArray *categories; //list of category items
#property(nonatomic, strong) NSMutableArray *listOfCategories; //list of category names
CategoryExpencesViewController.m
-(void)updateArray
{
CategoryPickerViewController *controller = [[CategoryPickerViewController alloc]init];
controller.categoryList = [[NSMutableArray alloc]init];
controller.categoryList = listOfCategories;
NSLog(#"%d", [listOfCategories count]);
NSLog(#"%d", [controller.categoryList count]);
}
I think you need this
Use your's Push Segue.
segue.sourceViewController (or self) will point at SecondViewController.
segue.sourceViewController.tabBarController will point at Tab Bar Controller.
From Tab Bar Controller you will find your's FirstViewController.
I suppose you would have solved it, but i post this just for the record:
Wrap the array into a class, and make it have a static construction method:
Wrapper.h:
#property (nonatomic, strong) NSMutableArray* array;
+(Wrapper*)createArray;
Wrapper.m:
+(Wrapper*)createArray{
static Wrapper* instance = nil;
if (instance == nil) {
instance = [[Wrapper alloc] init];
//Your initialization code for the array
}
return instance;
}
Then, in your FirstViewController:
-(void)updateArray{
CategoryPickerViewController *controller = [[CategoryPickerViewController alloc]init];
controller.categoryList = [[NSMutableArray alloc]init];
controller.categoryList = [[Wrapper createArray] array];
NSLog(#"%d", [listOfCategories count]);
NSLog(#"%d", [controller.categoryList count]);
}
As this is the first call to Wrapper, the array is generated.
Then in your SecondViewController, when you call:
secondView.categoryList = [[Wrapper createArray] array];
and you obtain the same array as in FirstViewcontroller.
have you thought of NSUserDefault try it and you can also create a instance global variable in AppDelegate class and access it by creating an instance of AppDelegate in any other ViewController but i think NSUserDefault is the best option from all.

removeAllObjects for NSMutableArray that populates UITableView crashes with ARC

I have a property of
#property (nonatomic, strong) NSMutableArray *timesArray;
It is used to populate the data in my UITableView. When I want to clear my view, I do this:
- (void)clearView {
self.nameField.text = #"";
self.noteField.text = #"";
if ([_timesArray count] > 0) {
[self.timesArray removeAllObjects];
[self.customTableView reloadData];
}
}
The removeAllObjects causes a crash. I am not sure why. I looked around and a lot of posts talk about an object being overreleased. How is that happening if I'm using ARC and not calling retain/release on any objects. My _timesArray just holds NSDate objects I get from a UIDatePicker.
My stack trace looks like:
My insertPill looks like:
- (void)insertPill:(id)sender {
//[[NSNotificationCenter defaultCenter] postNotificationName:InsertPillNotification object:self];
[self clearView];
}
If I don't removeAllObjects, and just do:
NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithCapacity:0];
self.timesArray = emptyArray;
This works. But I'd still like to know why by removing the objects it does not work.
Edit: I initialize the array in viewDidLoad:
_timesArray = [[NSMutableArray alloc] initWithCapacity:0];
When I want to add a new object to the array, I do this:
NSMutableArray *tempUnsortedArray = [[NSMutableArray alloc] initWithArray:_timesArray];
[tempUnsortedArray addObject:_datePicker.date];
self.timesArray = tempUnsortedArray;
I'm not sure if the way I'm adding data the array is causing the issue or not.
You're getting a doesNotRecognizeSelector: exception. This probably means that the object you think is a NSMutableArray is not really one. It is probably an NSArray. Where are you assigning the object?
To start debugging the issue, po the object before calling removeAllObjects. What type of object is it reported as?
Otherwise it could be possible that there is a non NSObject element in timesArray.
#property (nonatomic, strong) NSMutableArray *timesArray;
if ([_timesArray count] > 0)
It seems, you syntesize your property like this:
#syntesize timesArray = _timesArray;
You chacking count of _timesArray, but removing object from timesArray. I never set new name for my properties and don't sure how it works, but I dont like it.

Problem with NSMutableArray

I declared a NSMutable array and assigned some values to it.
.h
NSMutableArray *imageDetailsFromCategory;
#property (nonatomic, retain) NSMutableArray *imageDetailsFromCategory;
.m
#synthesise imageDetailsFromCategory
in ViewDidLoad:
imageDetailsFromCategory = [[NSMutableArray alloc]init];
//assigning object to Array..working fine.showing two images.
imageDetailsFromCategory = [self getImageDetailsFromCategory:generatedString];
Now my app is loading... I am doing some UI changes with the array. However I want to pass this array on another button click to another class. But when click event is triggered the array shows 0x76779e0"{(int)$VAR Count} like this in the same class I declared the arry. I can't get the array count after the button click.
Can any one tell me how can I access my array. What is the problem?
The method [self getImageDetailsFromCategory:generatedString]; I think returns a autoreleased array. Try using the proper setter for retaining it, like
self.imageDetailsFromCategory = [self getImageDetailsFromCategory:generatedString];
You are overriding your imageDetailsFromCategory variable that you alloc'd in the first line with your second line.
So imageDetailsFromCategory = [[NSMutableArray alloc] init] creates a mutable array… but imageDetailsFromCategory = [self getImageDetailsFromCategory:generatedString]; replaces the previously alloced mutable array with a brand new object.
THat's as if you did int i=5; then i = [self someMethod];: the value 5 would be lost.

Sharing an array between 2 classes

I currently have two arrays, 1 in each class, but I am cloning them before displaying the other viewController. So whatever happens e.g. delete an item in one viewController, I clone the array for the other ViewController when it needs it.
What is the best way to make these ViewControllers read and write to the same array? I would like a shared array resource which I can access, modify from the 2 viewControllers, possibly a third, whenever necessary.
How is this done without cloning all the time.
If the array is shared only by the two view controllers, just let them point to the same object.
#interface FirstViewController {
//...
NSMutableArray *arrayData;
}
#property (nonatomic, retain) NSMutableArray *arrayData;
#end
#interface SecondViewController {
//...
NSMutableArray *arrayData;
}
#property (nonatomic, retain) NSMutableArray *arrayData;
#end
And somewhere in the code
NSMutableArray *array = [[NSMutableArray alloc] init];
firstViewController.arrayData = array;
secondViewController.arrayData = array;
[array release];
This array conceptually becomes a Model for your design. If the two view controllers perform the same tasks on the data of the array that can be abstracted, consider having a custom class that contains the array and serves as the model class.
You can create a singleton class which holds the array, then access it through the sharedInstance. I recommend this article for a review of the various approaches to this situation.
This is probably a good use of the Model-View-Controller paradigm. Break out the array into the model that both view controllers can access.

how can I display MSMutableArray data in UITableViewController?

I am getting data from NSTextField, and saving data in NSMutableArray. I want to show the same data in another class which in child of UITableViewController.
How can I show data of NSMutableArray to myTableView which is another class??
Help me out, please
Surely, I will appraise if I found proper way.
Your tableView is in a MyViewController class. You need to create a NSMutableArray *sourceArray attribute on that class, as well as the associated property, using for instance:
#property (nonatomic, retain) NSMutableArray *sourceArray;
Then when you instantiate this View Controller or whenever you make it appear, assign the results to sourceArray :
MyViewController *mvc = [[MyViewController alloc] initWith...];
mvc.sourceArray = theResult;
[self presentModalViewController:mvc animated:YES];
[mvc release];
Then use the sourceArray as the Table View datasource.
Make a property of NSMutableArray in app delegate class and assign the result of the source array to it as soon as you fetch any result, because you can access its instance all over your project.