使用 presentViewController 方法在另一个 VC 中设置一个 属性
Setting a property in another VC by using presentViewController method
我正在尝试在另一个 viewController class 中设置一个 .text
属性,但它没有被设置。我怎样才能在不使用 segue 的情况下获得这个结果?
AccountTypeVC *vc = (AccountTypeVC *)[mainStoryboard instantiateViewControllerWithIdentifier:@"AccountTypeVC"];
AccountTypeVC.serviceTitle.text = [tableData objectAtIndex:indexPath.row];
[self presentViewController:vc animated:NO completion:nil];
AccountTypeVC
.h
@property (strong, nonatomic) IBOutlet UILabel *serviceTitle;
没有必要在 instantiateViewControllerWithIdentifier
之后初始化所有 UI 元素。建议在调用视图控制器的 viewDidLoad
后将所有 UI 元素与数据绑定。
在 AccountTypeVC
中再添加一个 属性。
@property (strong, nonatomic) NSString *serviceTitleText;
并在viewDidLoad
方法中将其设置为标签。
- (void)viewDidLoad {
[super viewDidLoad];
self.serviceTitle.text = self. serviceTitleText;
}
并在显示 AccountTypeVC
之前设置 serviceTitleText
属性 而不是标签。
AccountTypeVC *vc = (AccountTypeVC *)[mainStoryboard instantiateViewControllerWithIdentifier:@"AccountTypeVC"];
vc.serviceTitleText = [tableData objectAtIndex:indexPath.row];
[self presentViewController:vc animated:NO completion:nil];
我正在尝试在另一个 viewController class 中设置一个 .text
属性,但它没有被设置。我怎样才能在不使用 segue 的情况下获得这个结果?
AccountTypeVC *vc = (AccountTypeVC *)[mainStoryboard instantiateViewControllerWithIdentifier:@"AccountTypeVC"];
AccountTypeVC.serviceTitle.text = [tableData objectAtIndex:indexPath.row];
[self presentViewController:vc animated:NO completion:nil];
AccountTypeVC
.h
@property (strong, nonatomic) IBOutlet UILabel *serviceTitle;
没有必要在 instantiateViewControllerWithIdentifier
之后初始化所有 UI 元素。建议在调用视图控制器的 viewDidLoad
后将所有 UI 元素与数据绑定。
在 AccountTypeVC
中再添加一个 属性。
@property (strong, nonatomic) NSString *serviceTitleText;
并在viewDidLoad
方法中将其设置为标签。
- (void)viewDidLoad {
[super viewDidLoad];
self.serviceTitle.text = self. serviceTitleText;
}
并在显示 AccountTypeVC
之前设置 serviceTitleText
属性 而不是标签。
AccountTypeVC *vc = (AccountTypeVC *)[mainStoryboard instantiateViewControllerWithIdentifier:@"AccountTypeVC"];
vc.serviceTitleText = [tableData objectAtIndex:indexPath.row];
[self presentViewController:vc animated:NO completion:nil];