I have an UIViewController in wich I imported a class:
#import "differenza.h"
and then I created an instance of that class when a button is pressed:
- (IBAction)ok:(id)sender {
differenza *classeDifferenza;
classeDifferenza = [[differenza alloc] init];
[classeDifferenza metodo];
}
As you can see, I also called a method.... now I don't have enough space in that UIViewController for the results of that method, so I need a new ViewController to show all the results... but I don't know how to recall results from that new ViewController, as I can not use the
classeDifferenza.variabile
way ....
This is something I did not really get already. In the past I used (thanks to your help: Setting up class instances in a multi view app (Objective C)) a "common class" where to store all data and all methods... but I don't think It's always the proper way... I feel like I am abusing that solution... or It's the right way to do what I described?
Thanks!
You can pass data with prepareForSegue check this topic
How to pass prepareForSegue: an object
You can send variable results like that
Related
I'm new to Xcode.I'm trying to figure out how to use showViewController.My question is how to call the UIViewController that to display ? via name or a identifier? And where can I find them ? In addition, the document says The default implementation of this method calls the targetViewControllerForAction:sender: method to locate an object in the view controller hierarchy that overrides this method,what does it mean? Should I call targetViewControllerForAction:sender:fist when using showViewController?
showViewController behavior basically depends on context you're app is in.
But calling it is really simple. All you need is reference to your UIViewController object(or it's subclass).
You can do it in couple of different ways:
If you're using Storyboards, give your storyboard view controller identifier, and use API call: UIStoryboard(name, bundle).instantiateViewControllerWithIdentifier(identifier). This gives you UIViewController that you should pass as an argument to showViewController. You could find information about API here: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIStoryboard_Class/index.html#//apple_ref/doc/uid/TP40010909-CH1-SW6
Also, you could get your UIVIewController instance directly constructing it using proper initializer.
So: not worry about this targetViewController method, just get your object (initialize it via constructor or get it from storyboard), and pass it to show method.
In case you have further questions - feel free to ask.
I am a newbie in Objective-C. I stucked into a very big problem.
I have a ModelClass (named MyModel) for data management which is a subclass of NSObject...
And to access the data the app needs authentication using a viewcontroller (say X) I don't have access to its properties (Actually it is a framework's viewcontroller)...
I am accessing that model class from a viewController (named MyViewController)..and I want the retrieval along with authentication of data by calling one method of that model class...but, to present the uncontrolable viewcontroller X for authentication; I must use pushToViewController: method of UINavigationController SDK class.
And I found that subclasses of NSObject class do not respond to navigationController.
But I really need to do this. Any kind of effective help is appreciated and thanks in advance.
I've found a way to do this. I am giving the steps-
1> Create an UIViewController property (say obj) in MyModel.
2> Assign that obj to self(MyViewController's self object) just after creating the object of the model class(MyModel).
3> Put the following line in the model class from where to push to the target viewcontroller (for mine X)-
[self.obj pushViewcontroller:targetViewController animated:YES];
That's it. And thanks all.
I followed John Wordsworth tutorial (http://www.johnwordsworth.com/2011/10/adding-charts-to-your-iphone-ipad-app-using-core-plot/) to produce a line graph with CorePlot.
Later I decided to get some JSON data from a website so that I can use it for my graph.
All the url connection methods are performed in my ViewController.m file. And there I created an NSArray called indicator containing all the JSON data.
My problem is that all of the graph's parameters are defined in a separate NSObject class called SimpleScatterPlot.m where I would like to use the array of strings "indicator" (defined in ViewController.m) to customize the x-Axis labels.
What do I have to do so that I can use this JSON data array in SimpleScatterPlot.m?
I tried #import "ViewController.h" but it did not solve it.
Thanks in advance for any guidance you can give me.
Not sure if I am understanding it correctly, but I think you have not try to access the indicator nsarray from SimpleScatterView.m but to set there the value from ViewController.m.
1) Define a public NSArray in SimpleScatterView.m and synthesize it.
2) Instead of trying to gain access, use "prepareForSegue" in ViewControler.m to set indicator in the destinationSegueController.
3) In SimpleScatterView.m implement "-(void)setIndicator:(NSArray *)indicator" and update the GUI as needed.
Although the "Model - View - Controller" paradigm recommends not to use ViewControllers to perform communications and so on, but that is for another thread.
Create a property in Viewcontroller and synthesize it. Then pass the JSON data to the property.
To access the array in SimpleScatterView.m:
Viewcontroller *viewcontrol = [[ViewController alloc] init];
You can then access the array by using viewcontrol.indicator.
NSLog(#"%#",viewcontrol.indicator);
If I do not read incorrectly what you are trying to do, my guess is that you could appropriately use SimpleScatterView from within your UIViewController view (this is, assuming that SimpleScatterView is actually a UIView).
In this case, when your controller has downloaded its data, you could instantiate SimpleScatterView and display it by adding it to your view controller view.
... <data has been downloaded> ...
SimpleScatterView* scatterView = [[SimpleScatterView alloc] initWithFrame:...];
[self.view addSubview:scatterView];
When you initialize your SimpleScatterView, you could pass it a reference to the data array (say, in its custom init method, or using a property). e.g.:
SimpleScatterView* scatterView = [[SimpleScatterView alloc] initWithFrame:... andData:(NSArray*)...];
or:
scatterView.dataSet = <your_json_array>;
Of course, you have plenty of alternatives to this design. Specifically, I would mention:
the possibility of using a model class to handle all of your data, such that the controller writes the data to the model, while the plot view reads the data from it. The model could be implemented though a singleton in this case for ease of access;
the possibility of using a "dataSource" for your plot view: this would entail defining a protocol between the view and its datasource (e.g., a getData method); your view controller would play the role of the data source (in other words, instead of passing the array to the plot view as in above example, you would pass a reference to the controller and the view would access its data).
I'm updating the code in an app I didn't write while at the same time basically teaching myself objective C so am a beginner with this stuff.
I have a class called TableViewController and in it I have a method that is fired once a JSON feed successfully gets some data. This method is called:
- (void)JSONFetch:(MYJSONFetch *)fetch didDownloadCollection:(id)collection
{
//CODE HERE
}
I have a bunch of other classes that inherit from this class and in turn they call their own version of this method and add their own bits of functionality to the mix.
So for example I have a class called CategoryViewController that has this method defined in it:
- (void)JSONFetch:(MYJSONFetch *)fetch didDownloadCollection:(id)collection
{
[super didDownloadCollection:collection];
}
Notice the SUPER call there, ideally it should be able to access all the code in the parent's method and its own code as well.
In the .h file on the category controller I have this:
#import "MYTableViewController.h"
#interface MYCategoryViewController : MYTableViewController
{
//code here
}
So it should be able to 'see' the other code, but I get this error on the SUPER line:
Instance method '-didDownloadCollection' not found (return type defaults to 'id')
Which presumably means it actually can't see the parent method. It's not set as private as far as I can tell, the .h explicitly mentions the inheritance and other method calls happily bounce back and forth between the two.
So, something I've done has borken this good and I've no idea what. I've an inkling it must be in the MYJSONFetch code, but am not sure.
Anyone shed any light on this, hours spent trying to figure out why it can't see the parent method.
The method is
- (void)JSONFetch:(MYJSONFetch *)fetch didDownloadCollection:(id)collection
So you have to call
[super JSONFetch:fetch didDownloadCollection:collection];
Thats because the method is looks like this JSONFetch:didDownloadCollection: instead of this didDownloadCollection:.
What's the best way to have a list of text items, tap on one and be able to edit it? How about going to a detail view? But then how do you get your change back into the UITableView?
I suppose this could all be done with SQL or CoreData but since the UITableView is based on an array, can we just edit that element of the array and reset the table view?
Eventually I want the data to be persistent so I'll probably go with CoreData or SQL but for now I just want to go from a list to details, edit the details, and go back to the list.
"But then how do you get your change back into the UITableView?"
Both the table view and the detail view should be accessing a common model object. In other words, the detail view that's changing data doesn't have to even know the table view exists.
Look up protocols. Basically you have a class that has information to share. In this case it will be your detail controller or some sort of data object as mentioned above. Have this class (with the info to share) declare a protocol that uses that information. The method should use parameters to sneak out your information. The class also declares a
id (less than)protocol(greater than) delegate
where "protocol" is what you declared above.
Now in this class when you obtain the information that you want to share (however you do it) send the delegate method message to your delegate. [delegate informationObtained:newInfo].
For the class or classes that need this information, implement the protocol method(s). The information you need is being passed by the parameters to the method. It's the same way you use TextFieldDelegate and UIPickerFieldDelegate, only you decide the protocol and you decide how to implement them. Here's a code example
In SpeakHereController.h
#protocol SpeakHereControllerDelegate
-(void)newSoundCreated:(NSString *)newSoundName savedFile:(BOOL)savedFile element: (id)element;
-(void)cancelNewSound;
-(void)soundEdited;
#end
in the interface:
id delegate;
don't forget the #property, make it (non atomic, assign)
In the implementation file,
once I have the information I want to share:
[self.delegate newSoundCreated:myFileName savedFile:self.savedFile element:self];
Now for the tableview controller that wants to use this information:
in the .h file declare that you are going to implement the protocol
#interface AlarmSoundsTableViewController : UITableViewController <SpeakHereControllerDelegate, UITableViewDataSource, UITableViewDelegate>
Then in the implementation file implement the method:
-(void)newSoundCreated:(NSString *)soundName savedFile:(BOOL)savedFile element:(id)element
{
[self setSoundFileName:soundName];
...