我想使用模型segue。通过单击“年份”按钮,它应该转到“年度视图控制器”,单击表格单元格,屏幕设置为初始视图控制器,在视图控制器中设置标签1。
Person.h
#import <Foundation/Foundation.h>
@interface CarObj : NSObject
@property (nonatomic, strong) NSMutableString *Year;
@property (nonatomic, strong) NSMutableString* Name;
@end
之后点击Name按钮,它应该转到Name View Controller,然后单击它应该将cell的信息保存到Person.h的'Name'参数。
如何实现?我无法设置标签1和标签2 ....
我正在使用的segue:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:@"GrabYear"])
{
YearViewController *yvc = [segue destinationViewController];
[yvc setCurrentCar:currentCar];
}
}
以及我正在使用的行选择:
MenuViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MenuSB"];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
CarObj *c = [cars objectAtIndex:[path row]];
[mvc setCurrentCar:c];
[mvc viewDidLoad]; // I am not getting previous view loaded automatically
[self dismissViewControllerAnimated:YES completion:^{}];
我在currentCar.year中获取的字符串无法通过使用来设置Label [yearLabel setText:currentCar.year];
答案 0 :(得分:3)
控制器对[控制器视图]的调用未在api的事件链中发生。 因此,在没有首先加载子控件的情况下在视图的子控件上设置的任何值都将被覆盖,让您对这个谜团感到困惑。
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
id vc = [segue destinationViewController];
MyController *controller = (MyController*)vc;
//You'll need lazy load controller child controls "now"! with this method call
[controller view];
//now you can set controls
controller.myLabel.text = @"Value will now persist";
//note objects not owned by the "view" can be set here without the hack
controller.myContextSting = @"This value will persist without the hack";
}
答案 1 :(得分:0)
你的问题是这段代码:
MenuViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MenuSB"];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
CarObj *c = [cars objectAtIndex:[path row]];
[mvc setCurrentCar:c];
[mvc viewDidLoad]; // I am not getting previous view loaded automatically
[self dismissViewControllerAnimated:YES completion:^{}];
因为您正在创建MenuViewController
的新实例,请对其进行配置,然后将其丢弃。
您要做的是向年份和名称视图控制器添加委托属性,并让MenuViewController
将自己设置为prepareForSegue:
中的委托。然后,在行选择上,您将执行以下操作:
[self.delegate setCurrentCar:c];
而不是您目前使用mvc
做的事情。