Passing value from ViewController to NSObject in Objective-C - objective-c

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

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.

Related

Objective C: How to get values of selected UIPickerView from one class to another

I set the UIPickerView value in one class, implemented the delegate and everything else, and also didSelectRow.
In another ViewController of another class, I set the delegate for dataSource. But, when I try to call it, it is null.
How can this issue be fixed? I appreciate your help.
Code in didSelectRow:
NSInteger distRow = [self.distPicker selectedRowInComponent:
pickerViewComponent];
NSString *distance = self.dist[distRow];
I'm calling both of following methods from another ViewController, but it doesn't work:
NSString *distance = self.dist[[self.distPicker selectedRowInComponent:
pickerViewComponent]];
NSLog(#"picker %#",self.distPicker.dataSource);
NSLog(#"%#", distance);
NSLong for the picker is null and for time is 0.

How to access content of NSMutable array from another class?

I have a FirstViewControlloller in which I have a start and stop button. When start is pressed pictures are taken by using AVCaptureSession until stop button is pressed. These pictures are processed and information (The R-value component of RGB) from each picture is stored in an NSMutableArray called Yaxis.
Added to this View I have a ContainerView. In this view I want to continously display a graph of the information in my Yaxis-array. The problem is to reach this information.
Some parts of my code follow here:
FirstViewController.h
#property (nonatomic, strong) NSMutableArray *Yaxis;
FirstViewController.m
-(void)viewDidLoad{
Yaxis=[[NSMutableArray alloc] init];
[super viewDidLoad];
}
At another place in the code objects are continuously added to "Yaxis" as pictures are taken.
[Yaxis addObject: RGB];
This works fine since I have no problem accessing the array filled with information as long as Im in FirstViewController.
To be able to use this information to update my graph I have tried to create an object of FirstViewController in my ContainerViewController and in that way reach my Yaxis array.
ContainerViewController.m
FirstViewController * myView=[[FirstViewController alloc] init];
myView.Yaxis
When I do this Yaxis is null. As I understand it the problem is that I create a new object of FirstViewController and therefore I will not be able to reach the filled array that is actually a part of another object. So how do I get connected to the object that holds my filled Yaxis array from within my ContainerViweController class?
Access NSMutableArray from another class - Objective C
I found this question that seams to adress the same problem but I did not solve my problem using the answers from there.
You initialize the array in -(void)viewDidLoad. But that method is not called anywhere in your second code snippet. Try putting the Yaxis=[[NSMutableArray alloc] init]; line in your init method
When you manually create a new FirstViewController, this is a new object unrelated to the original, and the mutable array property is initially nil.
The new VC will not magically point to the one that's displayed. Also, the VC might get destroyed, and the array released.
You should separate the storage of this data in another object if it's shared between VCs.

(Beginner)Trying to pass an array from ViewController to DetailController

I have loaded an array (recordArray) in RootViewController class which I need to pass to the DetailViewController class so that I can access any variable within it. I can pass a single value successfully, but I cannot get how to pass the whole array across.
I have created a second NSMutableArray recordArray in DetailsViewController.h, added the #property statement (with retain), and synthesized in DetailsViewController.m. I then added the following line in DetailsViewController.m
recordArray = [[NSMutableArray alloc] initWithArray:self.recordArray];
but that just gives me an empty array in the detail view.
I have read through some of the posts on this, but have not found anything which I understand sufficiently to be able to implement. I expect I'm going about this all wrong....
Thanks in advance for your help guys.
Please refer to this code:
iPhone SDK: How do I pass an array of values from a ViewController onto other ViewController?
There you can refer to Satya's answer.
Let me know if you need more help
EDIT:
Let us assume that there is an NSArray called secondArray declared in SecondViewController
Define a method in SecondViewController which says:
-(void)setValue:(NSArray *)array
{
secondArray = array;
}
Hope this helps.
The problem is that here:
recordArray = [[NSMutableArray alloc] initWithArray:self.recordArray];
You are just assigning the same property recordArray, which is probably nil at this point, what you want to do is dependency injection. So whenever you instantiate your DetailController, you can do:
myDetailController.recordArray = myRecordArray
Where myRecordArray is the value you want to pass or "inject" in the detailview controller.

Passing values from UITableView to UIView in table header

My CoreData model drives series of UITableViewControllers.
One of the detail tables has a header that contains a UIView "GraphView" that uses data from the rows of the table. (It's like iTunes having a custom header to chart song length in a given playlist.) My "playlist" populates from CoreData correctly. But how do I pass data from the TableView to the UIView in its header?
It seems wasteful and redundant to fetch and parse it again, especially when the data is sitting in a convenient mutable array called "steps". Can I just pass the mutable array to the UIView in the header? If so, how?
I've tried creating an NSArray property in the UIView's class called "passedStepsArray", and then setting it from the StepDetailViewController like so:
graphView.passedStepsArray = [[NSArray alloc] initWithArray:steps copyItems:YES];
But back in the GraphView class, it remains NULL. No value gets passed.
Any ideas?
From my visualization of the problem, it seems you are on the right tracks by having an NSArray property in the GraphView. You probably don't need to copy the existing array using the initWithArray:copyItems: method, just pass a non-mutable copy of the populated steps array:
graphView.passedStepsArray = [[steps copy] autorelease];
This is assuming that the graphView passedStepsArray property is set to retain. (Then don't forget to set self.passedStepsArray = nil; in the graphView dealloc methood.)

Typecasting return value of methods when returned value is parent class of the typecast?

I have code similar to this.
MySubclassOfUIView *view = [aUIPickerView viewForRow:4 forComponent:0];
The viewForRow:forComponent method of UIPickerView returns a UIView. MySubclassOfUIView is exactly that: a subclass of UIView.
The UIPickerView delegate (not mentioned here) uses an array of MySubclassOfUIView objects to populate the rows of the UIPickerView components. Thus, I know the viewForRow:forComponent method is really going to be returning a pointer to an object of type MySubclassOfUIView.
Xcode gives me this warning.
Incompatible pointer types initializing 'MySubclassOfUIView*' with an expression of type 'UIView*'.
So I figure that I'll typecast it to fix the warning, which gives me this code.
MySubclassOfUIView *view = (MySubclassOfUIView*)[aUIPickerView viewForRow:4 forComponent:0];
And the warning goes away.
Please forgive my shaky C and Objective-C skills, but am I doing the right thing (as far as the context given so far)? Is there some other better way to handle this situation?
If you are absolutely sure that it will return a MySubclassOfUIView, then it is OK to do this. If there is any chance that it could return something else (such as you made a mistake and added the wrong thing to the array), then you should check the type and use a temporary variable.
UIView *temp = [aUIPickerView viewForRow:4 forComponent:0];
NSAssert([temp isMemberOfClass:[MySubclassOfUIView class]],[NSString stringWIthFormat:#"aUIPickerView returned the wrong class (%#)",[temp class]]);
MySubclassOfUIView *theView = (MySubclassOfUIView*)temp;
What you can do is:
MySubclass* subFoo = [[MySubclass alloc] init];
MySuperclass* superFoo = subFoo;
What you shouldn't do is:
MySuperclass* superFoo = [[MySuperclass alloc] init];
MySubclass* subFoo = superFoo;
This is, because your Subclass will have all properties, selectors, etc from the Superclass. But the Superclass won't have all (..) of the Subclass.
For the rest, see ughoavgfhw's answer.