18422

How to access a UITextView on another ViewController

Question:

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

Answer1:

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.

Answer2:

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.

Recommend

  • How do I convert RGB into HSV in Cocoa Touch?
  • _iVar vs. iVar_ for variable naming [closed]
  • How to get subviews frames early in initialization process
  • Adding child progress for multiple file download
  • playing video in iOS 5
  • How can I figure out what UITableViewCell I'm clicking on during a search?
  • Java catching exceptions and subclases
  • Printing input from TextField to a Label in Xcode with Swift
  • Do stored procedures have the ability to delete a file from the OS?
  • Change color of row programmatically in WatchKit
  • Unload image of UIImageView thats offscreen
  • send data back from jsp iterator to struts action class
  • UITableViewCell subclass wrong image in cell or old image bug
  • iOS - MKOverlayView custom view rect fills works, but line draws do not
  • Allocating a 2D contiguous array within a function
  • Using extern @class in order to add a category?
  • Aligning image on right side of title on UIButton with imageEdgeInsets and titleEdgeInsets
  • Specifying virtual keyboard type for EditText in XML
  • Creating PDF from TIFF image using iText
  • abstracting over a collection
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • Can't remove headers after they are sent
  • Hide HTML elements without javascript, only CSS
  • C++ pointer value changes with static_cast
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • Not able to aggregate on nested fields in elasticsearch
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • how to save the state in userdefaults of accessory checkmark-iphone
  • Database structure design with variable amounts of fields
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • MySQL WHERE-condition in procedure ignored
  • Web-crawler for facebook in python
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • trying to dynamically update Highchart column chart but series undefined
  • How to stop GridView from loading again when I press back button?
  • Checking variable from a different class in C#
  • java string with new operator and a literal