如何访问另一个 ViewController 上的 UITextView [英] How to access a UITextView on another ViewController
问题描述
我试图将 UITextView 的字符串从 anotherViewController 等于 MainViewController .我的意思是用户从 anotherViewController 在 UITextView 中写一些东西,当触摸完成按钮时, UITextView 中的字符串应该等于 MainViewController 中的另一个字符串.我如何访问 UITextView ???
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 是我在 AnotherViewController 上的 UITextView 的名字.
***TEXT is the name my UITextView on AnotherViewController .
@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];
}
主视图控制器
#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];
}
推荐答案
将 mainViewController 对象设置为 anotherViewController 类型的对象的委托.并使用来自 AnotherViewController 的委托将消息传递给 MainViewController.
Set mainViewController object as the delegate of an object of anotherViewController type. And use that delegate from AnotherViewController to pass messages to MainViewController.
在 MainViewController 中,当你创建另一个视图控制器对象时:
Inside MainViewController, when you create your AnotherViewController object do:
anotherVC.delegate = self;
在AnotherViewController 中,当您的按钮操作完成并且您想将文本传回时,比如说存储在一个字符串中.然后将其传递给 MainViewController 为:
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 与 NSString 参数将是 MainViewController 类中的一个方法.
compareText with a NSString argument would be a method in your MainViewController class.
在您的 AnotherViewController.h 界面中,声明一个 ivar:
In your AnotherViewController.h interface, declare an ivar:
id delegate;
然后使用@property(.h)和@synthesize(.m)作为getter和setter.
Then use @property (in .h) and @synthesize (in .m) for getter and setter.
然后做上面的.
这篇关于如何访问另一个 ViewController 上的 UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!