从View Controller调用dismissViewControllerAnimated后刷新View Controller
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从View Controller调用dismissViewControllerAnimated后刷新View Controller相关的知识,希望对你有一定的参考价值。
重新加载视图控制器的最合适方法是什么?
为了扩展这个场景,我有一个UITableView在View Controller中填充了单元格。
我通过转换到新的“创建单元格”视图控制器来创建新单元格,但是当我调用dismissViewControllerAnimated
函数时,新单元格不会出现。
我不能使用segue,因为初始视图控制器是标签栏视图控制器的一个组件,因此如果我从“创建单元格”视图控制器中删除,则标签栏会消失。
那么在成功解雇“Create Cell”视图控制器后,如何重新加载视图?
提前致谢。
有问题的代码,需要刷新(实现非工作通知方法):
@interface MyListings ()
@property (weak, nonatomic) IBOutlet UIButton *createListingButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) ListingModel *listItem;
@property (strong, nonatomic) UserModel *usr;
@end
@implementation MyListings
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(newCellCreated:)
name:@"newCellCreated"
object:nil];
self.listItem = [[ListingModel alloc] init];
self.usr = [[UserModel alloc]init];
NSDate *currentDateTime = [NSDate date];
FIRUser *user = [FIRAuth auth].currentUser;
if ([self.usr getDataForUser:user.email])
{
if ([self.listItem getDataForUser:[NSString stringWithFormat:@"%d", self.usr.user_id]])
{
NSLog(@"Got listings");
};
}
titles = self.listItem.title;
currentBids = self.listItem.starting_bid;
stillAvailables = self.listItem.timer;
listingIDs = self.listItem.listing_id;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"YYYY/MM/dd"];
for(int idx = 0; idx < [self.listItem.listing_id count]; idx++)
{
NSDate *d1 = [dateFormatter dateFromString:self.listItem.timer[idx]];
if([d1 compare:currentDateTime] == NSOrderedDescending)
{
stillAvailables[idx] = @"Expired";
}
else
{
stillAvailables[idx] = @"Available";
}
}
_createListingButton.layer.cornerRadius = 8;
_createListingButton.layer.borderWidth = 1.5f;
_createListingButton.layer.borderColor = [UIColor whiteColor].CGColor;
[_createListingButton addTarget:self action:@selector(createListingButtonHighlightBorder) forControlEvents:UIControlEventTouchDown];
[_createListingButton addTarget:self action:@selector(createListingButtonUnhighlightBorder) forControlEvents:UIControlEventTouchUpInside];
[_createListingButton addTarget:self action:@selector(createListingButtonUnhighlightBorder) forControlEvents:UIControlEventTouchDragExit];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
- (void) newCellCreated:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"newCellCreated"])
[self.tableView reloadData];
}
- (void)createListingButtonHighlightBorder
{
_createListingButton.layer.borderColor = [UIColor colorWithRed:0.61 green:0.00 blue:0.02 alpha:1.0].CGColor;
}
- (void)createListingButtonUnhighlightBorder
{
_createListingButton.layer.borderColor = [UIColor whiteColor].CGColor;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return titles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyListingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyListingsTableViewCell"];
[cell updateCellWithTitle:[titles objectAtIndex:indexPath.row] currentBid:[currentBids objectAtIndex:indexPath.row] stillAvailable:[stillAvailables objectAtIndex:indexPath.row] listingID:[listingIDs objectAtIndex:indexPath.row]];
cell.backgroundColor = [UIColor clearColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"editListing" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"editListing"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
EditListing *destController = segue.destinationViewController;
destController.listId = self.listItem.listing_id[indexPath.row];
}
}
您可以拥有通知或委托方法。
由于您没有发布任何代码,请将名称替换为方便。
我将使用通知给你一个简短的例子。
在TableViewController中:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "newCellCreated"), object: nil, queue: nil) { (notification) in
self.tableView.reloadData()
}
}
在CreateCell ViewController中:
func closeView() {
self.dismiss(animated: true) {
NotificationCenter.default.post(NSNotification.Name(rawValue: "newCellCreated"))
}
}
编辑 - 目标C版
在TableViewController中:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(newCellCreated:)
name:@"newCellCreated"
object:nil];
}
- (void) newCellCreated:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"newCellCreated"])
[self.tableView reloadData];
}
在CreateCell ViewController中:
-(void) closeView {
[self dismissViewControllerAnimated:NO completion:^{
[[NSNotificationCenter defaultCenter]
postNotificationName:@"newCellCreated"
object:self];
}];
}
以上是关于从View Controller调用dismissViewControllerAnimated后刷新View Controller的主要内容,如果未能解决你的问题,请参考以下文章
仅当从警报调用时,如何向第二个向下钻取View Controller添加按钮?
使用jQuery AJAX请求CodeIgniter从Controller显示数据库中的数据到View而不刷新页面
Swift:在调用 applicationDidEnterBackground 时确定 Active View Controller