How can we push data to modalviewcontroller similar to pushviewcontroller
I am trying to push data to modalviewcontroller like pushviewcontroller. I have the following code as to call the modalviewcontroller. It will call the view and insert the data as textfield. I save it as a string and then try to insert in the database. In order to store in the database I need to send the unique key to insert the record.
-(void) editNote{
TextViewController * vc = [(TextViewController *)[TextViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.navigationController presentModalViewController:nav animated:YES];
[vc release];
}
On save option I do call the following method. So now on click of edit button I want to push the record data so that I can use it in save function:
-(IBAction) save{
self.description = self.textFieldBeingEdited.text;
NSString *description_query = [NSString stringWithFormat:@"INSERT OR REPLACE INTO NET (ID,DESCRIPTION) VALUES ('%@','%@');",rec.id, self.description] ;
const char *sql_description = [description_query UTF8String];
sqlite3_exec(db, sql_description, NULL, NULL, NULL);
[[self parentViewController] dismissModalViewControllerAnimated:YES];
}
Has an开发者_JAVA百科yone come across such issues ?
Not sure I understand the problem, but I gather it has to do with sending data from the parent controller to the modal one at save time? Easiest way is to use an Objective-C protocol to notify the parent that the save button has been hit, and run everything from there.
Say, for example, I have a class called SaveController, and a RootViewController.
First, I create a protocol called SaveControllerDelegate.h:
@protocol SaveControllerDelegate <NSObject>
- (void)saveWasHit:(id)sender;
@end
In SaveController.h I can add to the definition:
@interface SaveController : UIViewController {
id <SaveControllerDelegate> delegate;
}
@property (nonatomic, assign) id <SaveControllerDelegate> delegate;
@end
Now, in RootViewController.h, I add the protocol as a delegate:
@interface RootViewController : UIViewController <SaveControllerDelegate> {
...
}
Going back to SaveController, in the .m file I have an IBAction that occurs when the save button is hit. I modify it to test for the delegate method in RootVewController (or whatever the delegate may be), and if present, call the delegate method and send the data I need:
- (IBAction)saveButtonHit:(id)sender
{
if ([self.delegate respondsToSelector:@selector(saveWasHit:)])
[self.delegate saveWasHit:data];
}
All that is left now is to edit RootViewController.m to respond to the delegate:
saveController = [[SaveController alloc] init];
[saveController setDelegate:self];
And implement the protocol:
- (void)saveWasHit:(id)sender
{
//do whatever I need to do with the data from SaveController here.
}
The best way to send information from a view controller to the next is to do it at creation. Maybe you can change the editNote
method into something like this:
-(void)editNote:(Note*)note {
TextViewController * vc = [[TextViewController alloc] initWithNote:note];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.navigationController presentModalViewController:nav animated:YES];
[vc release];
}
And the save method to something like this:
-(IBAction) save{
self.description = self.textFieldBeingEdited.text;
NSString *description_query = [NSString stringWithFormat:@"INSERT OR REPLACE INTO NET (ID,DESCRIPTION) VALUES ('%@','%@');", self.note.id, self.description] ;
const char *sql_description = [description_query UTF8String];
sqlite3_exec(db, sql_description, NULL, NULL, NULL);
[self dismissModalViewControllerAnimated:YES];
}
Note that I removed changed [self parentViewController]
to just self
. This is a common mistake that no longer works as expected on iOS 5.
精彩评论