Reputation: 97
I have 2 questions about this storyboard test project. I am new to Xcode, and I can't find solution to this problem.
View Controller A is Root View Controller. View B and View C extend from it separetely. Table View extends from View C. View B has 2 buttons - To Table View and to View B. (I forgot to add textField on View B, and segue from B to A)
1) Button To Table View pushes Table View. Table Cell contains a string. Pressing the cell, view goes back to View Controller C and that string is displayed in text Field.
problem 1: if I use modal segue from table view to view C textField will be updated with method prepareForSegue, but I will lose Navigation Bar on view C.
problem 2: if I use push segue from table View to View C, back button on view C will not point to view A, it will point to table View.
Solution? Click on the text field to select it, then press back button to update view C. But how to implement?
Or is there better solution?
2) I want to press button on View C to go to view B. If I use push segue, View B Back button will point to view C. If I use modal push, view B loses Navigation bar.
Can I somehow make push segue to View B, that it's back button points to view A?
[EDIT]
to pass data between view Controllers, I used NSUserDefaults. Turns out that it is very efficient way of passing simple data (strings, numbers, etc) between Views
hot to use it for MODAL segue? in desired tableViewController add in the
-(void)tableview:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//define user defaults
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
NSString *testString = @"test";
[defaults setObject:testString] forKey:@"test1"];
[defaults synchronize];
//dismiss ViewController
[self dismissViewControllerAnimated:YES completion:nil]; }
if you wish to use push segue to dismiss view controller and skip navigation sequence (drop the view from sequence) replace
[self dismissViewControllerAnimated:YES completion:nil];
with
[self.navigationController popViewControllerAnimated:YES];
Upvotes: 0
Views: 2916
Reputation: 113
I will update this answer as we get through each problem.
If you are using the table view as a means to make a selection then you should instead Dismiss the table view controller instead of pushing a new view modally.
//In the table view controller's on cell selected method
[self dismissViewControllerAnimated:YES completion:^{
//code
}];
Pushing views will result in the the next view being placed on top if you want to get back to view A from view C (Provided you presented it modally) you can use the same code above to get back from C to A
Basically in this case:
View A --modal-> View C --modal-> TableViewController
Then you have to call the above code once in TableViewController and once again in ViewController C and bam back to A
As far as having B point back to A it's easier if you go from A to B However if you never really need to go from A to B I suggest changing up the structure.
But to fulfill your request one method of switching your view hierarchy is by dismissing view C while at the same time pushing B on top of A.
//Inside View C If you want it to happen only after the first animation completes just move the other line up into the block
[self dismissViewControllerAnimated:YES completion:^{
//code
}];
[self.parentViewController.navigationController pushViewController:<#(UIViewController *)#> animated:YES];
I hope this is helpful.
Upvotes: 0