简体   繁体   中英

Pass UITextView contents to ModalViewController from parent view

I have a view with a UITextView and a button, both connected via an IBOutlet. Pressing the button brings up a ModalViewController. I need to pass the text in the UITextView to the ModalViewController as a string. I've done a bit of Googling but could only find info on passing strings from the ModalViewController to the parent view.

PS: Forgive my newbieness :p

You could subclass init with nib:bundle to include a string for the mycontroller view controller. So that in it's .h you will have

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andstring:(NSString *)string;

and in the viewcontroller's .m you will have

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andstring:(NSString *)string
{
   //initialize self here from bundle;
   //do other stuff
   self.text=string}

assuming you define a NSString *text propery in the header of the view controller which you present modally;

Then in where you define my controller you would do

MYVCController *mycontroller=[[MYVCController alloc] initWithNibName:nibname bundle:bundlename andstring:textView.text];

Is the ModalViewController object created and presented in an IBAction method? If so, in that method you should query the UITextView for its value (textView.text) and pass it to the ModalViewController.

Either you need to create a property for setting a variable inside the modal view controller, or you can define a method and do that.

eg

@interface ModalViewController : UIViewController {
NSString *displaystr;
}

@property (nonatomic, retain) NSString *displaystr;

...
@end

@implementation ModalViewController

@synthesize displaystr;

...

@end

Then you can use the property displaystr to store the text in the UITextField. You can get the text via the text property.

Make a NSString varible in appDelgate class and make it property.

NSString *value;

@property(nonatomic,retain) NSString *value;

and synthesize it in .m

@synthesize value;

Make object for you app delegate class and access that to set that value and get that value.

in modelViewController

write this code for button which you use to dismiss

 yourAppDelegateClass *objDelegate=(yourAppDelegate *)[[UIApplication sharedApplication] delegate];

 objDelegate.value=textView.text;

and by the object of app delegate you can access the value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM