简体   繁体   English

如何使用委托将数据从第二个ViewController传递到TableViewController

[英]How to pass data from 2nd viewcontroller to tableviewcontroller using delegates

I have 2 viewcontroller s - one is a tableview and the other is a normal viewcontroller . 我有2个viewcontroller s-一个是tableview ,另一个是普通的viewcontroller I want pass data from the second viewcontroller to a tableview controller by using delegates. 我想通过使用委托将数据从第二个viewcontroller传递到tableview控制器。 I have created a delegate and delegatemethod in viewcontroller and implemented delegatemethod in the tableview controller. 我已经在viewcontroller创建了一个委托和delegatemethod ,并在tableview控制器中实现了delegatemethod The problem is that I am getting data to the array but tableview is not reloading. 问题是我正在将数据获取到数组,但tableview没有重新加载。 Why? 为什么?

Can anyone help with this problem? 任何人都可以帮助解决这个问题吗? Thanks in advance. 提前致谢。

    #import "TableViewController.h"

        @interface TableViewController ()<name>{

            NSMutableArray *data;
        }

        @end

        @implementation TableViewController

        - (void)viewDidLoad {
            [super viewDidLoad];
            data = [NSMutableArray array];
            [self.tableView reloadData];
        }

        - (IBAction)callingSecondView:(id)sender {

            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
            ViewController *var = [storyboard instantiateViewControllerWithIdentifier:@"vc"];
            var.delegate = self;
            [self.navigationController pushViewController:var animated:YES];
        }
        -(void)getdata:(NSString *)name{

            [data addObject:name];
            [self.tableView reloadData];

        }
        #pragma mark - Table view data source

        - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

            return 1;
        }

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

            return [data count];
        }
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"  forIndexPath:indexPath];

            if (cell != nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

            }
            cell.textLabel.text = [data objectAtIndex:indexPath.row];
            return cell;
        }

And I am creating delegate a object and protocol in ViewController.h 我正在ViewController.h创建委托对象和协议

        #import <UIKit/UIKit.h>

        @protocol name <NSObject>

        -(void)getdata : (NSString *)name;

        @end


        @interface ViewController : UIViewController
        @property (strong, nonatomic) IBOutlet UITextField *txt;
        - (IBAction)done:(id)sender;

        @property(nonatomic,retain) id<name> delegate;

ViewController.m

        #import "ViewController.h"

        @interface ViewController ()

        @end

        @implementation ViewController
        @synthesize delegate;
        - (void)viewDidLoad {
            [super viewDidLoad];

        }
        - (IBAction)done:(id)sender {

            [delegate getdata:self.txt.text];
            [self.navigationController popToRootViewControllerAnimated:YES];

        }
        @end

I think the first VC don't reloadData because it's not the visible VC. 我认为第一个VC不要reloadData,因为它不是可见的VC。 Try reloadData when the VC willAppear. 当VC出现时,尝试reloadData。

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用委托将数据从 UIPageViewController 传递给子 ViewController - how to pass data from UIPageViewController to child ViewController using delegates 如何将数据从Firebase iOS从TableViewController传递到另一个ViewController - How pass data from Firebase iOS from a TableViewController to another ViewController 我如何将数据从SlideTableTableViewController传递到ViewController中嵌入的TableView - How i pass Data from SlideOut TableViewController to TableView embedded in ViewController 如何在不使用委托和协议的情况下将数据从编程创建的UIPopoverController传递到ViewController的情况下传递数据? - How can I pass data without using segues from programmatically created UIPopoverController to ViewController using delegates and protocols? 无法将 realm 数据从 tableViewController 传递到 viewController - Can't pass realm data from tableViewController to viewController fmdb路径并从另一个viewController第二次使用db - fmdb path and using db 2nd time from another viewController 数据验证完成后如何移至第二个ViewController - How to move to 2nd ViewController after data validation complete 如何从ViewController变为TableViewController - How to go from a viewcontroller to a tableviewcontroller 如何使用导航将字符串从一个tableViewController传递到其他tableViewController - How to pass a string from one tableViewController to other tableViewController using navigation 应用重新运行时,如何保存ViewController中的数据并将其加载到TableViewController中? - How to save data from ViewController and Load it to TableViewController when app rerun?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM