Passing text from UITextView to string in different ViewController - objective-c

I need to pass the text of a UITextView in viewController1 to viewController2 and set the text to a string in viewController2. I thought I had the code figured out, but it turns out that nothing is being passed.
In viewController2.h I added
viewController1*v1
and assigned a property to it.
in the viewController2.m
-(void)method{
v1=[[viewController1 alloc] initWithNibName:#"viewController1" bundle:nil];
NSString *text=[[NSString alloc]init];
[v1.tweetText setText:text];
}
This is probably the wrong way to go about it, so does anyone have other solutions to this problem?

Thats the correct way to send data to another vc, but you are not setting the variable text to anything. Try setting it to hello and see if you get hello as the tweetText in the other vc.

Related

Set Text Field from another object ios

I am stuck trying to set and retrieve the text in a UITextField object on titleViewController.
I have a Journal object that has a reference to the titleViewController object, but I can not seem to set the text value for the UITextField.
I have an outlet to the UITextField object in the titleViewController, and Journal can see it, but when I write to code to set the value nothing happens when it is run.
There is no errors that pop up in XCode or the Log. I know it should work as the set up works for accessing custom methods in other such viewControllers being managed by the same Journal.
-EDIT-
Here is a code sample as requested:
//Get viewController from Array
TitleVC *titlePage = [_PageArray objectAtIndex:0];
//Get string from Dictionary
NSString *test = [_saveDictionary objectForKey:#"Name"];
//This is one of the attempts to set the UITextField
//FIXED by Tomy211(but still does not work)
[titlePage.textName setText:test];
//This is attempting dot notation
titlePage.textName.text = test
titlePage.textName = test //Wrong I know but had to test
//Also made sure test was a proper string
NSLog(test) //Displayed "Dan" as Expected
This is in Journal.m which accessed a reference to the titleVC from a storyboard using this code:
-(UIViewController *)loadView:(NSString *)viewID{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:viewID];
return viewController;
}
The ViewController returned from that method is used in displaying the View in a scrollview, and I havent had an issue with accessing methods from the ViewControllers this way.
All I want to do is let Journal.m set and read the value from textName which is an outlet connected to a UITextField.
-UPDATE-
I checked and discovered I can read the value of the textField but not write to it.
-FINAL UPDATE-
I found the issue with my code. The code was correct(minus the correction by Tomy211), but the issue was where I was calling the code. I was calling the method before the view was displayed, so when the view did get displayed it revert back to the default values.
Note to all that have similar issues:
Check to make sure the view is being displayed before updating a value of a UIObject part of the view. If you have to update values with the view not displayed, use instance variables and have the UIObject get their values from them on the viewDidLoad method.
The problem was I tried to update the TextField objects before the view has been loaded.
Moving the method call to after the point I load the view fixes the problem. I have updated my question to reflect what was correct, and what was incorrect.
Note to all that have similar issues: Check to make sure the view is being displayed before updating a value of a UIObject part of the view. If you have to update values with the view not displayed, use instance variables and have the UIObject get their values from them on the viewDidLoad method.

How Do I validate UITextfield with Correct Answer String?

I have a UITexfield in Xcode that the user inputs their answer to a question. If the answer is correct they are taken to another screen. All the help I have found only shows how to verify the amount of characters or numbers in a string.
I have declared
NSString *answer = #"The Correct Answer";
and
if ([UITextField1.text isEqualToString:#"The Correct Answer"])
then ...
This is where I get lost (sorry total newbie) I have been able to compare these in NSlog in foundation.h file but get really lost when trying to deal with .h and .m files in xcode.
Can anyone please check if with the above I am on the right track and please explain how I get to display the new screen when the string/answer is correct.
Thanks in Advance
The check you are making is correct. It checks the precise text of the TextField1 with the precise text of #"The Correct Answer". If it matches you can make your navigationController push a new view.
ea. Like this:
MyViewController *myVC = [[MyViewController alloc] init];
//do something with your new ViewController.view
[self.navigationController pushViewController:myVC animated:YES];
Of course you will have to adjust the new View to your own needs. Also make sure you have access to that navigationController.
But yeah, this is all very basic and you should be able to figure it out for yourself with some proper tutorials.
Well I'm not clear on your question.
If you are asking how to show a screen once a user typed the right answer in textbox then use:
Set textfield's delegate to your view controller then handle this method in your view controller:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Just do the needful there, something like:
if ([textField.text isEqualToString:#"The Correct Answer"])
{
[self presentModalViewController:correctVC animated:NO];
return NO;
}
return YES;

iOS - Custom keyboard, updating text field in parent view

I am implementing a custom keyboard and need to update the original textField when buttons are touched on that keyboard.
I have tried following the answer to this question:
Return Inputs to UITextfield with custom inputView
but I don't find it very clear.
In statViewController I have defined my textField xValue, and created an instance of my keyboard:
DCKeyboard *dckXValue = [[DCKeyboard alloc] initWithNibName:#"DCKeyboard" bundle:nil];
The next line of the answer uses self, so I take it that it's suggesting that I should create a property of dckXValue that is a statViewController:
dckXValue.objStatViewController = self;
Again, I follow the answer through (add the #class line, create the object/property) but then in my method that catches my button presses (in DCKeyboard.m), when I reach the line:
objStatViewController.xValue.text = #"Some value";
I am getting the error "Request for member 'xValue' in something not a structure or union" when I try to compile.
Can anyone give me any hints on where I might be going wrong?
Thanks!
It's a problem of property the text field is not a property. Try #import "StatViewController.h" in your implementation file

Problem with NSCollectionView

I'm quite a rookie when it comes to Cocoa programming, so I hope some experts can hint me on the right direction with a problem I'm facing.
I have a NSCollectionView that is bound to a NSArrayController (A) of elements having two properties: an URL to a picture and a NSArray (B) of elements of another class.
For every element inside the NSArrayController (A) , I load a subview with a NSImageView that should display the image in the URL and a NSTableView that is bound to the elements of the NSArray (B).
Everything works fine, except for one very important thing: the URL to the picture is not immediately available when I create the subview, it becomes available later, but when it does, I don't see the picture loading in the NSImageView.
I would think of a problem in the KVC/KVO implementation, but the strange thing is that the image loads correctly if, when the URL becomes available, the subview is not visible (e.g in a portion of the scrollview that is not displayed).
Example: The NSScrollview size is such that it can display only two subviews at a time. I add three elements to the NSArrayController (A): the first two images don't load, if I scroll down the scrollview to see the third element, I find the image loaded correctly.
Any ideas about what could cause such a strange behaviour?
Thank you in advance
Luca
series.h
#interface Series : NSObject {
#private
NSMutableString * banner;
}
-(Series*)initWithEpisode:(Episode*)ep;
-(void)setBanner:(NSString*)_banner;
#property (retain, readwrite) NSMutableString *banner;
#end
series.m
#implementation Series
#synthesize banner;
-(Series*)initWithEpisode:(Episode*)ep
{
self = [super init];
if(self){
banner = [[NSMutableString alloc]initWithString:#"some invalid URL"];
}
-(void) setBanner:(NSString*)_banner
{
[banner setString:[NSString stringWithFormat:#"some root path/%#", _banner];
}
-(void)dealloc
{
[super dealloc];
[banner release];
}
SeriesListViewController.m
-(void)VideoChangedStatus:(Episode*)ep{
//This is the delegate called by the object Episode when it retrieves the URL via asynchronous call to a webservice
Series *tmp = [[Series alloc]initWithEpisode:ep];
[[seriesList objectAtIndex:[seriesList indexOfObject:tmp]]setBanner:[ep banner]];
}
The binding is done in the subview nib file, to the NSImageView: I set File's Owner of type NSCollectionViewItem and then bind Valueurl to representedObject.banner
I didn't subclass NSCollectionView nor NSCollectionViewItem
After days of trying I found a solution that works: apparently it's not enough to use the setString method, I need to re-intialize the property inside the setBanner method
-(void) setBanner:(NSString*)_banner
{
banner = [NSMutableString[NSString stringWithFormat:#"some root path/%#", _banner]];
}
Still, I'd be very glad to know if someone has an explanation of why setString was causing that strange (to me) problem and why this solution works.
Thank you
Luca
I’m not sure why you’ve declared banner to be a mutable string — it looks like an immutable string would suffice.
At any rate, when you write a custom setter method you need to send -willChangeValueForKey: and -didChangeValueForKey: to ensure KVO (and hence bindings) compliance:
-(void) setBanner:(NSString*)_banner
{
[self willChangeValueForKey:#"banner"];
[banner setString:[NSString stringWithFormat:#"some root path/%#", _banner];
[self didChangeValueForKey:#"banner"];
}

Update uitextView from another controller

I want to update uitextview from another uiviewcontroller classes.
Can anyone help me?
It is not exactly clear what you are asking, but I think you mean that you want to send a message to a UITextView instance within another UIViewController subclass.
So long as this UITextView is a member of this UIViewController, you can send messages to it. (Of course, it must be declared as a property for getters and setters).
Example:
OtherViewController *otherViewController = [[OtherViewController alloc] init];
[[otherViewController textView] setText:#"your text here"];
[[self navigationController] pushViewController:otherViewController animated:YES];
I suppose this could be useful if you were about to switch push or pop a view controller and needed to add information to it before you did so. Hopefully this answered your question.
you can update your textview's text using NSNotificationCenter from other viewcontroller.