简体   繁体   English

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

[英]How to pass variables from one view controller to another?

I have created a bottom slide up view controller for a main view controller, but I'm not sure how to pass information from the main view controller to the slide up one.我为主视图控制器创建了一个底部向上滑动的视图控制器,但我不确定如何将信息从主视图控制器传递到向上滑动的控制器。 I have a button in the main view controller that can show or hide the slide up view controller, and I want to use the button to pass information.我在主视图控制器中有一个按钮,可以显示或隐藏向上滑动的视图控制器,我想使用该按钮传递信息。 The code below is the IBAction for the button which would show/hide the slide up view controller, but without a segue I'm not sure how to pass anything.下面的代码是用于显示/隐藏向上滑动视图控制器的按钮的 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;
    }
}

In the code above I've tried to access the slide up view controller (with a class called BottomTableViewController), but there seems to be a problem because it is asking for an expected expression when I attempt to access an object in that class (an NSString called photoDesc).在上面的代码中,我尝试访问向上滑动的视图控制器(使用一个名为 BottomTableViewController 的类),但似乎存在问题,因为当我尝试访问该类中的对象( NSString 称为 photoDesc)。

You can use NSNotification mechanism:您可以使用 NSNotification 机制:

In a controller that has to get param register for notification:在必须获取参数注册以进行通知的控制器中:

-(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;
    }
}

In a controller that has to send a parameter:在必须发送参数的控制器中:

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

Two options: 1) https://medium.com/@jigarm/nsuserdefaults-iphone-objective-c-514febbbf29b两个选项:1) https://medium.com/@jigarm/nsuserdefaults-iphone-objective-c-514febbbf29b

2) Have you tried passing 2)你有没有试过通过

    self      

and

    selectedPhotoDesc 

to the function到函数

    moveToShowBottomTableView 

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

相关问题 如何通过将全局变量用于iMessage将值从一个自定义单元格传递到另一视图控制器? - How to pass values from one custom cell to another view controller by using global variables for iMessage? 如何在IOS中将数据从一个视图传递到另一个视图控制器? - How to pass data from one view to another view controller in IOS? 如何将NSObject从一个视图控制器传递到另一个 - How to pass NSObject from one view controller to another 如何将值从一个视图控制器传递给另一个swift? - How to pass value from one view controller to another swift? 如何将UI ImageView从一个视图控制器传递到另一个? 迅捷3 - how to pass a UI ImageView from one view controller to another? swift 3 在另一个View Controller中使用变量 - Using variables from one View Controller in another 如何使用委托将数据从一个视图控制器传递到另一个视图控制器? - how to use delegate to pass data to from one View Controller to another? 如何将NSMutableDictionary从一个viewController传递到另一个View控制器? - How to pass NSMutableDictionary from one viewController to another View controller? 如何将UI图像从一个视图控制器传递到另一个? - How to pass UI image from one view controller to another? 将NSManagedObject从一个视图控制器传递到另一个视图控制器 - Pass NSManagedObject from one view controller to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM