How to access data from one view to another view?

前端 未结 4 1232
隐瞒了意图╮
隐瞒了意图╮ 2021-01-07 11:28

I have an UITabBarController with two tabs:

  1. UINavigationController
  2. OptionsViewController : UIViewController
4条回答
  •  情歌与酒
    2021-01-07 12:11

    I have an easy way to access data between views. Let's try. You have two views named view1 and view2, you can define a View *vc1 property in view2.h, set the vc1 point to view1, when pop the view2, like this:

    view1.m

    //here pop out view1 code
        View2 *view2 = [[View2 alloc] initWithNibName:@"View2" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:view2 animated:YES];
        view2.vc1 = self;  //transfer view1 instance to view2,use vc1 you may handle view1 in view2 directly
            [view2 release];
    

    view2.h

        #import "view1.h"
        @property (nonatomic, retain) IBOutlet View1 *vc1; //here you could name it as view1 as well :)
    

    view2.m

        vc1.lblTable.text = @"ok"; //you'll see "ok" in view1
        [self.navigationController popViewControllerAnimated:YES]; //navigate to view1
    

    //dont forget release vc1

提交回复
热议问题