-2

I need to pass value from a ViewController to NSObject as soon as the view loaded using Xcode with Objective c.

I am using the code below but the value is null.

-(void)viewDidAppear:(BOOL)animated
{
     MyHomeModelNSObject *nsOb;
     nsOb = [[MyHomeModelNSObject alloc] init];
     nsOb.myString = self.userName.text;
}

The above code is working between Views when using segue, but it does not work with when passing the value to NSObject.

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3741700
  • 21
  • 1
  • 6

1 Answers1

2

The above code is working between Views when using segue, but it does not work with when passing the value to NSObject.

You're not using a real object. You're declaring a pointer to an object, but never allocating the object itself:

MyHomeModelNSObject *nsOb;
nsOb.myString = self.userName.text;

See? You're missing the bit where you do:

nsOb = [[MyHomeModelNSObject alloc] init];

What's more, even if you added that, the object would be deallocated as soon as viewDidAppear exits because it's a local variable. If you want it to hand around, you'll need to 1) create it and then 2) assign it to some property of your view controller or another object.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I am new in Xcode, I did something like this but it still give me null ( Following_HomeModel *fol; fol = [[Following_HomeModel alloc]init]; fol.followingUserName = self.userName; – user3741700 Jan 22 '16 at 23:14
  • What gives you null? It'll help if you edit your question to show your code and be specific about what you expect and what you're seeing. That'll make it possible for someone to help. – Caleb Jan 23 '16 at 00:26