[英]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.