簡體   English   中英

ViewController重新加載tableview數據

[英]ViewController to reload tableview data

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

上面是主要的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。

下面的代碼觸發一個“確定”對話框,然后點擊“確定”,再觸發另一個,如果點擊確定-主線程上的tableview更新:

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM