Accessing label from other controller in ios - ios

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).

Related

Object from class in other class to access UIView iOS Objective-c

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.

iphone-Changing float in second ViewController

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

Modifying UIButton's alpha property from another class

I'm trying to change the alpha of an UIButton from another class. The function that is called in set the alpha property of my UIButton is actually called because I've put a NSLog there and I can see how it works. I'd be thankful if you could give me any suggestion.
Here's my current code.
ViewController.h
- (void) setAlphaToButton;
#property (strong, nonatomic) IBOutlet UIButton *myButton;
ViewController.m
#synthesize myButton;
- (void) setAlphaToButton {
myButton.alpha = 0.5;
NSLog(#"Alpha set");
}
ImageViewSubclass.m
- (void) tapDetected:(UITapGestureRecognizer *)tapRecognizer {
ViewController *VC = [[ViewController alloc] init];
[VC setAlphaToButton];
}
And when the image view is pressed, in my console I get: Alpha set. And the button doesn't change.
In your code, an instance of ViewController is alloced and inited, and the method setAlphaToButton is called on it. Then the view controller is released because you have no object retaining it. That's why you don't see any effect; the ViewController instance you call the method on never appears on screen.
It's not clear how your code is supposed to work; do you have an instance of ViewController in existence when tapDetected is called? If this is the case, and this is the ViewController whose button you want to alter the alpha of, then you need to have a reference to that instance of ViewController and call setAlphaToButton on it.
Your view is not loaded at the moment you trying to set alpha! You need to call this method after your viewDidLoad fired. You can force it by calling view, but it's kind of hackand not recommended!
MyViewController *vc = [MyViewController new];
vc.view; // this string will force view loading
[vc setAlphaToButton];
Add a property of uiviewcontroller class in imageviewsubclass as
ImageViewSubclass.h
#propery (nonatomic, retain) uiviewController *parent;
ImageViewSubclass.m
#synthesize parent;
And initialize it with "self" in view controller class when initalize object of imageviewsubclass and add on the view like
ImageViewsubclass *oneObj = [ImageViewsubClass alloc] init];
oneOBj.parent = self;
do the same for all objects of ImageviewsubClass objects.
and in
- (void) tapDetected:(UITapGestureRecognizer *)tapRecognizer {
[parent setAlphaToButton];
}

Access the value of text field from another class

I am making an app using a utility application template. I am trying to access the value of a UITextField from the FlipSideVewController class.
In the MainViewController.h file I have -
#interface MainViewController : UIViewController <UISplitViewControllerDelegate>{
UITextField *textField;
NSString *myText;
}
#property (retain, nonatomic) IBOutlet UITextField *textField;
#property (nonatomic, retain) NSString *myText;
-(IBAction)pressButton:(id)sender;
In the MainViewController.m file -
myText = [[NSString alloc] initWithFormat: textField.text];
NSLog(#"%#",myText);
I am creating the FlipSideViewController in the MainViewController class using the following code -
FlipsideViewController *controller = [[[FlipsideViewController alloc] initWithNibName:#"FlipsideViewController" bundle:nil] autorelease];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
This prints the value of the textfield in the console without any problems. The problem happens when I try to access the value of the textfield in the FlipSideVewController class (after the user presses the go button).
In the FlipViewController class I have -
MainViewController *obj = [[MainViewController alloc] init ];
NSString *abc = obj.textField.text;
NSLog(#"%#",abc);
The FlipSideVewController nib file is loaded fine without any problems. However the console output is (null) when in FlipSideVewController.
I will appreciate any help.
Thanks
If you use the Utility Xcode template, you should think of MainViewController and FlipSideVewController as given: the framework will instantiate them for you and make it available to the rest of the program as you define (either in your code or in IB). What I mean by this is that your line:
MainViewController *obj = [[MainViewController alloc] init ];
does not really do what you want.
In your case, what you seems to want is access a text field controlled by the existing MainViewController instance (the one that gives you the NSLog output correctly) from your other existing FlipSideVewController instance. This is a matter of "connectin" somehow those two controllers.
There are several ways to accomplish this. One is defining a "model" for your app and have it shared between the controllers. See also the Model-View-Controller pattern. A model is just a data structure that contains your "data"; you make that data structure available to both of your controllers. The easiest way to create such data structure and have it shared is through a singleton (I am not suggesting to use it as the best way, just noting that it is the easiest way, IMO).
Another, less clean way is passing a reference to MainViewController to FlipSideVewController and then accessing the text field through it. By example, you could define an ivar in your FlipSideVewController, then, where the two controllers are created, you do the assignment to the ivar.
You should go to your MainViewController and declare your textField as a property first and synthesize it, so you can access it using obj.textField. And if you have just created obj using alloc and init, you wont have any text in the textField instance Variable.
MainViewController.h
#property (retain) UITextField *textField;
MainViewController.m
#synthesize textField;
and you could use
myText=textField.text;
Now this should do it and you can access this textField by obj.textField in your other class. But you still wont get its value if you are initializing it in your other class because you will be creating a brand new obj whose textField.text will be blank( unless you have overrided its designated initializer to set the textField.text value).
Declare NSString *abc as instance variable
NSString *abc;
and then as property
#property (copy) NSString *abc;
#synthesize abc;
After you create your FlipSideViewController,
controller.abc=myText;
Remove the code where you create obj.
This will do it.

How to access a UITextView on another ViewController

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.

Resources