Different Text Fields Modally Calling Different Table View Controllers - ios

I have a working application which I am just trying to enhance. It's a simple Table View controller which has a plus button; the user clicks that and is modally presented with an Add Entry View Controller which contains a UIDatePicker and 4 text fields.
Right now, the text fields behave normally; you click on them and the keyboard comes up.
I am enhancing my application though to be so that a user taps TextField 1 and is taken modally to a new TableViewController, where they'd essentially be able to Create new entries or select existing ones.
If the user clicks textField 2, they'd go modally to the Table View Controller for that text field (which is different to the first Table View).
How would I go about doing this?
Right now, I have added in a modal segue in the story board for the first text field and when clicking that text field in the running application, the keyboard appears. However, as soon as I dismiss the keyboard, the new table view controller gets modally presented. I want to eliminate the keyboard appearing and show just the table view modally.
Attempting the code below meant I could not modally go to any table view or not pull up the keyboard for any text field because it's not distinguishing between text fields.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
Any assistance on differentiating between the text fields to call modally different table views would be really helpful.
Thanks

What i understand is, need to present(push) the UIViewControllers(TableViews) on already the presented(modally) viewController, If so
First you have to present your modal view controller inside a
navigation controller:
EmailIDValidationController *obj = [self.storyboard instantiateViewControllerWithIdentifier:#"EmailIDValidataion"];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:obj];
[self presentViewController:nc animated:YES completion:Nil];
Then inside the presented VIewController, you can do this:
presentedViewController *obj = [self.storyboard instantiateViewControllerWithIdentifier:#"STORYBARDID"];
[self.navigationController pushViewController:obj animated:YES];
You need to get the tableViewControllers by the UITextField events. So Use the delegates of it;
-(void)textFieldDidBeginEditing:(UITextField *)textField{
presentedViewController *obj = [self.storyboard instantiateViewControllerWithIdentifier:#"STORYBARDID"];
[self.navigationController pushViewController:obj animated:YES];
}

A simple fix.
Creating the segue in the Storyboard (as Modal) and applying textFieldDidBeginEditing instead of shouldBegin, I was able to set the tag and hide the keyboard. This did the trick by calling the new VC.

Related

Some code not executing in a method called from another ViewController

My problem
I have a standard UIViewController. With the press of a button, it loads a form sheet modal view controller. When dismissing this modal view with the press of a UIBarButtonItem I call a method by doing:
ViewController *main = [[ViewController alloc] initWithNibName:nil bundle:nil];
[main updateLabel];
In the method -(void)updateLabel in the main ViewController I'm setting the text of a label, but the label won't change. But I know the function gets called, because if I do a NSLog(#"Method call test); instead of label.text = #"Test" I can see the message in console.
What am I doing wrong? It must be the way I'm calling the method in the main ViewController, because I can easily change the label anywhere else.
What I want to do:
When dismissing a modal view controller, I want a method to be called in the main view controller, and in this case change the text of a label.
Thanks for your help!
You're creating a new instance of ViewController with that code, not getting a pointer to the one you already have.
If ViewController is the controller that presented the modal view, then you can get a pointer to it with,
ViewController *main = self.presentingViewController;
A better way to do this would be to use the delegate pattern.
https://developer.apple.com/library/ios/documentation/general/conceptual/DevPedia-CocoaCore/Delegation.html
The following is a design pattern suggestion
The modal view controller shouldn't know how to dismiss itself, that is the responsibility of the presenting view controller. After all, it could have been presented in many different ways (modally, popover, push navigation). Using the delegate pattern, the modal view controller would tell its delegate that it should be dismissed when the bar button item gets pressed. The delegate, which is the presenting view controller, would then dismiss the modal view and update the label mentioned in your question.

UIViewController Not able to redirect

Here is my problem. I had a UITableViewcontroller to which UINavigation controller is Embedded. To UITableviewController I had Add screen and an edit screen. Add screen is working perfectly. When I click on the records on the table view cell it is able to redirect to the edit page (detailed view). When I hit on Submit button the page is not navigating to the tableview cell. Here is the below error.
Warning: Attempt to present on whose view is not in the window hierarchy!
Here is the code I was trying for navigation.
UINavigationController *questionnaireNavController = [self.storyboard instantiateViewControllerWithIdentifier:#"DLProjectsTasksubtasks"];
[questionnaireNavController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
DLProjectsTasksubtasks *qvc = (DLProjectsTasksubtasks *)[questionnaireNavController topViewController];
[qvc.tableView reloadData];
[self presentViewController:questionnaireNavController animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
Between I was using a Segue for transfering the data from tableview data to Edit Screen
I think your segue is being pushed on the navigation controller rather than being presented modally. So you have two options, either change the segue in your storyboard to be modal and use the dismissViewControllerAnimated:YES completion: or keep it the way it is and use popViewControllerAnimated: on your navigation controller.
[self.navigationController popViewControllerAnimated:YES];
For passing the data to the previous page you can either pass a reference of the previous view controller OR accessing it through your navigationController.viewControllers. Index n-2 will be your previous view controller (n is the count of viewControllers).

Having issues displaying modal view programmatically

I am very very new to iOS programming / Objective C.
The flow of events I want to have happen is: user selects tab from tab bar view controller. Once VIEW A has been loaded it will open a modal window to get some information
VIEW A - (void)viewDidLoad
ModalYearPickerViewController *modalYearPickerViewController= [[ModalYearPickerViewController alloc] init];
[self presentViewController:modalYearPickerViewController animated:NO completion:nil];
I am trying to have my year picker view load up right away so the user can select a year from my picker (in VIEW B), then close the modal window after the value has been passed back to VIEW A.
Now, the fist view loads, then goes to a black screen automatically. I am unsure why as my view controller for modalYearPickerViewController has a picker etc on it.
Any tips or help loading a modal view controller programmatically would be greatly appreciated!
Thanks!
If you are using storyboards :
UIStoryboard *storyBoard = [self storyboard];
This will return the storyboard of your current view controller. I am assuming your View Controller A is also on your storyboard.
ModalYearPickerViewController *modalYearPickerViewController = [storyBoard instantiateViewControllerWithIdentifier:#"ModalYearPickerViewController"];
This will instantiate your view controller from the storyboard. But one other thing you have to do is set your view controllers storyboard id to ModalYearPickerViewController. You can set this right below where you set your custom view controller class in the storyboard.
[self presentViewController:modalYearPickerViewController animated:NO completion:nil];
and done.
If you have a xib file for that viewContrioller, it has to be loaded as well, to do that you have to call:
ModalYearPickerViewController *modalYearPickerViewController =
[[ModalYearPickerViewController alloc] initWithNibName:#"ModalYearPickerViewController" bundle:nil];

Present Modal View Controller from inside Popover View

So in my universal app I have a section where a person can look at an existing list of notes from our system (retrieved through a simple web service) and then also create a new note if they want. So for the iphone it's pretty simple layout, a TableViewController for displaying the list with a "Add" button on the NavigationBar that presents the modalview for adding the new item. On the iPad though, the same layout has a lot of wasted space so I opted to go with the popOver method to show the list in a popOver and then let them add from there. My problem is that when the user clicks on the Add button within the PopOver view, the modal view comes up full screen instead of just coming up within the popover view. Here's the code I have so far:
-(void) AddButtonPressed:(id)sender {
NewNoteVC *newNote = [[[NewNoteVC alloc] initWithNibName:#"NewNoteVC" bundle:nil] autorelease];
newNote.defaultClientID = defaultClientID;
UINavigationController *navCon = [[[UINavigationController alloc] initWithRootViewController:newNote] autorelease];
if ([isPopOver isEqualToString:#"YES"]) {
[navCon setModalInPopover:YES];
[self.navigationController setModalInPopover:YES];
[self.navigationController presentModalViewController:navCon animated:YES];
}
else {
[self.navigationController presentModalViewController:navCon animated:YES];
}
}
The "isPopOver" string is just a placeholder sent from the previous screen that called this TableView (I know I can switch this to a boolean for better performance I just put this together real quick to try it out). I know I messed up somewhere, I just don't know what setting I need to get this working correctly.
You need to define the view controller's modalPresentationStyle to be "current context".
navCon.modalPresentationStyle = UIModalPresentationCurrentContext;
This will result in modal view controller filling the popover like the popover's root controller.
Try using the presentViewController:animated:completion: instead of presentModalViewController:animated: and set self.navigationController.definesPresentationContext = YES

ViewDidAppear not called in main view when modal view is presented over a popover

I'm trying to use a popover as an intermediary menu between my main view and a modal view controller. I can successfully present the Modal view controller from the popover by using the following code:
UIStoryboard *storyboardiPad = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad" bundle:nil];
cbwEditControlPanel *editCP = [storyboardiPad instantiateViewControllerWithIdentifier:#"EditCP"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:editCP];
[nav setToolbarHidden:NO];
[nav setModalPresentationStyle:UIModalPresentationFullScreen];
[nav setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:nav animated:YES completion:nil];
self.modalInPopover = NO;
The problem I'm running into is that when the EditCP modal view controller is dismissed, the main view controller never updates. I have a pagecontroller on the main view that should be updated to reflect the number of pages as set in the EditCP modal view controller, but for some reason the modal view controller being called from the popover prevents the main view controller from updating the pagecontroller. I've even tried calling the main view's "View Will Appear" method from the popover or modal view when they are dismissed, but even if the 'viewWillAppear' method is called the pageController will not update!
Any ideas what is preventing the pageController from updating? I even passed a reference to the pagecontroller to the modal view and tried to update it there, but it seems that from the time the popover is presented until it is dismissed, I cannot update the number of pages on the PageController.
Thank you!
So this is an old question but I also came across a similar problem recently when using a popover. My solution was to use an unwind segue to trigger my parent view to perform some action. In my case my parent view contains contact information and the popover contains a list of cites. All I wanted to do was to have the parent view update with the new city once the user selected it from the popover. So in my parent view I create my unwind function as follows:
In the .h:
- (IBAction)unwindToContactTVC:(UIStoryboardSegue *)unwindSegue;
In the .m:
- (IBAction)unwindToContactTVC:(UIStoryboardSegue *)unwindSegue
{
[self updateTableForOffice];
}
In the above .m file is where you would have the logic to do whatever it is you want to in the parent view. To connect this unwind segue go to the child view in the storyboard and control drag from the view icon to the exit icon. You should see a pop up with the name of your unwind segue.
Finally, give that unwind segue a name and then in the child controller in the viewWillDisappear() function call the segue as follows:
- (void)viewWillDisappear:(BOOL)animated
{
[self performSegueWithIdentifier:#"unwind-to-contact-tvc" sender:self];
}
I hope that helps. If someone has a better solution let me know.
Well, I half solved the problem. The only way to get an update function when the popover disappeared was to stop using Storyboards and programmatically present the popover, using the main view as the delegate. I then was able to update correctly inside the popoverControllerDidDismissPopover method.
However, I am still interested in finding a way to update the pageControl when the modal is dismissed, before the popover is dismissed.

Resources