【问题标题】:I'm getting null while passing data from one viewController to another in objective-c在objective-c中将数据从一个viewController传递到另一个viewController时我得到了null
【发布时间】:2016-05-24 14:54:22
【问题描述】:

当从一个视图控制器单击按钮时,我尝试设置第二个视图控制器的 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];

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

@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;

}

【问题讨论】:

  • 这里为什么要设置commentViewController为rootViewController?
  • 为什么没有使用appdelegate didFininishLaunchWithOptions中的代码?
  • 这是另一个故事板中应用程序的另一个部分
  • 你使用两个故事板吗?
  • 一个,但应用程序的启动部分是由其他人使用 xib 文件完成的

标签: ios objective-c


【解决方案1】:

最好的办法是使用调试器进行检查。也许你传递的值是空的?

在您认为有问题的地方设置断点。

【讨论】:

  • 我传递的值不是空的,我已经检查过了
【解决方案2】:

如果您必须将值传递给导航堆栈中不存在的视图控制器,那么如果您正在推送故事板上的视图控制器,那么您必须创建视图控制器的实例

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

并传递像

这样的值
 objViewController.streamID = trackID;
 objViewController.radioID = radioID;

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

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

如果我的代码有任何错误,请纠正我,因为我正在通过手机发布答案。

【讨论】:

    【解决方案3】:

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

    可能是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];
    

    【讨论】:

      【解决方案4】:

      可能是初始化失败:

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

      注意==

      应该是:

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

      赋值,而不是相等。

      【讨论】:

      • 请注意那些试图编辑的人。我故意格式化我的回复,就像我为了反映作者的原始代码所做的那样。同样,我希望尽可能简短(但保留一点上下文)以突出代码中存在问题的部分。
      【解决方案5】:

      初始化你的目标控制器:

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

      然后,传递数据:

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

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

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

      注意: popupControllerSTPopupController 的属性,是 #import <STPopup/STPopup.h> 附带的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-04
        • 1970-01-01
        • 2017-11-08
        • 1970-01-01
        • 2014-07-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多