I have three objects in the applicaion. There is a UItableviewcontroller(with nib file) that shows a list of the items. One UIviewcontroller to add item (with nib file) and a mode开发者_如何学JAVAl class that contains item object.
I show the list of item firstly (on application start). I have a navigation button on navigation bar to pop up add view (add as subview) on the same screen (on table view). In add view I have a add button. when I click on add button it adds the record and disappear from the table view but doesn't reload the that.
I have used following code in add item button click action
listitem *home= [[listitem alloc] initWithNibName:@"listitem" bundle:nil];
[self.navigationController pushViewController:home animated:YES];
[home viewWillAppear:YES];
[home release];
[self.view removeFromSuperview];
In viewwillappear function I am reloading the data from database and also reloading the table view data using reloadData.
Am I doing correct. What is the mistake I am doing.
Your code is very tricky to read* but this is what I think you're doing:
You're making a new list each time that you add an item. you don't want to create a new home object, you want to go back to the last one?
i.e. replace your code with
[self.navigationController popViewControllerAnimated:YES];
and this will go back to your original list, which should have refreshed itself (a UINavigationController will call viewWillAppear for you).
Hope that helps.
NB You have to have used a navigation controller to add your 'add item' view otherwise this code won't work :( This is how you should be adding your item view.
AddItem *home = [[AddItem alloc] initWithNibName:@"AddItem" bundle:nil];
[self.navigationController pushViewController:home animated:YES];
This will slide on your add item view.
If you want the add item view to be a popup, a UINavigationController is definitely not the way to do it!
You will need to tell your initial list view when it needs to update itself. You can do this using either a delegate or a notification. I'd go for a delegate in this case.
You need to add this to your add item view controller's code (AddItem's .h file)
@interface AddItem : UIViewController {
UITableViewController *delegate;
}
@property (nonatomic, assign) UITableViewController *delegate;
@end
and synthesize it in your AddItem's .m file
@synthesize delegate;
When you create your add item view controller, set the delegate to be the initial view controller. i.e.
AddItem *home= [[AddItem alloc] initWithNibName:@"AddItem" bundle:nil];
home.delegate = self;
home.view.frame = CGRectMake(10, 20, 300, 300);
[self.view addSubview:home.view];
Finally, when you have added a new item, tell your delegate to refresh itself like so :
[delegate reloadData];
[self.view removeFromSuperview];
*It's standard practice to use capital letters for class names i.e. listitem should be called ListItem. Personally, I'd call it ItemListViewController so it's clear what it does.
精彩评论