I have a tableview and a detailview and I can pass data from my tableview to the detail view by including the detail view.h and setting some values from distance with initWithNib.
DetailView *detailView = [[DetailView alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
//passing data gekozenSpel
detailView.gekozenSpel = [tableData objectAtIndex:indexPath.row];
But now I have a data table with multiple entries and one of then is set, that is: can be changed, in the detailview, from 0 to 1 or from 1 to 0, as a string. It works all fine but now I want to apply the change backwards to the table view and that doesn't work.
myTableView *myTableview = [[myTableView alloc] initWithNibName:#"myTableView" bundle:[NSBundle mainBundle]];
[myTableView.theTable replaceObjectAtIndex:location withObject:value];
For if you use an initWithNib way-of-doing then you create a new empty table nsmutablearray. But I want to CHANGE a value in that array at a specific location with a specific content.
Maybe a singleton to access the data from everywhere? I tried but I have to create instances of what I declare as value and then it is private and not public. So I don't understand how you apply a singleton then.
Any help would be appreciated,
Jan
You will need to add a delegate to the DetailView, this delegate will pass a new array to the old view, this array will have the new tableData that you will replece
For more information on the delegate, here are some tutorials
http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/
How to use custom delegates in Objective-C
Simple Delegate Example?
I replace in the detail view the property (eigenschap) of the heart symbol (favourite: YES or NO), then that whole array is put over the mother array (dataVoorMijnTabel) to have it updated back into the main table view (mijnTabelView).
[eigenschappendata2 replaceObjectAtIndex:locatiehartje withObject:eigenschaphartje];
mijnTabelView *mtb = [[mijnTabelView alloc] initWithNibName:#"mijnTabelView" bundle:[NSBundle mainBundle]];
mtb.dataVoorMijnTabel = eigenschappendata2;
Related
This question already has answers here:
What's the best way to communicate between view controllers?
(4 answers)
Closed 8 years ago.
In my TableViewController.m I can easily get the selected row by doing [tableView indexPathForSelectedRow] but I want to get this value from DIFFERENT view. So when my TableViewController goes to my DetailViewController. I want my DetailViewController to have the data of the selected cell.
In a proper architecture, a Controller is designed so that it acts as a black box to other Controllers. In your example, it's not a very good practice that the Details controller searches for the TableViewController's selected item. Instead your TableViewController should use the DetailViewController as a 'service' and somehow pass the selected object as an argument, or set it as a property of the 'service provider' (DetailController).
For example
// this is still the TavleViewController's code
id selectedObject = // ... get selected object somehow... indexPathForSelectedRow or whatever
DetailViewController *newView = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
[newView showDetails:selectedObject];
Here, the function showDetails is just an example, it's a function you implement to pass the seleted object as argument.
EDIT:
How you do it depends on your model. Supose each object in your model represents a person. Your MainTableView has a list of names and the detailView shows the details about the selected Person. In such a case, which is typcial, you would have a class that represents Person, and the list of persons would be somewhere in an array. So...
NSIndexPath *i = [tableView indexPathForSelectedRow];
Person *selectedPerson = [self.myArrayOfPersons objectAtIndex:i.row];
// here you instantiate or show the details view
[detailsView showDetails:selectedPerson]; // this is one option, you could also use a property
// for example
detailsView.selectedPerson = selectedPerson; // this is an alternative to the showDetails,
In the DetailsViewController you could have something like this
- (void)showDetails:(Person *)person
{
// just fill your controls witht the person's information: age, birth place, address...
[self.someTextView setText:person.name]
}
I want to be able to dynamically create ViewControllers based on a JSON file. What I mean is, there will be a json that will dictate how many ViewControllers the user needs. I.e say I have a json file that lays out 5 ViewControllers, I want to be able to dynamically create these ViewControllers and be able to transition between them.
So what I am going to have is JSON file, that sets out the ViewControllers, say 3 for this example. This JSON file has info on the text, buttons etc and how to navigate between them.
So I want to be able to loop through this JSON, and create the necessary view controllers and add the required text, buttons etc. The JSON will also dictate how the view controllers link up together.
I know how to create one VC and add info like this (This is just quick example, just created vc and added label.
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor whiteColor];
UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(220, 50, 130, 80)];
testLabel.backgroundColor = [UIColor clearColor];
testLabel.textColor = [UIColor blackColor];
testLabel.text = #"Hello";;
[vc.view addSubview:testLabel ];
[self.navigationController pushViewController:vc animated:YES];
I don't know how to create several differently named ViewControllers in a loop using JSON. Anyone have any ideas on how to do this? Or is something like this even possible?
Any assistance would be greatly appreciated.
EDIT:
Very basic example of what JSON will look like
{
"ViewControllers":[
{
"name":"FirstVC",
"id":1
},
{
"name":"SecondVC",
"id":2
},
{
"name":"ThirdVC",
"id":3
}
]
}
So first VC links to secondVC and second to thirdVC
Just create an array and hold them there. Something like this:
NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithCapacity:0];
// ...
// Inside a loop
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor whiteColor];
UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(220, 50, 130, 80)];
testLabel.backgroundColor = [UIColor clearColor];
testLabel.textColor = [UIColor blackColor];
testLabel.text = #"Hello";
[viewControllers addObject:vc];
// Release vc and label if you're not using ARC
Now, if you want to name your controllers, one idea would be to create a subclass of UIViewController and add a name (or something like that) property. Then you just set this property also inside your loop and you can refer/filter based on that property.
You won't explicitly name them as separate variables, but rather you may have an NSArray of different UIViewController instances. As you read through your JSON file, you can loop over the information presented, creating a view controller and adding it to the array each time the JSON tells you to. When you're done, you can pull view controllers out of the array as your user navigates back and forth.
What does your JSON look like? Post an example and we may be able to provide more info.
Why your ViewControllers needs different names? You should simply create instance of one viewController class.
For example if you have a 3 "screens" from JSON:
home
second
about
And all of them have different buttons, text etc. And you have a class name MyViewController. This class may have a #property name, #property buttons (probably NSArray with object from your button management class) etc if you want to distinguish your screens.
Next you should store your objects (MyViewController class objects) in NSArray.
So what you want to do when app starts:
You created an object of MyViewController class and display it. If user want to go to second screen you should simply create another instance of the same class.
So if you want to display name of the screen you have a self.name value in your ViewController class.
I have this code (XCode 4, using Storyboards with ARC) which takes data from an array (rArray) and places it in the textfields from which it originally came from (I want to edit the data). The array (rArray) has valid data in it, but nothing is in the textfields. What am I doing wrong?
SingletonListOfReadings *rShareInstance = [SingletonListOfReadings sharedInstance];
rArray *ra = [rShareInstance.listOfReadings objectAtIndex: indexPath.row]; // get an rArray object out of listOfReadings
// place data back into textfields
EnterDataViewController *edvc = [[EnterDataViewController alloc] init];
edvc.txtSTA1.text = ra.rSTA;
edvc.txtBS.text = ra.rBS;
edvc.txtFS.text = ra.rFS;
edvc.txtDesc.text = ra.rDesc;
[self.navigationController pushViewController:edvc animated:YES];
the UI text elements in VC are not yet set when you try to populate it (it will be set only after [self.navigationController pushViewController:edvc animated:YES]; (and only when it gets viewDidLoad).
easy way to go is to change txtSTA1, txtBS, txtFS etc to strings (make sure they are in edvc .h file), pupulate them as u do now
so, txtSTA1STR is a string in the .H file of edvc
and txtSTA is a UI text element in the xib (or programmatically) in edvc
EnterDataViewController *edvc = [[EnterDataViewController alloc] init];
edvc.txtSTA1STR = ra.rSTA;
edvc.txtBSSTR= ra.rBS;
edvc.txtFSSTR= ra.rFS;
edvc.txtDescSTR= ra.rDesc;
[self.navigationController pushViewController:edvc animated:YES];
and inside edvc
- (void)viewDidLoad
{
txtSTA1.text = txtSTA1STR;
//... etc
}
I fixed it... I took the original code and moved it to the View controller (edvc) which handles the objects. Then I called it from the view controller where I needed the data.
Works fine... I think the problem was in the addressing of the receiving textfields.
Thank you for your help... I appreciate it.
As you see in the tile I try to share a variable between two views (the id of the selected object).
How can I do?
Here is my code for put on my other view :
EditProd *edit = [[EditProd alloc] initWithNibName:#"EditProd" bundle:[NSBundle mainBundle]];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:edit animated:YES];
[edit release];
And which code I need in my other view to get this variable?
EditProd should have a public property e.g.
#property bool myBooleanVariable;
Then just change it from view1 e.g.
EditProd *edit = [[EditProd alloc] initWithNibName:#"EditProd" bundle:[NSBundle mainBundle]];
edit.myBooleanVariable = false;
[self.navigationController pushViewController:edit animated:YES];
[edit release];
Declare a property on your second view controller (EditProd), and then set that property before calling pushViewController.
You can do it three different ways:
add a property to the new view and write to it from the first view
add protocol with method, that can take this variable, set the delegate to the first view, access from new view
save variable into some object, which is accessible from both views
I have a tableview whose columns are bound to an array controller. I'm programmatically adding items to the array:
Person is just a custom class;
personsPresent is in another class called Meeting, it's a MutableArray which contains Person objects.
Person *newPerson = [[Person alloc]init];
[[[self meeting] personsPresent] addObject:newPerson];
[[self tableView] reloadData];
This works, but the values don't show up into my table view until I sort the columns. I thought reloadData would do it for me.
In my xib, my nsarraycontroller's objectcotnroller settings is set to Class and Person. It's controller content it's bound to File Owner and it's model key path is meeting.personsPresent.
Any help would be appreciated.
Mark
You're mutating the array behind the array controller's back. Either ask the array controller to -rearrangeObjects (stinky) or use its -addObject: method (better).