ViewController重新加载表格视图数据

时间:2018-12-04 03:05:15

标签: ios objective-c xcode uialertcontroller

我从ViewController调用UIAlertController。当我在UIAlertController中按“确定”时,将提示另一个“确定”对话框。

现在的问题是,当我从对话框中单击“确定”按钮时,我能够退出UIAlertController,但是我想要的是退出UIAlertController并刷新主ViewController。

有人可以帮助我吗? :(

- (IBAction)btnAddDidPressed:(id)sender {
    AddCashValueVC *addCashValueVC = [storyboard instantiateViewControllerWithIdentifier:@"addCashValueVC"];

    alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"nav_Add_Credit", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController setValue:addCashValueVC forKey:@"contentViewController"];

    [self presentViewController:alertController animated:YES completion:nil];
}

上面是Primary ViewController。显示我如何调用UIAlertController。

- (IBAction)btnProceedDidPressed:(id)sender {
[self convertCashValue];
[self dismissKeyboard];
}

-convertCashValue:{
        self->alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"msg_App", nil) message:[result objectForKey:@"msg"] preferredStyle:UIAlertControllerStyleAlert];
                self->cashValueVC.update= YES;

                UIAlertAction *openAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"btn_Ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

                    CashValueVC *cashVC = [[CashValueVC alloc] initWithNibName:nil bundle:nil];
                    [self dismissViewControllerAnimated:YES completion:^{
                        [cashVC viewDidLoad];
                        [cashVC viewWillAppear:YES];
                        [cashVC.tableView reloadData];
                    }];

                }];

                [self->alertController addAction:openAction];

                [self presentViewController:self->alertController animated:YES completion:nil];
}

上面是UIAlertController中的另一个UIAlertController。

1 个答案:

答案 0 :(得分:0)

下面的代码触发一个“确定”对话框,然后点击“确定”,再触发另一个,如果点击确定,主线程上的表视图将更新:

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (strong, nonatomic) NSArray *array; //table view dataSource
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

// update button tapped
- (IBAction)action:(id)sender {
//changing table data source
    _array = @[
               @"afasfasf",
               @"afasfasf",
               @"afasfasf",
               @"afasfasf",
               @"afasfasf",
               @"afasfasf",
               @"afasfasf"
               ];


    //alert controllers
    UIAlertController *firstAlertController = [UIAlertController alertControllerWithTitle:@"First alert"
                                                                                  message:nil
                                                                           preferredStyle:UIAlertControllerStyleAlert];

    UIAlertController *secondAlertController = [UIAlertController alertControllerWithTitle:@"Second alert"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleAlert];





    //Actions
    UIAlertAction *firstControllerOKAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction *action) {
                                                       //firing second dialog
                                                       [self presentViewController:secondAlertController animated:YES completion:nil];
                                                   }];

    UIAlertAction *secondControllerOKAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction *action) {

                                                       //calling on main thread table view update
                                                       dispatch_async(dispatch_get_main_queue(), ^{
                                                           [self.table reloadData];
                                                       });
                                                   }];

    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"cancel"
                                                     style:UIAlertActionStyleCancel
                                                   handler:^(UIAlertAction *action) {}];

    //adding actions to first dialog
    [firstAlertController addAction:firstControllerOKAction];
    [firstAlertController addAction:cancel];

    //adding actions to second dialog
    [secondAlertController addAction:secondControllerOKAction];
    [secondAlertController addAction:cancel];


    //firing first dialog
    [self presentViewController:firstAlertController animated:YES completion:nil];
}

//delegate and datasource methods
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    [tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"23"];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"23"];

    cell.textLabel.text = _array[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}



@end