在 objective-c 中将数据从一个视图控制器传递到另一个视图控制器时,我得到空



当从一个视图控制器单击按钮时,我尝试设置第二个视图控制器的 NSString 属性

CommentsViewController *commentViewController =  [[CommentsViewController alloc] init];
STPopupController *commentPopupController = [[STPopupController alloc] initWithRootViewController:commentViewController];
commentPopupController.containerView.layer.cornerRadius = 4;
commentViewController.streamID = trackID;
commentViewController.radioID = radioID;

[commentPopupController presentInViewController:self];

但是,当视图控制器显示为弹出窗口时,这些字符串值为 null。 我做错了什么?

@property (strong, nonatomic) NSString *streamID;
@property (strong, nonatomic) NSString *radioID;

是视图控制器启动了两次还是什么,我找不到问题所在。这是注释视图控制器的 init 方法

- (instancetype)init {
if (self == [super init]) {
    self.title = @"Comments";
    self.navigationController.navigationBar.tintColor = [UIColor blueColor];
    self.contentSizeInPopup = CGSizeMake(self.view.frame.size.width - 50 , self.view.frame.size.height - 150);
    // self.landscapeContentSizeInPopup = CGSizeMake(400, 200);
}
return self;

}

最好的办法是检查调试器。也许您传递的值为空?

将断点放在您认为有问题的地方。

如果必须将值传递给导航堆栈中不存在的视图控制器,则如果要推送情节提要上的视图控制器,则必须将视图控制器的实例创建为

STPopupController *objViewController = [Self.storyboard instantiateViewControllerWithIdentifier: @"identifier"];

并传递诸如

 objViewController.streamID = trackID;
 objViewController.radioID = radioID;

如果您使用的是xib,请使用以下

SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]

如果我的代码有任何错误,请纠正我,因为我正在从手机发布 ansare。

没有任何迹象表明您在哪里访问streamIDradioID CommentsViewController很难说为什么它们是零。

可能是STPopupController正在通过其 init 方法访问CommentsViewController上的视图属性。这将导致在实际为属性分配任何值之前调用 viewDidLoad 方法。

在这种情况下,最简单的解决方法是在创建弹出控制器之前分配值。

// I would recommend using initWithNibName:nil bundle:nil here, even if
// you create the view in code.
CommentsViewController *commentViewController = [[CommentsViewController alloc] init];
// assign before creating STPopupController
commentViewController.streamID = trackID;
commentViewController.radioID = radioID;
STPopupController *commentPopupController = [[STPopupController alloc] initWithRootViewController:commentViewController];

这可能是由于初始化失败:

- (instancetype)init {
if (self == [super init]) {

请注意==

它应该是:

- (instancetype)init {
if (self = [super init]) {

分配,而不是平等。

初始化目标控制器:

CommentsViewController *commentViewController =  [[CommentsViewController alloc] init];

然后,传递数据:

commentViewController.streamID = trackID;
commentViewController.radioID = radioID;

最后,通过以下方式推送 STPopupController:

[self.popupController pushViewController:commentViewController animated:YES];

注意:popupController#import <STPopup/STPopup.h>附带的STPopupController属性

最新更新