I have 2 ViewControllers
ViewControllerWithCollectionView (FIRST) and ModalViewControllerToEditCellContent (SECOND)
I segue from FIRST to SECOND modally. Edit cell. Return.
After dismissing SECOND controller, edited cell doesn't get updated until i call
[collection reloadData]; somewhere manually.
Tried to put it in viewWillAppear:animated:, when i check log, it's not called (after dismissing SECOND)
I've tried various solutions, but i can't brake thru (maybe I'm just too exhausted). I sense that I'm missing something basic.
EDIT dismiss button
- (IBAction)modalViewControllerDismiss
{
self.sticker.text = self.text.text; //using textFields text
self.sticker.title = self.titleText.text;// title
//tried this also
CBSStickerViewController *pvc = (CBSStickerViewController *)self.stickerViewController;
//tried passing reference of **FIRST** controller
[pvc.cv reloadData];//called reloadData
//nothing
[self dismissViewControllerAnimated:YES completion:^{}];
}
It's tough to tell from the posted code what's wrong with the pointer to the first view controller that you passed to the second. You should also be able to refer in the second view controller to self.presentingViewController. Either way, the prettier design is to find a way for the first view controller to learn that a change has been made and update it's own views.
There are a couple approaches, but I'll suggest the delegate pattern here. The second view controller can be setup to have the first view controller do work for it, namely reload a table view. Here's how it looks in almost-code:
// SecondVc.h
#protocol SecondVcDelegate;
#interface SecondVC : UIViewController
#property(weak, nonatomic) id<SecondVcDelegate>delegate; // this will be an instance of the first vc
// other properties
#end
#protocol SecondVcDelegate <NSObject>
- (void)secondVcDidChangeTheSticker:(SecondVc *)vc;
#end
Now the second vc uses this to ask the first vc to do work for it, but the second vc remains pretty dumb about the details of the first vc's implementation. We don't refer to the first vc's UITableView here, or any of it's views, and we don't tell any tables to reload.
// SecondVc.m
- (IBAction)modalViewControllerDismiss {
self.sticker.text = self.text.text; //using textFields text
self.sticker.title = self.titleText.text;// title
[self.delegate secondVcDidChangeTheSticker:self];
[self dismissViewControllerAnimated:YES completion:^{}];
}
All that must be done now is for the first vc to do what it must to be a delegate:
// FirstVc.h
#import "SecondVc.h"
#interface FirstVc :UIViewController <SecondVcDelegate> // declare itself a delegate
// etc.
// FirstVc.m
// wherever you decide to present the second vc
- (void)presentSecondVc {
SecondVc *secondVc = // however you do this now, maybe get it from storyboard?
vc.delegate = self; // that's the back pointer you were trying to achieve
[self presentViewController:secondVc animated:YES completion:nil];
}
Finally, the punch line. Implement the delegate method. Here you do the work that second vc wants by reloading the table view
- (void) secondVcDidChangeTheSticker:(SecondVc *)vc {
[self.tableView reloadData]; // i think you might call this "cv", which isn't a terrific name if it's a table view
}
Related
I have a main view controller that is able to process gestures in an iPad app.
I launch a second view controller via:
wVC = [self.storyboard instantiateViewControllerWithIdentifier:#"vc_webView"];
[self presentViewController:wVC animated:YES completion:nil];
If I now gesture while that VC is showing, the gestures are not processed. How can I "pass" the gestures to the first storyboard for subsequent processing so that I don't need to rewrite the whole gesture functionality in the new VC?
OK so the answer is to use delegates.
For completion's sake, here is the way to do it:
Set up the first VC to be a delegate of the second, then in the second VC call the delegate function.
In original controller right before presenting the wVC:
wVC.delegate = self;
In the wVC.h file:
#protocol senddataProtocol <NSObject>
-(void)ProcessPasswordGesture:(NSInteger)iGest;
#end
#property(nonatomic,assign)id delegate;
In the wVC.m file:
#synthesize delegate;
[delegate ProcessPasswordGesture:<data>];
Hope this helps someone else!!
I have two view controllers inside a Navigation Controller.
In the first view controller I have two buttons. Both of them call the second view controller using a Push segue, but:
I need to know which button sent me in the second view controller. How?
In the second view controller I have a UIDatePicker and a Button "Ok": how can I send the chosen date to the first view controller when Ok is pressed? (And how do I receive them?)
EDIT:
I don't know if my problem is clear: now I know how to pass data from the first view controller to the second view controller with prepareForSegue, but what I really need is to pass data (the picked date) from the second view controller to the first, and how can I do it without a prepareForSegue (when Ok is pressed)?
EDIT2:
I made it. It was so simple, guys...
I decided to use modal segue:
Firstviewcontroller.h:
+(FirstViewController *)getInstance;
Firstviewcontroller.m:
static FirstViewController *instance =nil;
+(FirstViewController *)getInstance
{
return instance;
}
and in its ViewDidLoad:
instance = self;
Secondviewcontroller.m, in the OkButton IBAction:
SecondViewController *secondViewController = [SecondViewController getInstance];
//...
//modify what I need to modify in secondviewcontroller
//...
[self dismissModalViewControllerAnimated:YES];
That's it.
Thank you all anyway.
Assign Identifier to each segue in storyboard and implement
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
[vc setDelegate:self];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
For more info about How to use storyboard and pass value check this article or this discussion on stackoverflow
for the second question you can use delegate pattern
IN SecondViewController.h
#protocol SomethingDelegate <NSObject>
- (void)dateChanged:(NSString *)dateStr; //you can use NSDate as well
#end
#interface ViewController2 : UIViewController
#property(weak) id<SomethingDelegate> delegate;
#end
in .m file
-(void) OkClicked{
[_delegate dateChanged:#"YOUR_DATE_VALUE"];
}
In FirstViewController.h
#import "SecondViewController.h"
#interface FirstViewController : UIViewController<SomethingDelegate>
in .m
-(void)dateChanged:(NSString *)dateStr{
// do whatever you need with dateStr
//also i made some change in prepareForSegue method
}
Note:- take care your naming convenes for VC
just pass the button id to the second viewcontrol.
use delegates to sent the data from second viewcontroller back to first view controller
regards
Johan
I have a UISplitview controller (with a master and detail view). So, when the detailview loads the viewDidLoad calls a function named [self animateToPosition]; with the following code:
-(void*)animateToPosition
{
NSLog(#"Called Function");
[worldV animateToPosition:MaplyCoordinateMakeWithDegrees(-122.4192, 37.7793) time:1.0];
[self.view setNeedsDisplay];
return 0;
}
Now, when this is first run, my globeViewC correctly animates to the position passed in (worldV is initialized and created an all in viewDidLoad of detailviewController also "Called Function" is printed in the log.
However, I have a view controller and in the didSelectRowAtIndexPath I have the following code (in the file masterViewController.m)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[testArray objectAtIndex:indexPath.row]isEqualToString:#"Menu1"]) {
DetailViewController *test=[[DetailViewController alloc]init];
[self addChildViewController:test];
[self.view addSubview:test.view];
[test didMoveToParentViewController:self];
[test animateToPosition];
}
}
When I select the cell called menu1, "Called function" DOES display in the NSLog, but the animation that occurred when the view was first loaded doesn't anymore. I'm wondering if I must somehow reload the detailviewcontroller or initialize it again (but I tried initializing it again and that didn't work)
Would like if someone could see this and help me get this working.
It would be helpful if you could show more, especially how you alloc and init your worldV and what you have in your viewDidLoad.
Normally you don't create a new DetailViewController for SplitView setup.
Instead of creating a new DetailViewController, try to grab a hold of the existing detail view controller by creating a property:
#property(strong, nonatomic) DetailViewController *detailViewController;
and:
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
First thing to test here is whether your worldV is nil. A simple NSLog in animateToPosition would reveal the answer.
And whenever you need to call that method, do:
[self.detailViewController animateToPosition];
To animate from one coordinate to another with time, you may need to change your animateToPosition method to:
-(void)animateToPosition:(MaplyCoordinate)coordinates time:(CGFloat)time
Consult whether or not your MaplyCoordinate is an object, which I doubt.
So, to sum up, in your tableview's viewDidLoad, get a hold of the detail view controller.
When didSelectRow gets called, use the new method suggested and pass the coordinate and time this way:
[self.detailViewController animateToPosition:coordinates time:1.0];
Hope this helps.
In didSelectRowAtIndexPath function you create an DetailViewController but you don't add it to the view hierarchy. You should add it to the parent view controller like this
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
or just add it to navigation or present it as a modalViewController
On iPad simulator, I have a ViewController A that presents an UIPopoverController whose contentViewController is ViewController B, inside which I have a button to dismiss the UIPopoverController.
When it is dismissed, I need to update the view of ViewController A based on some field in ViewController B.
In order to do this, I am declaring ViewController A as a property (weakref) of ViewController B so that within ViewController B where it dismisses the popover, I can say:
[self.viewControllerA.popover dismissPopoverAnimated:YES];
self.viewControllerA.popover = nil;
self.viewControllerA.textLabel.text = self.someField
Is this the correct way of doing it? Since there is no callback when we dismiss the popover pragmatically, I can't think of any better solution.
Anybody has a better idea? Passing view controllers around just seems awkward to me.
The best way is use of Delegation, just declare the delegate in your controller B like
#protocol ControllerSDelegate <NSObject>
-(void) hidePopoverDelegateMethod;
#end
and call this on action for passing the data and dismiss of controller like
if (_delegate != nil) {
[_delegate hidePopoverDelegateMethod];
}
and
in your controller A you can handle this delegate call
-(void) hidePopoverDelegateMethod {
[self.paymentPopover dismissPopoverAnimated:YES];
if (self.paymentPopover) {
self.paymentPopover = nil;
}
[self initializeData];
}
I think, delegates or sending NSNotification will make better.
Note:
A change of the execution sequence will do more perfection to your current code.
self.viewControllerA.textLabel.text = self.someField
[self.viewControllerA.popover dismissPopoverAnimated:YES];
self.viewControllerA.popover = nil;
I have a UITableView embedded in a UIView that I can't figure out how to update. When I come to this page with the UITableView embedded in the UIView there's a button that when pressed brings up a modalForm. There is a textField that the user enters a name into and then presses another button to "Create" the object, dismiss the modalForm, and update the app data. However, I'm not sure how to refresh my UITableView data… Is there a way to tell the UITableView to refresh when the modalForm view is dismissed?
EDIT: I know i need to send this message [tableView reloadData]; in order to refresh, but I'm wondering where I can put it so it gets called upon dismissal of the modalForm?
In the view controller that is presenting and dismissing the modal, you should be calling the dismissViewControllerAnimated:completion method to dismiss the modal. The modal should not dismiss itself. You can use a completion block to execute whatever code you would like that will execute when the modal is finished dismissing. Example below.
[self dismissViewControllerAnimated:YES completion:^{
//this code here will execute when modal is done being dismissed
[_tableView reloadData];
}];
Of course don't forget to avoid capturing self strongly in the block.
If you end up having the modal dismiss itself, you will need either a delegate method so the modal can communicate back to the presenting view controller, or a notification sent by the modal and captured by the presenting view controller, or you could implement viewWillAppear: in the presenting view controller. This method will fire everytime the view is about to appear. Which means the very first time as well as after a modal is dismissed and is about to show the view it was presenting over top of.
----------------------------------------------------------------------------------------
Below is an example of writing your own protocol and using it.
MyModalViewController.h
#protocol MyModalViewControllerDelegate <NSObject>
//if you don't need to send any data
- (void)myModalDidFinishDismissing;
//if you need to send data
- (void)myModalDidFinishDismissingWithData:(YourType *)yourData
#end
#interface MyModalViewController : UIViewController
#property (weak) id <MyModalViewControllerDelegate> delegate;
//place the rest of your properties and public methods here
#end
The where ever you want to in your MyModalViewController implementation file, call your delegate method of choice. You should first make sure your delegate actually responds to the selector though.
MyModalViewController.m
if ( [self.delegate respondsToSelector:#selector(myModalDidFinishDismissing)] )
[self.delegate myModalDidFinishDismissing];
In your viewcontroller that is presenting the modal, you need to state in the header file that you conform to the protocol, you need to set the delegate of the modal as the viewcontroller, and make sure you actually implement the delegate method you intend on using.
MyPresentingViewController.h
#interface MyPresentingViewController : UIViewController <MyModalViewControllerDelegate>
MyPresentingViewController.m
myModal.delegate = self;
- (void)myModalDidFinishDismissing {
//do something
[tableView reloadData];
}
You can have a delegate variable in your modal form class. When you dismiss your modal form, you can call something like [delegate performSelector:#selector(modalFormClosed)] which calls the [tableView reloadData].
#interface ModalForm : UIViewController
#property (nonatomic, weak) id delegate;
#end
#implementation ModalForm
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.delegate performSelector:#selector(modalFormClosed)];
}
#end
In your class that uses the ModalForm:
myModalForm.delegate = self;
And make sure you have the modalFormClosed method too:
- (void)modalFormClosed {
[self.tableView reloadData];
}
Or you can send a NSNotification (look into NSNotificationCenter) when your modal form disappears.
I wish I had the reference, but some years ago I saw a note from Apple saying a modal view should never dismiss itself. Instead you call a method on a presenting view controller. In this method the presenter dismisses the modal view controller. Typically I put a category on my UIViewControllers:
#interface UIViewController (Extension)
-(void)simpleDismissAnimated:(BOOL)animated;
#end
#implementation UIViewController (Extension)
-(void)simpleDismissAnimated:(BOOL)animated {
[self dismissViewControllerAnimated:animated completion:nil];
}
Either include it in all your view controller subclasses, or in your pch file.
So have your modal view controller call:
[self.presentingViewController simpleDismissAnimated:YES];
Then in your presenting view controller, override the implementation of simpleDismissAnimated: to something like:
-(void)simpleDismissAnimated:(BOOL)animated {
[_tableView reloadData];
[self dismissViewControllerAnimated:animated completion:nil;
}
That should take care of it.
Try this line:
[self.tableView reloadData];
You can try putting [tableView reloadData] in viewWillAppear method where your tableView is.
I had the same problem. ReloadData didn't work me. I solved it so that i made new method like this:
- (void)insertNewString:(NSString *)text{
if (!_yourArrayWithData) {
_yourArrayWithData = [[NSMutableArray alloc] init];
}
[_yourArrayWithData insertObject:text atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
and when i want to add something to tableView, i use button with action like this:
- (IBAction)SaveNewPhrase:(id)sender {
NSString *mainText = _NewPhraseTextView.text; //I take nsstring from textView
if([mainText isEqual:#""]){
return;
}else{
NSMutableArray *contentFile = [NSArray arrayWithContentsOfFile:docPathContents()];
NSMutableArray *copyContentFile = [[NSMutableArray alloc] init];
//save array with new item
[copyContentFile addObject:mainText];
[copyContentFile addObjectsFromArray:contentFile];
[copyContentFile writeToFile:docPathContents() atomically:YES];
[self insertNewString:mainText]; //refresh tableView
[_NewPhraseTextView resignFirstResponder];
}}
I hope, that it helps.