How can I pass the ApplicationViewController to a different class? - ios

I have a single view application that has a subview. That subview contains a form that includes e-mail textfield and password textfield. I need to set the delegates to those textfields to the "global" this.
Everything is written code, nothing on the storyboard.
How can I pass my ApplicationViewController to the textfields?

Add a #property for your viewController and pass that to your UIView class.
In your UIViewClass.h
#property (nonatomic, strong) UIViewController *yourViewController;
Then pass the current viewController to your UIView when you create it.
UIView *yourView = [UIView new];
yourView.yourViewController = self;
Then you can access your viewController directly from in your UIView class.
yourTextField.delegate = _yourViewController;

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.

Change NSMutableArray from subview

I have main view controller that contain an NSMutableArray. I want to open a subview with stepper to change quantity of a product included in that NSMutableArray.
How can I access to the NSMutableArray from subview and change an object inside it?
In your SubView class make a property of class NSMutableArray as
#interface SubViewClass : UIView
#property (strong, nonatomic) NSMutableArray *array;
#end
Now in your main view controller when calling this SubView you can assign your NSMutableArray instance into SubView array;
subViewInstance.array = mainControllerArray;
Now you can modify this array the way you want in your SubView Class.
Create a modal class. Then assign all the data to it and access that modal class from your other subview class.

ios Passing TextView from PushView to PresentingView

I am trying to do the following, and not able to find a straightforward answer.. It is related to this :Passing uitextfield from one view to another. But not exactly.
I have a Firstview.m, from which I push to a Secondview.m. The Secondview.m has a UITextView. I allow the user to edit the UITextView on Secondview.m. Now I want to store this text value in a variable in Firstview.m. One way to to do this is as follows
in Firstview.h
#property (nonatomic) Secondview *secondView;
That is keep a secondView variable in Firstview itself. But this doesn't seem efficient. Ideally I should only have 1 NSString text field in FirstView. What is the right way to do this ? Thanks
You can achieve this by using Delegation in Objective-C.
In your SecondView.h add following right after Header Inclusion
#protocol YourDelegateName <NSObject>
-(void)setText:(NSString *)strData;
#end
Also add delegate property to your header for accessing them in calling class, like below (This goes with other properties declaration in SecondView.h file):
#property (nonatomic, weak) id<YourDelegateName> delegate;
Now, Comes the calling the delegate part. Say, you want to save the text value of UITextView of SeconView in strTextViewData of FirstView class, when the following event occurs:
- (IBAction)save:(id)sender
{
[self.delegate setText:self.txtView.text]; // Assuming txtView is name for UITextView object
}
Now, In FirstView.h add YourDelegateName in delegate list like below:
#interface FisrtView : ViewController <YourDelegateName>
#property (nonatomic, reatin) NSString *strTextViewData;
#end
And then in FisrtView.m file when you create instance of SecondView class, set delegate to self like below:
SecondView *obj = [[SecondView alloc] initWithNibName:#"SeconView" bundle:nil];
obj.delegate = self; // THIS IS THE IMPORTANT PART. DON'T MISS THIS.
Now, Implement the delegate method:
-(void)setText:(NSString *)strData
{
self.strTextViewData = strData;
}
Applying this to your code will do what you want. Also, Delegation is one of the most important feature of Objective-C language, which - by doing this - you will get to learn.
Let me know, if you face any issue with this implementation.

How can I change the text from another view controller?

I'm trying this
SignUpViewController *userEmail=[[SignUpViewController alloc] init];
userEmail.emailAddress.text=email;
but is not working.
It seems like emailAddress (assuming UITextfeild) is not allocated properly when you called
SignUpViewController *userEmail=[[SignUpViewController alloc] init];
So, it is nil at the point of init.
Better way to do would be, added one public property in SignUpViewController of NSString. Save the value in that property. Like Below
SignUpViewController *userEmail=[[SignUpViewController alloc] init];
userEmail.emailString=email;
in SignUpViewController.h file add
#property (nonatomic, strong) NSString *emailString;
in SignUpViewController.m file in view did load
- (void)viewDidLoad{
//if you have not used nib or stroyboard init you textfield first
emailAddress.text=emailString;
}
Move your emailAddress #property into the .h file of SignUpViewController (Assuming you have an IBOutlet property set from Storyboard or Interface Builder
I think a better way of doing this is using a delegate to communicate between ViewControllers. See this answer for a quick example of creating a protocol for the delegate method, setting the second ViewController as the first one's delegate and then calling that method.

Setting the delegate of a UITextField/UITableView inside a Custom UIView to the managing ViewController

I have a CustomView UIView class that represents my screenUI in which I programatically create and position the UI elements, among which multiple UITextFields and a UITableView.
I also have a UIViewController and in it's loadView which I assign my CustomView to self.view.
I do this in order to best keep the cohesion of the view and the controller.
My question is how can I set the delegate of the UITextFields and UITableView inside the CustomView to the managing UIViewController?
It is my understanding that the UITextFieldDelegate and all other protocol methods should be implemented in the Controller and not the View, but I would like to avoid setting the delegate of the items in the view controller (e.g. textField1.delegate = self in the UIViewController class instead of textField1.delegate = <value for setting UIViewController as delegate> in my CustomView class) in order to keep my code cleaner.
Thank yous.
You could have a delegate property on your custom view:
customView.delegate = self;
self.view = customView;
In the delegate setter you could forward responsibilities:
-(void) setDelegate:(id<*the protocols you need*>) del
{
_textField.delegate = del;
...
}
You could have more than 1 type of delegate on your custom view, it depends on you needs, but one implementing all the protocols should usually suffice.
I would probably just forward them along. Create properties representing the delegates on your custom view. then in loadView set them to self.
// CustomView.h
#property (nonatomic, weak) id <UITextFieldDelegate> textFieldDelegate;
#property (nonatomic, weak) id <UITableViewDatasource> tableViewDataSource;
#property (nonatomic, weak) id <UITableViewDelegate> tableViewDelegate;
// Create textField
textField.delegate = self.textFieldDelegate;
// in ViewController
- (void)loadView
{
CustomView *view = //...
view.textFieldDelegate = self;
}

Resources