iOS Passing NSMutableArray between views and load in a table view - objective-c

Well, I have an NSMutableArray in a view and I want to pass the value of this array to another view. But I do not want to use the method [self presentModel view controller ...] because I just want to display another view when I press another button that I created. The problem is, when I press the button to display the value of the array, the array loses its value, returning empty, but if I use the method [self presentModel view controller ...] it returns the value correctly.
CurrentView:
#property and #synthesise AnotherViewController *superAnother
AnotherViewController *anView = [[AnotherViewController alloc]initWithNibName:nil bundle:nil];
superAnother.arrayOfTheAnotherView = [[NSMutableArray alloc]initWithArray:arrayOfTheCurrentView];
The Another View:
#property and #synthesise NMutableArray *arrayOfTheAnotherView;
NSMultableArray array = [[NSMultableArray alloc]initWithArray:arrayOfTheAnotherView];
Loading a table view with the array:
cell.textLabel.text = [array objectAtIndex:indexPath.row];

arrayWithArray does not create other instances of the collected objects, but another array instance using the same references. If you want to change the datasource objects in the popover or whenever else, you'll want to copy the items with arrayWithArray:copyItems:, of course all the contained objects must conform NSCopying protocol to let them to be copied.

Another way of passing an NSMutableArray between view controllers is something along these lines:
In interface builder add an IBAction to the button in the first view controller
Add a storyboard segue between the two viewControllers (not from one button to the next view controller but directly between viewControllers
Give this segue a unique identifier
In the IBAction from the button in the first view controller add the line
[self performSegueWithIdentifier:#"myIdentifier"];
Then in the first view controller you can implement the delegate method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"myIdentifier"]) {
SecondViewController *con = [segue destinationViewController];
con.array = self.array;
}
}

There are some problems with this code:
You define your superAnother property but you're not assigning anything to it. Your code should be like this:
self.superAnother = [[AnotherViewController alloc] initWithNibName:nil bundle:nil];
self.superAnother.arrayOfTheAnotherView = [[NSMutableArray alloc] initWithArray:arrayOfTheCurrentView];
Then in your AnotherViewController.m:
cell.textLabel.text = [self.arrayOfTheAnotherView objectAtIndex:indexPath.row];
PS. Of course don't forget to release them if you're not using ARC

Related

add/hide UITabBarItem

I have a UITabBarController and would like to add a tab, with the ability to make it disappear. I added 2 tabs with XCode. You can add a third tab by program?
I know a command like:
[self setViewControllers: [NSArray arrayWithObjects: x1, nil] animated: NO];
You can retrieve the array that uses XCode to add a third view?
thanks
I can not load the view with the code. I created a view controller from the storyboard, when I try to load it from code I get a black screen, use this code:
ViewControllerA *x1 = [[ViewControllerA alloc] init];
[self setViewControllers:[NSArray arrayWithObjects: x1, nil] animated:NO];
Yes, if you use [UITabViewController setViewControllers: animated:] you can add in an array containing the two previous view controllers plus the new third one.
For example, you'd probably want to do it something like this:
// assuming you've set an IBOutlet to your tab bar controller
NSArray * currentSetOfViewControllers = [appTabBarController viewControllers];
if((currentSetOfViewControllers == NULL) || [currentSetOfViewControllers count] == 0))
{
NSLog( #"I don't see any view controllers; is my tab bar controller outlet set properly?")
} else {
NSMutableArray newSetOfViewControllers = [[NSMutableArray alloc] initWithArray: currentSetOfViewControllers];
if(newSetOfViewControllers)
{
ViewControllerA *x1 = [[ViewControllerA alloc] init];
if(x1)
{
[newSetOfViewControllers addObject: x1];
[appTabBarController setViewControllers: newSetOfViewControllers];
// release our alloc'd mutable array only if you do not have ARC turned on
[newSetOfViewControllers release];
}
}
}
You'd probably also want to give your new view controller an associated tab bar item with a title and an image. Check out [UITabBarItem initWithTitle: image: tag:].
I've linked the Apple documentation for you which I hope would help!
create a custom tab bar in delegate so create a view just like a tab bar and set 1 image view on view and 3 button (1 button set hidden) add this view in tab bar controller after that when u require 3rd button enable third button

update a UILabel when the cell in UITableView is selected

A really simple question here. I have a label on one view and a UITableView on the previous view. I have got a segue triggered when the user selects the row and I want the label to be updated with the text from that row. Here's one example, the code is pretty obvious.
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *countrySelection;
switch (indexPath.section) {
case kFirstSection:
countrySelection = [[NSString alloc]
initWithFormat:#"The country you have chosen is %#",
[self.MyCountries objectAtIndex: indexPath.row]];
[self performSegueWithIdentifier:#"doneResults" sender:self];
self.countryResult.text = countrySelection;
break;
The label isn't updated and I just don't know what should be done.
Thanks in advance!
These kind of things really need to be set on the View Controller that owns them. Use a public property to pass the value of the selected country to that view controller as outlined below:
First, create a property called something like:
#property(non atomic,strong) NSString *countryChosen;
in the destination View Controller, and make sure to #synthesize it
No reason to create another property for the IndexPath. Just use
// Pass along the indexPath to the segue prepareForSegue method, since sender can be any object
[self performSegueWithIdentifier:#"doneResults" sender:indexPath];
in the didSelectRowAtIndexPath method.
Then in the prepareForSegueMethod:
MyDestinationViewController *mdvc = segue.destinationViewController;
NSIndexPath *indexPath = (NSIndexPath *)sender;
mdvc.countryChosen = [self.MyCountries objectAtIndex: indexPath.row]];
On the viewDidLoad event of the Destination VC, just use:
self.countryResult.text = countryChosen;
* EDIT *
To deal with a datasource that has multiple sections, just use the same logic that you have in the cellForRowAtIndexPath.
NSDictionary *selRow = [[self.countriesIndexArray valueForKey:[[[self.countriesIndexArray allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:sindexPath.row];
Change this to suit your needs, but basically you are implementing the same logic that you would to display a cell, except you are specifying the indexPath (both section and row) that you want.
Then something like the following to set that property on the destination VC:
self.countryResult.text = [selRow valueForKey#"Country"];
In your current view controller create a new property for the indexPath of the cell the user selected, like this:
#property(strong,nonatomic) NSIndexPath *path;
#synthesize it and then when a user selects a row, set it by using
self.path = indexPath;
When you perform a segue, it will always call
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
So what you can do now when prepareForSegue: gets called, is the following:
/* if this is not the only segue you are performing you want to check on the identifier first to make sure this is the correct segue */
NSString *countrySelection = [[NSString alloc]
initWithFormat:#"The country you have chosen is %#",
[self.MyCountries objectAtIndex: self.path.row]];
segue.destinationViewController.countryResult.text = countrySelection;
/* after creating the text, set the indexPath to nil again because you don't have to keep it around anymore */
self.path = nil;
For this to work the view controller you want to show after selecting the cell must have a property for the UILabel, on which you are trying to set the text.

Passed array is not passed by popViewControllerAnimated... why?

Dont be put off by the huge question... (its mostly code).
OK, I have a navigation Controller which contains a view controller (Called AddClaim) containing a tableView.
if a cell is selected, this is called:
EditClaimDetails *detailViewController = [[[EditClaimDetails alloc] init] autorelease];
// Pass the selected object to the new view controller.
detailViewController.selectedIndexPath = indexPath;
detailViewController.newClaimArrayDetails2 = newClaimArrayDetails;
[self.navigationController pushViewController:detailViewController animated:YES ];
This works nicely and a new view controller is shown containing a tableView (It is an exclusive list).
In ViewDidLoad of the EditClaimDetails this code exists: (claimTypeHoldingArray is a mutable array declared in the header file)
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(pressedBack)];
self.navigationItem.leftBarButtonItem = backButton;
claimTypeHoldingArray = [[NSMutableArray alloc] initWithArray:newClaimArrayDetails2];
Basically the result of this is as expected: A back button is shown - when pressed - it calls a selector popping the view controller to AddClaim, claimTypeHoldingArray contains the newClaimsArray given in AddClaim.
This is part of the code in didSelectRowAtIndexPath: (claimTypeArray is the array which holds the textLabels of the cells)
[claimTypeHoldingArray replaceObjectAtIndex:0 withObject:[claimTypeArray objectAtIndex:indexPath.row]];
What this does is that the first object of claimTypeHoldingArray is replaced with what text was on the TextLabel of the cell. so far so good. (tested with nslog)
This is the code for when the back button is pressed:
-(IBAction)pressedBack {
AddClaim *sender = [[[AddClaim alloc] init] autorelease];
sender.newClaimArrayDetails = claimTypeHoldingArray;
[self.navigationController popViewControllerAnimated:YES];
This is where the trouble starts...
This action (according to me) should replace newClaimArrayDetails with claimTypeHoldingArray. (it does so) But when the view controller is popped and the screen moves back to add claim this array is not changed!
What have I done wrong?! btw, all properties are set.
this is the test i do in viewDidAppear:
NSLog(#"%#",[newClaimArrayDetails objectAtIndex:0]);
This answer is the same scale as the question, hope its's not too large ;)
So, in your pressedBack button method you're trying to update the initial AddClaim view controller object with the claimTypeHoldingArray.
You're halfway right - you're definitely updating an AddClaim object, just not the one that's inside your navigation controller. you're creating a new one and updating that instead!
-(IBAction)pressedBack {
// This line creates a new AddClaim view controller
AddClaim *sender = [[[AddClaim alloc] init] autorelease];
// This line updates your _new_ AddClaim view controller
sender.newClaimArrayDetails = claimTypeHoldingArray;
// This line displays the _old_ AddClaim object
[self.navigationController popViewControllerAnimated:YES];
You need to pass into your EditClaimDetails view controller the AddClaim view controller that created it.
In your cell is selected method add something like
detailViewController.addClaimViewController = self;
(where addClaimViewCOntroller is a property on your EditClaimDetails object like
#property (nonatomic, retain) Addclaim *addClaimViewController;
Then, your pressedBack method becomes
-(IBAction)pressedBack {
// This line updates your _old_ AddClaim view controller
addClaimViewController.newClaimArrayDetails = claimTypeHoldingArray;
// This line displays the _old_ AddClaim object
[self.navigationController popViewControllerAnimated:YES];
Hope that helps.
Check your array property definition in AddClaim, is it by any chance (nonatomic, copy)? If yes the it would hold a private copy of your array, so that you original array couldn't change.

Passing variables (or similar) to newly loaded view

I am loading new views for a small iphone app, and was wondering how to pass details from one to another?
I am loading a tableview full of data from and xml file, then once clicked a new view is brought in via:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SubInfoViewController *subcontroller = [[SubInfoViewController alloc] initWithNibName:#"SubInfoView" bundle:nil];
[self presentModalViewController:subcontroller animated:YES];
[subcontroller release];
}
Next step would be to tell the newly loaded view which row had just been loaded?
Any idea, thoughts more than welcome, and please be gentle big newbie...
I typically create my own init method to do things like this. I think it would likely be better to pass in the corresponding "model" object represented by the tableView row, rather than the row number itself, like this:
In SubInfoViewController.h
#interface SubInfoViewController : UIViewController {
YourObject *yourObject;
}
#property (nonatomic, retain) YourObject *yourObject;
Then in SubInfoViewController.m:
- (SubInfoViewController*)initWithYourObject:(YourObject*)anObject {
if((self = [super initWithNibName#"SubInfoView" bundle:nil])) {
self.yourObject = anObject;
}
return self;
}
You'd create and present it this way:
// assuming you've got an array storing objects represented
// in the tableView called objectArray
SubInfoViewController *vc = [[SubInfoViewController alloc] initWithYourObject:[objectArray objectAtIndex:indexPath.row]];
[self presentModalViewController:vc animated:YES];
[vc release];
This could be adapted pretty easily to allow you to pass in any type of object or value (such as a row number if you still want to do that).
Add an instance variable to your view controller and declare a property corresponding to it, so after you alloc, init it, set it like subcontroller.foo = Blah Blah.

When "programmatically" creating UINavigationController and UITabBarController, how do I address their functions (like viewWillAppear?)

I am creating my Nav and TabBar in code at launch via:
IN : myAppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// set up a local nav controller which we will reuse for each view controller
UINavigationController *localNavigationController;
// create tab bar controller and array to hold the view controllers
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
// setup the first view controller (Root view controller)
RootViewController *myViewController;
myViewController = [[RootViewController alloc] initWithTabBar];
// create the nav controller and add the root view controller as its first view
localNavigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
// add the new nav controller (with the root view controller inside it)
// to the array of controllers
[localControllersArray addObject:localNavigationController];
// release since we are done with this for now
[localNavigationController release];
[myViewController release];
// setup the first view controller just like the first
ResortsListViewController *resortsListViewController;
resortsListViewController = [[ResortsListViewController alloc] initWithNibName:#"ResortsListView" bundle:nil];
resortsListViewController.title = #"Category1";
resortsListViewController.tabBarItem.image = [UIImage imageNamed:#"image1.png"];
resortsListViewController.navigationItem.title=#"Category1";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
// setup the second view controller just like the first
ResortsListViewController *resortsListViewController;
resortsListViewController = [[ResortsListViewController alloc] initWithNibName:#"ResortsListView" bundle:nil];
resortsListViewController.title = #"Category2";
resortsListViewController.tabBarItem.image = [UIImage imageNamed:#"image2.png"];
resortsListViewController.navigationItem.title=#"Category2";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
// setup the third view controller just like the first
ResortsListViewController *resortsListViewController;
resortsListViewController = [[ResortsListViewController alloc] initWithNibName:#"ResortsListView" bundle:nil];
resortsListViewController.title = #"Category3";
resortsListViewController.tabBarItem.image = [UIImage imageNamed:#"image3.png"];
resortsListViewController.navigationItem.title=#"Category3";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[resortsListViewController release];
// load up our tab bar controller with the view controllers
tabBarController.viewControllers = localControllersArray;
// release the array because the tab bar controller now has it
[localControllersArray release];
// add the tabBarController as a subview in the window
[window addSubview:tabBarController.view];
// need this last line to display the window (and tab bar controller)
[window makeKeyAndVisible];
}
As you see, I am re-using ResortsListViewController for different category displays (resorts with Beaches, resorts with Pools, resorts with espresso bars) ... now, without harassing me (grin) about the silliness of my categories (cos this is a test app) I need need to do several things:
I need to be able to know which tab click caused the ResortsListViewController to be displayed. I was hoping to use TAG but "initWithRootViewController" does not have the "tag" control. So, if i use an imagefilename that is the category name, I can use that filename to distinguish categories...or even navigationItem name. I need to know if there is a way for ResortsListViewController to know which tabbar item click caused it's display. I thought to look for a "action" that I could assign to the tabbar item, but that is not the way tabbarcontroller works.
When clicking from one tab to another, the view does indeed change, the title of ResortsListViewController changes, etc...but the TABLEVIEW it holds does not clear and display any new data. Searching the web I have found a possible solution:
http://discussions.apple.com/thread.jspa?threadID=1529769&tstart=0
basically saying:
In order for UINavigationControllers
to send
"viewWill/Did/Appear/Disappear"
messages, it needs to have received
"viewWill/Did/Appear/Disappear" from
its container.
What is the container for my UINavigationControllers in this situation? myAppDelegate is defined in the .h file as:
NSObject <UIApplicationDelegate, CLLocationManagerDelegate>
and does not have a:
- (void)viewWillAppear:(BOOL)animated {
}
section. When I add one it says "NSObject may not respond to -viewWillAppear" in the debugger.
Any help out there?
1) This is fairly simple. You need to set the delegate property for the UITabBarController. You would set this to the controller object that owns your tabbarcontroller (or your app delegate if that is your setup). Whichever object is set as the delegate would then receive:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
AND
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
to manage tab selections. Place one of these methods in the implementation of whatever object you set as the delegate (you would want it to be the object that owns all of the controllers).
2) If you have a UIViewController that appears as a tab selection (such as ResortsListViewController) then you would need to put the viewWillAppear method in the controller implementation yourself:
#implementation ResortsListViewController
- (id)initWithNibName:(NSString *)name bundle:(NSBundle *)bundle {
...
}
... etc. ....
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[table reloadData];
}
...
#end
Let me know if I misunderstood the second part of your question.
Okay, here goes: This is the correct answer to the question, however, it did not end up being this hard. All I had to do was the following:
Create a property in ResortViewController of type int with variable name whichChoice (for instance). Then address it in the setup of the TabBarController a la:
// setup the first view controller just like the first
ResortsListViewController *resortsListViewController;
resortsListViewController = [[ResortsListViewController alloc] initWithNibName:#"ResortsListView" bundle:nil];
// START HERE IS THE CHANGE
resortsListViewController.whichChoice = 1;
// END HERE IS THE CHANGE
resortsListViewController.title = #"Category1";
resortsListViewController.tabBarItem.image = [UIImage imageNamed:#"image1.png"];
resortsListViewController.navigationItem.title=#"Category1";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
To find out which tab was clicked when my resortsListViewController takes over, I simply query the class variable: whichChoice to get the answer.
Things like this are so simple you skip over them. I thought you had to pass the variable in an action and specify where it would go with the target like you do in other objects -- but when you set things up ahead of time you do not have to do that. Now, having said that, dynamically setting "whichChoice" is not so easy without a lot more thought...but setting it to a variable KNOWN at setup is fine.
For answer #2, I simply put a variable style of IBOutlet in my class, hooked it up to the table, and then followed your instructions, because without the variable in the IBOutlet and hooking it up to the table, there is no reference to the table view. For some reason hooking up the table simply to the VIEW "Referencing Outlet" and calling [self.tableview reloadData] did not do the job.
But for the most part, your answers were right and led me in the right direction. As an aside, I really hate that you have to right-click and drag from here-to-there in IB if you have an IB element you built. You should be able to hook it up in code using it's ObjectId (or something). That would be more in line with programmers. I know IB is made to allow programming to be easier for designers, but gee wilikers..it is hard to wrap my mind around! I end up dropping IB and creating elements in code most of time time...which I do not know if is as fast to execute. I tend to think not...but have no proof to the contrary.