开发者

How to access a UITextView on another ViewController

开发者 https://www.devze.com 2023-04-05 17:18 出处:网络
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 bu

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 .

开发者_Go百科

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

}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号