I have two ViewController , ViewController and getraenkeController.
Now in ViewController.h is float named getraenk. I read that I should do it like this:
#property float getraenk;
and then in getraenkeController.m I should do this:
ViewController.getraenk
but that doesn't work.
I also importet the header from ViewController.h
Now how can I access the float from ViewController in getraenkeController?
The float is added as a property to instances of the class, not the class itself. Once you create an instance of the class, then you'll be able to access the property.
ViewController *controller = [[ViewController alloc] init...];
controller.getraenk = 4334.3;
First declare public property
if you use ARC :
#property(nonatomic, assign) float getraenk;
and then you must create object of ViewController class e.g.:
ViewController *obj = [ViewController new];
now you should get access to getraenk property e.g.:
obj.getraenk
Related
ClassA *a = [[ClassA alloc] initWithNibName:#"classA" bundle:nil];
a.viewInClassA.hidden = NO;
When i Run this code, the a.viewInClassA.hidden = NO do not make any effect PLEASE HELP
This is happen because you are making new class you not taking reference. So make property of that class and pass reference of class c in another class and then you can do hide using that property
In ClassA for example you have tableview.
Put this in ClassA.h file
#property (weak, nonatomic) IBOutlet UITableView *tableview;
From classB.h file make property of classA
#property (Strong, nonatomic) classA *classAObject;
And form classb.m where you want to hide table view write this
self.classAObject.tableview.hidden = YES;
when you open classB pass classA reference
ClassB *classB = [[ClassB alloc] initWithNibName:#"ClassB" bundle:nil];
classB.classAObject = self;
[self.navigationController pushViewController:classB animated:YES];
Don't try and manipulate another view controllers views. It's a violation of the principle of encapsulation. It's bad design, and sometimes it fails, as in your case.
Instead, add a property to your ClassA view controller that tells whether or not your view should be hidden. In your ClassA view controller's viewWillAppear read the property and use it to hide or show the view.
I have a main ViewController that contains a desginated class. Within that ViewController there is a Container that is linked to an embed ViewController. Within that embed ViewController I am creating an NSMutableArray. I am not trying to access that array inside the main ViewController. I know that if I use:
create_challenge_peopleSelect *myScript = [[create_challenge_peopleSelect alloc] init];
NSLog(#"%#",myScript.selectedCells);
The NSLog will output null because I am creating a new ViewController and that gets rid of the already set array. So my question is how can I access that array without overwriting it?
UPDATE:
Heres where the NSMutableArray is being created:
create_challenge_peopleSelect.h:
#property (strong, nonatomic) NSMutableArray *selectedCells;
create_challenge_peopleSelect.m:
if([selectedCells containsObject:label.text])
{
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedCells removeObjectIdenticalTo:label.text];
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedCells addObject:label.text];
}
This class is the container class off the main ViewController
No I want to access the selectedCells within my main ViewController, I have been doing things such as:
create_challenge_peopleSelect *myScript = [[create_challenge_peopleSelect alloc] init];
I would prefer to stay away from the App Delegate If possible.
You seem to be unclear on the difference between classes and instances. OK, so, say we have two NSArrays:
NSArray *a = [[NSArray alloc] initWithObjects:#"hello", #"I", #"am", #"an", #"array", nil];
NSArray *b = [[NSArray alloc] initWithObjects:#"so", #"am", #"I", nil];
If I do a.count, I'll get 5 as the answer because the array contains five objects. Meanwhile, if I do b.count, I'll get 3, because that array contains three objects. It isn't that creating b "gets rid of the already set count". They are separate objects completely unrelated to each other.
Your view controller class is the same way. When you create a different instance, it doesn't overwrite the old one -- it's just not the same object. In order to use the original view controller object, you need to get a reference to it.
So how do you get a reference to it? Well, the general answer is you design your app so that the two objects know about each other. There are lots of specific ways to accomplish this. A lot of people will say "Just stick a reference in the app delegate." That is one thing you can do, but it's not always the best choice. It can get out of control if you just stick everything in your app delegate. Sometimes it's the right answer, often other things are the right answer. Another approach is to have an object that knows about both of those objects introduce them to each other. But sometimes there is no such object. So it's situational.
Basically, instead of creating a new view controller, you need to maintain a pointer to the original.
I suggest storing an instance of your UIViewController in the AppDelegate in order to retain the particular instance of the view controller you've created by making it a global variable.
ex. In the App Delegate.h
#import "ViewController.h"
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (nonatomic) ViewController *viewController;
Then from whatever view controllers' .m's from which you need to read/write to the variable, create a pointer to the application's app delegate, ex:
#import "AppDelegate.h"
#interface WhateverViewController ()
AppDelegate *mainDelegate;
- (void)viewDidLoad {
mainDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
}
So wherever you first create that view controller in your code (before ever using it), initialize it using this global variable. ex. If you're using xibs:
mainDelegate.viewController = [[UIViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self.navigationController pushViewController:mainDelegate.viewController animated:YES];
ex. If you're using storyboards:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"StoryboardName" bundle:nil];
mainDelegate.viewController = [storyboard instantiateViewControllerWithIdentifier:#"viewControllerID"];
[self.navigationController pushViewController:mainDelegate.viewController animated:YES];
(This is assuming it's in a place other than the app delegate in which case the pointer to the App Delegate isn't needed.)
Then when accessing the array from another UIViewController use
mainDelegate.viewController.array
To access the NSMutableArray from one class to another class use following code.
In the first view controller in which u have declared the object of NSMutableArray, declare the property and synthesize for the same as below,
//In FirstViewcontroller.h class,
#property (nonatomic, strong) NSMutableArray *arrData;
//In FirstViewcontroller.m class
#synthesize arrData;
Also FirstViewcontroller object should be global so you can create the object of FirstViewcontroller in app delegate file.
//appdelegate.h
#property (nonatomic, strong) FirstViewcontroller *objFirst;
//appdelegate.m
#synthesize objFirst;
FirstViewcontroller *objFirst=[[FirstViewcontroller alloc]init];
Now in SecondViewcontroller in which you have to access array,
create the share object of Appdelegate file
//SecondViewcontroller.m
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
Then use will get the required array as below,
app.objFirst.arrData
This is your required array I hope it will help you.
The basic idea here is that in your original class, the array is referred to by a pointer. Your original class would allocate it and presumably load it. Other parts of your program can be handed the contents of the property, which is a pointer, assign that to their own pointer holder, and use it as if you had declared it there. Please use the above code;
MyClass *aClass = [[MyClass alloc] initWithMyInitStuff];
NSMutableArray *ThatArray = aClass.MyArray;
NSLog("Count of ThatArray: %d", [That.Array count]);
What you've done in the code provided is set a public property for a mutable array...
#property (strong, nonatomic) NSMutableArray *selectedCells;
The NSMutableArray is not "created" by setting that property. At some point in your code you also have to create the NSMutableArray by initialising...
NSMutableArray *selectedCells = [[NSMutableArray alloc] init];
or by using a convenience method such as...
NSMutableArray *selectedCells = [NSMutableArray arrayWithCapacity:(NSUInteger)<initialising capacity>];
or
NSMutableArray *selectedCells = [NSMutableArray arrayWithArray:(NSArray *)<initialising array>];
Initialising an NSMutableArray is often done only once. If it is repeated, the contents are overwritten against the property used to point to the array. As such, a useful location for this is often within the viewDidLoad view controller lifecycle method.
I have a map-controller where the user can tab the map to add a new marker. The idea is then to store the coordinates in the new marker-class. The problem I am facing is setting those variables.
NewMarkerController.h
#interface NewMarkerController : UIViewController
{
NSNumber *posLat;
NSNumber *posLng;
}
#property (nonatomic, retain) NSNumber *posLat;
#property (nonatomic, retain) NSNumber *posLng;
#end
I am also synthesizing this in the .m file is that makes any difference.
MapController.m
NewMarkerController *vc = [[NewMarkerController alloc] init];
[vc posLat:coordinate.latitude];
The last line shows an error saying No visible #interface for 'NewMarkerController' declears the selector 'postLat', but...there is...?
Can anyone spot the problem I am having here?
[vc setPosLat:coordinate.latitude];
or
vc.posLat = coordinate.latitude;
This syntax:
[vc posLat:coordinate.latitude]
means that posLat is a function of the vc kind of class. As you want to set a variable, if you synthesized it you can just do:
[vc setPosLat:coordinate.latitude]
or
vc.posLat = coordinate.latitude
Im trying this code in one Controller.h
#interface ColorPickerViewController : UIViewController {
IBOutlet UILabel *Labelniz;
}
#property (nonatomic, retain) UILabel* Labelniz;
This code in Controller.m
#implementation ColorPickerViewController
#synthesize Labelniz=_Labelniz;
But i using something like ColorPickerViewController.Labelniz gives an error.
Thank you in advance.
Create a property in the UIViewController that you want to access the label from, just like you did with the label in but for ColorPickerViewController. Then when you push/present the new view, set it to self.
ColorPickerViewController *colorPickerViewController;
#propery (nonatomic, retain) ColorPickerViewController *colorPickerViewController;
and of course:
#syntesize colorPickerViewController
set it to self right before the view is presented:
viewThatYouArePresenting.colorPickerViewController = self.
[self.navigationController pushViewController:youViewController animated:YES]//Or whichever your using, this is just an example
then you can set it from the view like you were doing:
colorPickerViewController.Labelniz = #"xxxxx";
doing this:
ColorPickerViewController *controller = [[ColorPickerViewController alloc] init];
instantiates another instance of that controller so it is essentially changing the label of the newly instatiated ColorPickerViewController.What you want is to change the label in the ColorPickerViewController that is already instantiated.
I hope you are doing it this way
ColorPickerViewController *controller = [[ColorPickerViewController alloc] init];
controller.Labelniz = .......
ColorPickerViewController is the class, and controller is the object. you can access properties of a particular object (in this case the Labelniz property of the controller object).
I am trying to equal the string of an UITextView from anotherViewController to the MainViewController . I mean users write something in UITextView from anotherViewController and when touch the done button the string in UITextView should equal to another string in MainViewController . How can I access UITextView ???
I did something like this but did not get any result !
#import "AnotherViewController"
#class AnotherViewController;
#interface MainViewContoller : UIViewController {
NSString *main_string;
AnotherViewController *textEntered;
}
-(void)viewDidLoad {
textEntered.TEXT = main_string
}
***TEXT is the name my UITextView on AnotherViewController .
EDITED :
#interface AnotherViewController : UIViewController {
UITextView *TEXT;
id delegate;
}
#property (nonatomic, retain) IBOutlet UITextView *TEXT;
#property (nonatomic ,retain) id delegate;
#end
#implementation AnotherViewController
#synthesize TEXT ,delegate;
- (IBAction)doneButton {
//!!! problem with compare ! the compiler got me some warning
[self dismissModalViewControllerAnimated:YES];
}
MAIN VIEW CONTROLLER
#import "AnotherViewController.h"
#class Another...;
#interface MainViewControlelr : UIViewController {
NSString *main_string;
TextViewController *textEntered;
}
- (void)viewDidLoad
{
textEntered.delegate = self;
}
- (void) compareText {
[textEntered.TEXT isEqual:main_string];
}
Set mainViewController object as the delegate of an object of anotherViewController type. And use that delegate from AnotherViewController to pass messages to MainViewController.
Inside MainViewController, when you create your AnotherViewController object do:
anotherVC.delegate = self;
And in AnotherViewController, when your button action is done and you want to pass the text back, say store in a string. Then pass it to MainViewController as:
[delegate compareText:textView.text];
compareText with a NSString argument would be a method in your MainViewController class.
EDIT:
In your AnotherViewController.h interface, declare an ivar:
id delegate;
Then use #property (in .h) and #synthesize (in .m) for getter and setter.
Then do the above.
I think you created an AnotherViewController pointer but did not set it.
before you can access textEntered.TEXT, you should set you textEntered pointer to your AnotherViewController object.
Here is how:
on iGhalamViewController class, create a property for setting textEntered object like this in your interface declaration (probably in iGhalamViewController.h)
#property (assign) AnotherViewController *textEntered;
So, at the root place where do you create this viewcontroller objects both. After you create AnotherViewController and iGhalamViewController, set your textEntered pointer like this.
AnotherViewController* a = [[AnotherViewController alloc] initWithBlah:blah];
iGhalamViewController* b = [[iGhalamViewController alloc] initWithBlah:blah];
b.textEntered = a;
Then it should work like you expected. But use it in other method than viewDidLoad. viewDidLoad probaby gets called before you can set textEntered. A button press event method should be fine.