[英]tableview data doesn't display after dismissing modal view and no data changed
[英]TableView doesn´t reload after modal view
我正在制作一個程序,在那里我為 iPad 練習 UIPopoverController
在第一個彈出窗口中,我有一個帶有一些單元格的 tableView,我想要做的是使用文本字段調用新視圖,以便我可以添加帶有文本字段文本的新單元格。
我遇到的問題是,當我關閉第二個視圖時,tableView 沒有更新。 我無法調用 reloadData 因為第一個視圖不會調用“viewWillAppear”或任何其他視圖。
在 iphone 模擬器中它運行良好,因為 iphone 不使用 uiPopoverController 但在 iPad 中我必須關閉第一個 popover 並且視圖僅在我第二次進入時重新加載。
我通過閱讀一些帖子知道可以使用通知中心來實現它,但我不知道如何
我的代碼是這樣的
- 第一個視圖,tableView
BNRAssetTypeViewController.h
#import <Foundation/Foundation.h>
@class BNRItem;
@interface BNRAssetTypeViewController : UITableViewController
@property (nonatomic,strong) BNRItem *item;
@property (nonatomic, copy) void (^dismissBlock)(void);
@end
BNRAssetTypeViewController.m
#import "BNRAssetTypeViewController.h"
#import "BNRItem.h"
#import "BNRItemStore.h"
#import "BNRDetailViewController.h"
#import "AddNewAssetedTypeViewController.h"
@implementation BNRAssetTypeViewController
-(instancetype)init
{
//Chamar o designated initializer
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
//Titulo
UINavigationItem *navItem = self.navigationItem;
navItem.title=@"New Asseted Type";
//Criar um novo bar button que faz novo item do lado direito
UIBarButtonItem *bbi =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(addNewAssetedType)];
//meter do lado direito
navItem.rightBarButtonItem=bbi;
}
return self;
}
-(instancetype)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
-(void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"ViewDidLoad");
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}
-(void)viewWillAppear:(BOOL)animated
{
NSLog(@"ViewwillApear");
[self.tableView reloadData];
}
-(void)viewDidAppear:(BOOL)animated{
NSLog(@"ViewDidApear");
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[BNRItemStore sharedStore]allAssetTypes]count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
NSArray *allAssetTypes = [[BNRItemStore sharedStore]allAssetTypes];
NSManagedObject *assetType = allAssetTypes[indexPath.row];
// Use key-value coding to get the asset type's label
NSString *assetLabel = [assetType valueForKey:@"label"];
cell.textLabel.text = assetLabel;
// Checkmark the one that is currently selected
if (assetType == self.item.assetType) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType=UITableViewCellAccessoryCheckmark;
NSArray *allAssets = [[BNRItemStore sharedStore]allAssetTypes];
NSManagedObject *assetType = allAssets[indexPath.row];
self.item.assetType=assetType;
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
self.dismissBlock();
} else {
[self.navigationController popViewControllerAnimated:YES];
}
}
-(void)addNewAssetedType{
AddNewAssetedTypeViewController *anatvc = [[AddNewAssetedTypeViewController alloc]init];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:anatvc];
navController.modalPresentationStyle=UIModalPresentationCurrentContext;
[self.navigationController presentViewController:navController animated:YES completion:nil];
}
-第二個視圖
添加NewAssetedTypeViewController.h
#import <UIKit/UIKit.h>
@interface AddNewAssetedTypeViewController : UIViewController <UITextFieldDelegate>
@property (nonatomic,copy) void (^dismissBlock)(void);
@end
添加NewAssetedTypeViewController.m
#import "AddNewAssetedTypeViewController.h"
#import "BNRItemStore.h"
#import "BNRAssetTypeViewController.h"
@interface AddNewAssetedTypeViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end
@implementation AddNewAssetedTypeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(save:)];
self.navigationItem.rightBarButtonItem=doneItem;
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:@selector(cancel:)];
self.navigationItem.leftBarButtonItem=cancelItem;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.view endEditing:YES];
[[BNRItemStore sharedStore] createAssetType:self.textField.text];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)cancel:(id)sender
{
[self.presentingViewController dismissViewControllerAnimated:YES
completion:nil];
}
-(void)save:(id)sender
{
[self.presentingViewController dismissViewControllerAnimated:YES
completion:nil];
}
@end
奧拉! 當在彈出窗口中添加某些內容時,您可以使用NSNotificationCenter
觸發通知。 首先,注冊第一個視圖控制器來觀察該通知。 在第一個 VC 中保留對觀察者的引用,您稍后將注冊:
@implementation BNRAssetTypeViewController {
id _observer;
}
在視圖中會出現:
_observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"somethingAddedNotification"
object:nil
queue:nil
usingBlock:^(NSNotification *notification)
{
[self.tableView reloadData];
}];
在視圖中會消失:
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:_observer];
}
然后在方法save:
在第二個視圖控制器中:
[[NSNotificationCenter defaultCenter] postNotificationName:@"somethingAddedNotification" object:nil];
免責聲明:未經測試的代碼。 讓我知道事情的后續。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.