Iphone 无法使用model segue在视图上设置标签
我想用模型赛格。通过点击“年”按钮,它应该进入“年视图控制器”,点击表格单元格,屏幕设置为初始视图控制器,在视图控制器中设置标签1 Person.hIphone 无法使用model segue在视图上设置标签,iphone,ios,ios6,storyboard,segue,Iphone,Ios,Ios6,Storyboard,Segue,我想用模型赛格。通过点击“年”按钮,它应该进入“年视图控制器”,点击表格单元格,屏幕设置为初始视图控制器,在视图控制器中设置标签1 Person.h #import <Foundation/Foundation.h> @interface CarObj : NSObject @property (nonatomic, strong) NSMutableString *Year; @property (nonatomic, strong) NSMutableString* Name
#import <Foundation/Foundation.h>
@interface CarObj : NSObject
@property (nonatomic, strong) NSMutableString *Year;
@property (nonatomic, strong) NSMutableString* Name;
@end
对于我正在使用的行选择:
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中获取的字符串无法使用设置标签
[yearLabel setText:currentCar.year] 您的问题在于以下代码:
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
的新实例,对其进行配置,然后将其丢弃
您要做的是将委托属性添加到年度和名称视图控制器,并在prepareforsgue:
中将您的MenuViewController
设置为委托。然后,在行选择上,您将执行以下操作:
[self.delegate setCurrentCar:c];
与您当前使用的
mvc
不同,您的问题在于以下代码:
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
的新实例,对其进行配置,然后将其丢弃
您要做的是将委托属性添加到年度和名称视图控制器,并在prepareforsgue:
中将您的MenuViewController
设置为委托。然后,在行选择上,您将执行以下操作:
[self.delegate setCurrentCar:c];
在api的事件链中,没有发生控制器对[controller view]的调用,而不是您当前使用的
mvc
。
因此,在不首先加载子控件的情况下在视图的子控件上设置的任何值都将被覆盖,从而使您无法理解其中的奥秘
-(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";
}
控制器对[controller view]的调用未在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";
}
为了将来,你应该使用授权更新你以前的问题(请删除它以保持整洁)?显示处理segue和表格单元格选择的代码。为什么要使用模式segue而不是推式segue?请看Ray Wenderlich出色的iOS故事板教程:@Wain:添加了代码。为了将来,您应该使用委派更新以前的问题(请删除它以保持整洁)?显示处理segue和表格单元格选择的代码。为什么要使用模式segue而不是推式segue?请看Ray Wenderlich出色的iOS故事板教程:@Wain:添加了代码