0

我有一个对象从我的主 VC 发送到 viewDidLoad 中的 MasterTabViewController(UITabBarController) 我 NSLog 对象,它显示了对象,很好。现在我需要该对象转到第一个选项卡 UIViewController。我尝试了多次,无法让它去。我是新人,请原谅我的无知,

我通过 segue 将对象从我的主 vc 发送到我的 MasterTabViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showLogin"])
    {
        MasterTabViewController *preview = segue.destinationViewController;
        preview.communityTapped = self.tempCommunity;

    }



}

^这很好用!^ self.tempCommunity 是一个实例社区对象。

MasterTabViewController.h

- (void)viewDidLoad
{
    [super viewDidLoad];
    FirstTabViewController *firstvc;
    firstvc.communityTapped = self.communityTapped;
    NSLog(@"%@ !!!!!!!!! ",self.communityTapped.commDescription);

    // Do any additional setup after loading the view.
}

FirstTabViewController.h

@property (nonatomic, strong) IBOutlet UILabel *descriptionLabel;
@property (nonatomic, strong) Community *communityTapped;

FirstTabViewController.m

- (void)viewDidLoad
{
    self.descriptionLabel.text = self.communityTapped.commDescription;
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

如果有人可以提供帮助,我将不胜感激,因为我已经多次尝试并失败了。

4

2 回答 2

1

You can't set up IBOutlets to a tab bar controllers view controllers (and I see from your project that you never hooked them up). In your viewDidLoad for the tab bar controller, you can get a reference to any of its view controllers with the viewControllers property. So do something like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.firstvc = self.viewControllers[0];
    self.firstvc.communityTapped = self.communityTapped;
    NSLog(@"%@ !!!!!!!!! ",self.communityTapped.commDescription);
}
于 2013-11-10T07:41:44.040 回答
0

我认为问题出viewDidLoad在您的MasterTabViewController.h. 您正在创建变量firstvc,但您没有将值设置为任何值。因此,当您communityTapped在下一行设置值时,您试图将值设置为communityTapped空。

选项1

如果您在界面生成器中设置了选项卡,则需要IBOutlet在您的中创建一个MasterTabViewController.h并将其连接到您的视图控制器。类似于以下内容:

@property (strong, nonatomic) IBOutlet FirstTabViewController *firstvc;

然后要设置communityTapped, 属性的值,您可以使用如下内容:

self.firstvc.communityTapped = self.communityTapped

选项 2

另一种选择是务实地创建选项卡,并将其添加到视图中。我不太确定你的设置是什么,但我想它会是这样的:

FirstTabViewController *firstvc = [[FirstTabViewController alloc] init]; firstvc.communityTapped = self.communityTapped; NSArray *viewControllers = [[NSArray alloc] initWithObjects:firstvc, nil]; [self.navigationController.tabBarController setViewControllers:viewControllers animated:NO];

于 2013-11-10T04:36:12.690 回答