Trying to save text and output it on different screen - ios

I've put together a text field that can take text and save it and output it into a label on the same controller page. My question is how do I use another controller page (using tab view controller) and output the same text on the second controller.
I've linked the label as the same IBOulet that the text is saved as.
Below is my code for the firstcontroller.m
#import "FirstViewController.h"
#interface FirstViewController ()
#property (nonatomic, strong) IBOutlet UILabel* label;
#end
#implementation FirstViewController
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
self.label.text = #"";
return TRUE;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
self.label.text = textField.text;
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
textField.text = #"";
}
Thanks in advance

You can use the UITabBarDelegate to listen when the user click on another tab to pass the text.
Please check this link Class reference
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
Or you can check this too Class reference
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
With this you could get the new view controller to assign the value in a good way.
Let me know if it helps you

Really, a UITextField is not a place to store data for any significant amount of time, as Model information should not be stored in a UIView. If you'd like this text to be accessible from multiple UIViewControllers, you should create a Model object that can store the string, and persist regardless of what happens with your view controllers.
You can pass this text to the model object by having your UIViewController be a delegate for the UITextField. The UIViewController should then be made to conform to <UITextFieldDelegate> so that you can listen for :
- (void)textFieldDidEndEditing:(UITextField *)textField
When this is called, pass the text to your model. The model could be set up with a Singleton pattern, just have 1 static model object accessible globally. When your app exits, you could write this object to disk using <NSCoding>, so that you can read it back in again next time the app starts.
For non-trivial data, I'd recommend using CoreData.
tl;dr- dont pass text to other controllers directly from the textfield.

Related

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.

UIResponder Notification?

Is there any sort of UIResponder Notification sent from objects that become a first responder? Or any way to know whether any UITextField or UITextView gets keyboard focus.
I have a ton of UITextFields/UItextViews in my application, and would like to add an input accessory view to all of them, but was hoping to avoid individually adding it in code to all their locations.
You can get your view controller to be a delegate of text views and text fields.
#interface MyViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate>
And then set it (in code or in the storyboard)
self.textField.delegate = self;
self.textView.delegate = self;
Then add these methods to your view controller
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
}
These will be called when the user selects the textField/textView.

passing data value between two view controller using custom protocol

1) I Am passing the value between two view controller using custom
protocol..But the value always showing NULL.
I need to pass the value from second view controller to first view controller
2) In Secondview controller.h
#protocol PopoverTableViewControllerDelegate <NSObject>
#property (nonatomic, strong) id<PopoverTableViewControllerDelegate>myDelegate;
3) secondview controller.m
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary*dict=[sercharray objectAtIndex:index];
str=[dict objectForKey:#"id"];
NSLog(#"test value %#",str);
[self.myDelegate didSelectRow:str];
NSLog(#"delegate value %#",self.myDelegate);
//THIS VALUE ALWAYS SHOWING NULL AND ALSO I SHOULD PASS THIS VALUE TO FIRST VIEW
CONTROLLER.I SHOULD USE DISMISS VIEW CONTROLLER.
[self dismissViewControllerAnimated:YES completion:nil];
}
4) First View controller.h
#interface Firstviewcontroller :
UIViewController<PopoverTableViewControllerDelegate>
5) First view controller.m
secondviewcontroller *next=[[seconviewcontroller alloc]init];
next.myDelegate=self;
(void)didSelectRow:(NSString *)cellDataString {
passstring = cellDataString;
NSLog(#"pass string %#",pass string);
//first view controller str variable value i need to pass this string[passstring].
}
I think you might be a little confused about what Delegation is used for and why. For example you might want to make a protocol in a UIViewController subclass if you were doing some kind of action in that ViewController and needed to inform another subclass that that action is being taken, or of the result of that action. Now in order for the subclass that wants to know about the action(the receiver), it has to conform to that protocol in it's header file. You also must "set" the delegate to the receiving class/controller. There are many ways to get a reference to the receiving controller/class to set it as the delegate but a common mistake is allocating and initializing a new instance of that class to set it as the delegate, when that class has already been created.What that does is set your newly created class as the delegate instead of the class that's already been created and waiting for a message. What your trying to do is just pass a value to a Newly created class. Since your just creating this UIViewController class all thats needed for that is a Property in the receiver(ViewControllerTwo). In your case a NSString:
#Property (nonatiomic, retain) NSString *string; //goes in ViewControllerTwo.h
and of course don't forget in the main:
#synthesize string; //Goes in ViewControllerTwo.m
Now there is no need for a setter in your ViewControllerTwo.
- (void)setString:(NSString *)str //This Method can be erased
{ //The setter is created for free
self.myString = str; // when you synthesized the property
}
The setter and Getters are free when you use the #synthesize. Just Pass the value over to the ViewController. The implementation is identical to your code except for the delegate:
ViewControllerTwo *two = [[ViewControllerTwo alloc] initWithNibName:#"ViewControllerTwo" bundle:nil];
[two setString:theString];
[self.navigationController pushViewController:two animated:YES];
[two release];

Determine textfield from toolbar IOS

I am trying to set up undo and redo for each textfield and unsure how to figure out how to determine which text field is the first responder.
Is there an argument I can pass into the methods called by the buttons from the toolbar, or do I need to do some fancy footwork?
This is an idea:
If the viewController becomes delegate of each textField, then the viewController will get notified as each textField's value changes, or becomes first responder.
To adopt the delegation, you will do:
#interface MyViewController : UIViewController <UITextFieldDelegate>
#end
#implementation
- (void)someMethod{
// for a series of textfields
myTextfield1.delegate = self;
myTextfield1.delegate = self;
// or you hook the delegate in IB
}
// then you get notified
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// textField here that gets passed in as an argument is the first responder
// if you have, let's say tag number for each
NSInteger activeTextFieldTag = textField.tag;
}
#end
Here is the reference to UITextFieldDelegate Protocol

Check current responder?

I'm making an application that requires the use of multiple textFields with number pads as there first responder. I have created an image to use as a negative button that will be an addition to the number pad.
I am wondering if there is a way to check which textField the number pad is typing to.
Any help would be appreciated!
If all of your potential first responders are UITextFields, another approach would be to conform your controller class to UITextFieldDelegate protocol, and then grab a reference to the currently editing UITextView at the time it begins editing.
Conform your class in your .h:
MyController : NSObject <UITextFieldDelegate> //Might often be a UIViewController rather than an NSObject subclass...
Define a property:
#property (weak, nonatomic) UITextField *editingField;
Then synthesize in your .m:
#synthesize editingField = __editingField
Then implement:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self setEditingField:textField];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self setEditingField:nil];
}
Now, whenever you want to know which text field is your first responder:
UITextField *firstResponder = [self editingField];
If there are only a limited number of them, you can query each with [textFieldN isFirstResponder]. If you want a general purpose utility, you can look at each subview in a view and see whether it is the first responder or whether any of its subviews farther down the hierarchy are.

Resources