【问题标题】:Refresh View Controller after calling dismissViewControllerAnimated from View Controller从视图控制器调用dismissViewControllerAnimated后刷新视图控制器
【发布时间】:2018-08-07 09:39:48
【问题描述】:

重新加载我的视图控制器最合适的方法是什么?

为了扩展该场景,我有一个 UITableView,在视图控制器中填充了单元格。
我通过转换到新的“创建单元格”视图控制器来创建新单元格,但是当我调用 dismissViewControllerAnimated 函数时,新单元格不会出现。
我不能使用 segue 因为初始视图控制器是选项卡栏视图控制器的一个组件,因此如果我从“创建单元格”视图控制器 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];
    }
}

【问题讨论】:

  • 你试过使用通知吗?
  • 我该怎么做?
  • 您可以使用多种不同的方法来实现您想要的。您可以创建delegate 方法、NSNotification 或回调block。请向我们展示一些代码,我们可以看到其中最适合您的方法。
  • 我的电脑目前处于故障状态。目前,我真的无法添加任何代码。但是,为了描述视图控制器,它只包含实例化原型单元所必需的内容,这些单元位于表格视图中,其属性等同于各种数组的元素。因此,它是一个简单的表格视图类,其中包含表格视图单元格。

标签: ios objective-c xcode uitableview uiviewcontroller


【解决方案1】:

您可以拥有通知或委托方法。

由于您没有发布任何代码,请替换名称以方便您。

我会给你一个使用通知的简短示例。

在您的 TableViewController 中:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "newCellCreated"), object: nil, queue: nil) { (notification) in
        self.tableView.reloadData()
    }
}

在您的 CreateCellViewController 中:

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];
}

在您的 CreateCellViewController 中:

-(void) closeView {
    [self dismissViewControllerAnimated:NO completion:^{
        [[NSNotificationCenter defaultCenter]
         postNotificationName:@"newCellCreated"
         object:self];
    }];
}

【讨论】:

  • 我有点明白了,只是我使用了 Objective-C...所以我会尝试找到等效的函数。
  • 添加了目标c版本
  • 太棒了。我很乐意提供帮助
  • 其实,没关系。有一个问题,它最终无法正常工作...:\
  • 问题是刷新后的视图实际上不再显示新单元格了。
猜你喜欢
  • 2013-03-31
  • 2023-03-14
  • 1970-01-01
  • 2016-07-28
  • 2018-04-02
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
相关资源
最近更新 更多