问题
I have small problem with Core Data. I will skip the code because it's working fine. I just need to know what to do inside prepareForSeague method.
App has 2 Views - ViewControllerA and TableViewControllerB. In TableViewControllerB I've set up table view with custom cell with cell identifier: customCell. I press button ADD and it shows Alert view with 2 rows. Alert View adds 2 Attributes to entity and these attributes are displayed in table cell. Entity is called SOMEentity while Attributes are name (NSString) and value (NSString also).
View controller A has two textFields: TextFieldA and TextFieldB, a button calculate and label called calculateLabel. Calculate button simply adds values from textFields.
Now I want to do this - clicking on cell will display Attribute Value in TextFieldA.
what do I have to do inside
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;
{
if ([[segue identifier] isEqualToString:@"FromViewBToViewA"]) {
}
}
Also, do I have to write something inside viewDidLoad method in ViewControllerA? all strings and labels have nonatomic, strong properties.
Thanks!
回答1:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;
{
if ([[segue identifier] isEqualToString:@"FromViewBToViewA"]) {
ViewControllerA * dest = (ViewControllerA*)segue.destinationViewController;
dest.property1 = @"Some property value";
dest.property2 = @"Some property value";
}
}
Grab the destination controller cast it and pass in the expected values. Then in your destination controller. Make sure you include property1 and property2 as properties in the header of ViewControllerA
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
//Set your text view values with your configured properties from the segue.
}
回答2:
you need to create a property in your destination UIViewController and set the value in the prepareForSegue:sender: method like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NextViewController *nvc = [segue destinationViewController];
[nvc setEntity:@"MyEntity"]; //for example
}
回答3:
Create a property on the second viewController, the one that you want to pass the object to. Then in the prepareForSegue:sender: method set the property on the destinationViewController (that is the one you want to set the object on). Remember to cast it to be of your own subclass. As you don't know in prepareForSegue:sender: method which cell was the one that was tapped, you need to store a reference to the selected object in a property or an ivar. You can get that object in the method 'tableView:didSelectRowAtIndexPath:' using the NSIndexPath.
Next, in the viewWillAppear: method of the viewController that now has that property, update the textField using that property.
来源:https://stackoverflow.com/questions/18801963/pass-core-data-entity-attribute-from-viewcontroller-a-to-b