passing array from one view to another - iphone

i am a new iphone programmer i want to create an array that keeps adding a value at its last position when i moving from one view to another
because at last view ,i need that full array which has all values from all views....
i tried taking an array and sending it view to next view (array is defined in all views) but when i tried to put value to it

You can use NSMutableArray class with addObject: in order to add your views into array. Have a single view controller, declare that array and all your views can access the view controller's array.

try to save it to a class (singelton class) it will work....it will surely work if you transfer it to the delegate class....make an NSMutableArray object there and initialize it once in applicationDidFinishLaunching method and then make object of delegate class and transfer your values to it by addObject method.....

The best way is initialize your mutablearray in appdelegate and use the same using the object of delegate in each class wherever you want to use.you can add the object using addobject and can access using property objectAtIndex anywhere.

Related

Swift: dynamically creating and assigning delegates

MKMapView's delegate property is defined as so in swift:
#IBOutlet weak open var delegate: MKMapViewDelegate?.
If I had to dynamically create map views for each item in an array whose length/count is unknown beforehand, how I dynamically assign a unique delegate instance to each one of the maps without keeping a class variable for each delegate (since we don't know the count of items in the array). Is there something similar to Obj-C's __Strong ?
Update:
I could have a class array to which I add each delegate, but still, are there any other approaches, which are more inline for example by using __Strong, if there is such a thing in Swift
how I dynamically assign a unique delegate instance to each one of the maps without keeping a class variable for each delegate
There's no need for a unique delegate object for each map view. Delegate methods typically get a reference to the delegator as their first parameter, so the delegate can easily tell which map view is sending the message.

What would be the appropriate way to use a delegate protocol from 2 views?

I have a view controller, which uses a delegate protocol in order to pass back an array of strings. I now have another view controller, which I'd like to use the same protocol, but I've I use it I get a warning in Xcode Duplicate protocol definition of 'SearchDetailsDelegate' is ignored.
I need these two views to pass back an array for the parent view controller to parse. What would be a more appropriate way of achieve what I need to do here? Would key value observing be the way to go here?
You have few options:
rename your protocols to be different.
create an external protocol and adopt that protocol on each view
Add a property to your view called ParentController with a type of it's parent.
#property (strong,nonatomic) ParentViewController *ParentController;
(synthesise that off course)
Then, in your viewController, when you instantiate the view assign the viewController as the parent
YourView *childView = [[YourView alloc]init];
childView.parentController = self;
Now you can add a method in your viewController that can receive the strings array
-(void)setStringsArray:(NSArray*)arr{
//do what ever you need with the array
//don't forget to add this method to your .h file so it will be visible
}
Lastly send the strings array from the view:
[self.parentController setStringsArray:yourArray];
BTW
if you want to know what view send the array you can:
-(void)setStringsArray:(NSArray*)arr fromView:(UIView*)senderView{
//do what ever you need with the array
//don't forget to add this method to your .h file so it will be visible
}
and use
[self.parentController setStringsArray:yourArray fromView:self];
BTW 2
an other option will be to use notifications.
Define the protocol in a separate .h file (new file of objective c protocol) and then include it in the required view controllers.Redefining the same protocol in two different view controllers is not recommended as it has been in your case

UITableView reloadData

I have a UIViewController that contains a UITableView.
I'm trying to fill this table with data parsed from an XML file.
The first time the TableView loads the data isn't available just yet.
What would be the best way to reload this view?
I tried to do a self.TableView reloadData after the parsing finished.
Unfortunately the NSMutableArray holding the parse result getting reset during reloadData.
The array is set at the UIVivewController.
Should I set the NSMutablearray as a global variable?
THanks a bunch
The array should be an ivar (instance variable) of the view controller containing the UITableView. When you are done parsing the ivar should be populated with the data and you should be able to populate the UITableView with no problem using [self.TableView reloadData]. Make sure you are not removing all objects or re-initializing the array in any of the delegate or datasource callbacks.
Don't make it global, make it an instance variable. As in:
#interface MyViewController : UIViewController {
NSMutableArray* parsedDataArray; //declare the array here
}
#end
But beyond that, yes, calling reloadData after the parsing finishes is reasonable. Just fix the code to not reset the parse results when reloadData is called.
Yes, take that NSMutableArray as a global for that controller file.
Be sure u are getting value in NSMutableArray in the controller which holds table view.
simply reload table.
Regards,
Shyam

passing array from one viewcontroller to another

i have a custom tabBar class in which i switch three view controllers ,i am removing the presentview controller and presenting the other .
custom tabbar class
-list
-inbox
-messages
now i have to pass an array from list to inbox
i usually create an instance of the recieving class likeInbox *inbox=[[Inbox alloc]init];
inbox.array=self.array;
but in this case its not working.the array in inbox class returns null when i nslog it
Do you inherit or use the UITabBarController in your custom tabBar?
If you want to pass a variable between list<->inbox<->messages I would suggest that you implement a method in the tabBar that can be called from the subviews. The method would send the array to the appropriate subview.
Check your property in Inbox class and check to see if self.array is not null.
Edit try: inbox.array = [NSArray arrayWithArray:self.array]
ok this worked when i passed the value to applicationdelegate and from the other class accessed from here.

Setting ivar in objective-c from child view in the iPhone

Maybe a FAQ at this website.
I have a TableViewController that holds a form. In that form I have two fields (each in it's own cell): one to select who paid (single selection), and another to select people expense is paid for (multiple selection).
Both fields open a new TableViewController included in an UINavigationController.
Single select field (Paid By) holds an object Membership
Multiple select field (Paid For) holds an object NSMutableArray
Both vars are being sent to the new controller identically the same way:
mySingleSelectController.crSelectedMember = self.crPaidByMember;
myMultipleSelectController.crSelectedMembers = self.crSelectedMembers;
From Paid for controller I use didSelectAtIndexPath method to set a mutable array of Memberships for whom is paid:
if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark) {
[self.crSelectedMembers removeObject:[self.crGroupMembers objectAtIndex:indexPath.row]];
//...
}
else {
[self.crSelectedMembers addObject:[self.crGroupMembers objectAtIndex:indexPath.row]];
//...
}
So far everything goes well. An mutable array (crSelectedMembers) is perfectly set from child view.
But...
I have trouble setting Membership object.
From Paid By controller I use didSelectAtIndexPath to set Membership:
[self setCrSelectedMember:[crGroupMembers objectAtIndex:indexPath.row]];
By NSlogging crSelectedMember I get the right selected member in self, but in parent view, to which ivar is pointed, nothing is changed.
Am I doing something wrong? Cause I CAN call the method of crSelectedMembers, but I can't change the value of crSelectedMember.
If I understand your question, the most likely cause is an improper property declaration.
If you want to pass values from one object to another using each objects properties, then you need to make sure to use assign to ensure the properties in one object are pointing at the same instances as the property in the other object.
So in your topViewController you have a property:
#property (nonatomic,retain) NSString crSelectedMember;
Then in your child view controllers you have:
#property (nonatomic,assign) NSString crSelectedMember;
This forces the value into the exact object in the parent controller.
However, this is a very fragile way to pass data between viewControllers. As your app becomes more complicated, it will be impossible to track all the passed data. (Worse, if you run into memory limitations, the parent view controller may unload and not even exist when you try to pass data to it.)
Instead, you should have a single custom object devoted to holding your data. Each view controller should query that object for the data it needs and should write any changes back to that object. The view controllers never communicate directly. This technique allows you to control the data in one specific location instead of spreading it out all over your code and it scales well. You can add an arbitrary number of view controllers to you app without having to worry about tying them all together.
See this post for details: iPhone: How to Pass Data Between Several Viewcontrollers in a Tabbar App