简体   繁体   中英

I'm getting null while passing data from one viewController to another in objective-c

I try to set a NSString property of a second view controller when a button is clicked from one view controller

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

But when the view controller shows as popup, those string values are null. what are my getting wrong?

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

Is it that the view controller is initiated twice or what, i couldn't locate where the problem is. this is the init method of the comment view controller

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

}

Best thing to do is to check with debugger. Maybe the values you pass are null?

Put breakpoints where you think the problem is.

If you have to pass the values to the view controller which is not present in the navigation stack, then if you are pushing view controller which is on storyboard then you must create instance of view controller as

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

And pass values like

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

And if you are using xib then use following

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

Correct me if there is any mistake in my code as i'm posting answere from my phone.

Without any indication where you are accessing streamID or radioID in your CommentsViewController it's hard to say why they are nil.

It could be that STPopupController is accessing the view property on your CommentsViewController in its init method. This will cause your viewDidLoad method to be called, before you actually assign any values to the properties.

Easiest fix in this case would be to assign the values before you create the popup controller.

// 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];

It's likely due to failed initialization:

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

Note the ==

It should be:

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

Assignment, not equality.

Initialize your target controller:

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

and then, pass datas:

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

Finally, push STPopupController by:

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

Note: popupController is a property of STPopupController that comes with #import <STPopup/STPopup.h>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM