How to access data from one view to another view?

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

I have an UITabBarController with two tabs:

  1. UINavigationController
  2. OptionsViewController : UIViewController
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-07 12:16

    I usually set IBOutlets in each of my viewcontrollers which point to the other controller. So if I had view controllers A and B. A has an IBOutlet to B and B to A. Then whenever I want to access anything in B from A i just use a dot operator on B.

    In your example UINavigationController would #include "OptionsViewController.h" and have an ivar IBOutlet OptionsViewController * ovc (which is set in IB) and then any instance variable from your options view controller can be referenced as ovc.UILabel.text from the navigation controller. This process can be reversed to access values from your navigation controller in your options view controller.

    Example Navigation Controller (.h):

    #include "OptionsViewController.h"
    @interface UINavigationController // (whatever the name of this class is)
    {
    OptionsViewController * ovc;
    }
    @property (nonatomic, retain) IBOutlet OptionsViewController * ovc;
    @end
    

    Example OptionsViewController.h:

    @interface OptionsViewController 
    {
    UILabel * label;
    }
    @property (nonatomic, retain) IBOutlet UILabel * label;
    @end
    

    Then from UINavigationController (.m) you can just write ovc.label.text to access the text.

提交回复
热议问题