如何将变量从一个视图控制器传递到另一个?

时间:2015-06-24 15:36:29

标签: ios objective-c xcode

我为主视图控制器创建了一个底部滑动视图控制器,但我不确定如何将信息从主视图控制器传递到滑动控制器。我在主视图控制器中有一个按钮,可以显示或隐藏滑动视图控制器,我想使用按钮传递信息。下面的代码是显示/隐藏滑动视图控制器的按钮的IBAction,但没有segue我不知道如何传递任何东西。

- (IBAction)btnMoveToShowBottomView:(id)sender {
    UIButton *button = sender;

    switch (button.tag) {
        case 0:
            [_delegate moveToHideBottomTableView];
            break;

        case 1:
           BottomTableViewController *tableScreen = [[BottomTableViewController alloc]...];
        tableScreen.photoDesc = selectedPhotoDesc;
        [_delegate moveToShowBottomTableView];
            break;

        default:
            break;
    }
}

在上面的代码中,我试图访问滑动视图控制器(带有一个名为BottomTableViewController的类),但似乎有一个问题,因为当我尝试访问该对象时它要求一个预期的表达式class(一个名为photoDesc的NSString)。

2 个答案:

答案 0 :(得分:0)

您可以使用NSNotification机制:

在必须获取param注册通知的控制器中:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificaitonCenter defaultCenter] addObserver: self selector:@selector(handleParam:) name:@"myNotification" object: nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)handleParam:(NSNotification*)notification
{
    if ([notification.name isEqualToString:@"myNotification")
    {
        id myParam = notification.object;
    }
}

在必须发送参数的控制器中:

[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object: myObject];

答案 1 :(得分:0)

两种选择: 1)https://medium.com/@jigarm/nsuserdefaults-iphone-objective-c-514febbbf29b

2)你试过传递

吗?
    self      

    selectedPhotoDesc 

到函数

    moveToShowBottomTableView 
相关问题