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

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.

Related

Creating a class to get all buttons delegates

I have a UIViewController class called , classA . This class is connected to buttons outlets of its connected view (from storyboard ).
What i would like to do , is to create another classB , that will handle all the buttons actions.
My question is, how can i set the delegates of a button in classA to be handled in classB, and how i make classB to be alive for all that time.
I was thinking about this .
in classA :(the main view controller that have the buttons outlets )
ClassB *buttonsResponder=[[classB alloc]init]; //initialize buttons class
[self.button addTarget:buttonsResponder action:#selector(do:) forControlEvents:UIControlEventTouchDown];
My main problem is, when classB handle the action, and lets say he has to open some mail composer view controller, how can classB open that mail composer "inside" classA view ? or even some UIView that i want to show in classA , but from classB
I was thinking of sending a pointer to the current view controller as an argument of the button, but i dont know how, and if it will work :
[self.button addTarget:buttonsResponder action:#selector(do:and
controller pointer?) forControlEvents:UIControlEventTouchDown];
The best way to keep ClassB alive in ClassA instance is make it a private property in class extension:
// In CalssA
#property (nonatomic, strong) ClassB *buttonsResponder;
You need to allocate it somewhere, viewDidLoad is good place:
self.buttonsResponder = [[classB alloc]init];
and in the same method you can make it a button(s) delegate:
[self.button addTarget:self.buttonsResponder action:#selector(do:) forControlEvents:UIControlEventTouchDown];
// Extended
If you want to present another view controller from your ClassB you can create property to hold weak reference to classA in classB:
// In ClassB.h
#property (nonatomic, weak) ClassA *myClassA;
Pass a reference to of classA when you initialise classB in viewDidLoad:
// In ClassA.m
self.buttonsResponder = [[classB alloc]init];
self.buttonsResponder.myClassA = self;
And when you want to present another view controller you can do it from classB:
[self.myClassA presentViewController:...];
[self.button addTarget:self.buttonsResponder action:#selector(do:)
forControlEvents:UIControlEventTouchDown];
If you want to present another viewcontroller from buttonsResponder:
[self.window.rootViewController presentViewController:anotherViewController animated:YES completion:nil];

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];
}

Using my own delegate

I´m having problems declarating my own delegate. Well...thats not exactly true: i have it declarated and, when i build the project, the compiler reports no issues. I declarated it in this way:
I made a file (enviarDatos.h) for declare the protocol:
#protocol enviarDatos <NSObject>
- (void)addItemViewController:(NSMutableArray *)item;
#end
In the Vista2.h (ViewController) file I imported the file enviarDatos.h and declared a property:
#property (nonatomic, weak) id <enviarDatos> delegare;
In the Vista2.m (ViewController) file I use the protocol method:
#interface ViewController : UIViewController <enviarDatos> {
And, finally, in the ViewController.m file I implement the delegates method:
- (void)addItemViewController:(NSMutableArray *)ar {
origen = ar;
}
Does anyone see something wrong? the code of the last function its never executing.
Thanks for your help.
EDIT:
What i need is to change an array in ViewController from Vista2 (another viewcontroller)
Then create delegate property in next view(child view) & set it to self in parent view while pushing or showing child view.
ParentView.m
1.Implement protocol methods
- (void)addItemViewController:(NSMutableArray *)ar
{
origen = ar;
}
2.While showing child view
ChildViewController *child = [[ChildViewController alloc] init];
child.delegate = self;
//present child view
ChildView.h
#property (nonatomic, weak) id <enviarDatos> delegare;
ChildView.m
-(void) anyMethod
{
if([self.delegate respondsToSelector:#selector(addItemViewController:)])
{
[self.delegate addItemViewController:mutableArray];
}
}
Ah, it looks like you are declaring the delegate property in the wrong place.
You should declare the property delegate in enviarDatos.h.
#property (nonatomic, weak) id <enviarDatos> delegate;
Then in Vista2.m you will do something like this...
EnviarDatos *myObject = [[EnviarDatos alloc] init];
myObject.delegate = self;
This then sets up the EnviarDatos object and assigns the Vista2 object as the delegate.
Now, in EnviarDatos.m you can run...
[self.delegate addItemViewController:someObjectArray];
And this will then run that code in the Vista2 object.
Delegates are used for calling back to objects that create them (or some other objects). If you create an object and then want to run a method in it then you won't need a delegate.
Can you say at what condition addItemViewController is invoked?
You seem to be on the right track, but are you sure you are setting the delegate as
[yourObject setDelegate: self];
Have you tried debugging it? Does the debugger pause at addItemViewController if you set a breakpoint there? Can you confirm the delegate is not null inside the method? I may post some code but your seems to be right except for the assigning of delegate, I think you should check it.

Accessing label from other controller in 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).

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.

Resources