【问题标题】:How to access a UITextView on another ViewController如何访问另一个 ViewController 上的 UITextView
【发布时间】:2011-09-19 14:58:35
【问题描述】:

我试图将 UITextView 的字符串从 anotherViewController 等于 MainViewController 。我的意思是用户从 anotherViewController 在 UITextView 中写一些东西,当触摸完成按钮时, UITextView 中的字符串应该等于 MainViewController 中的另一个字符串。如何访问 UITextView ???

我做了这样的事情,但没有得到任何结果!

#import "AnotherViewController"

@class AnotherViewController;

@interface MainViewContoller : UIViewController {

    NSString *main_string;
    AnotherViewController *textEntered;

}

-(void)viewDidLoad {

textEntered.TEXT = main_string

}

***TEXT 是我在 AnotherViewController 上的 UITextView 的名称。

已编辑:

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

}

【问题讨论】:

    标签: ios xcode uitextview


    【解决方案1】:

    将 mainViewController 对象设置为 anotherViewController 类型的对象的委托。并使用来自 AnotherViewController 的委托将消息传递给 MainViewController。

    在 MainViewController 中,当您创建 AnotherViewController 对象时:

    anotherVC.delegate = self;
    

    在另一个视图控制器中,当您的按钮操作完成并且您想要将文本传回时,请说存储在一个字符串中。然后将其传递给 MainViewController:

    [delegate compareText:textView.text];
    

    带有 NSString 参数的 compareText 将是 MainViewController 类中的一个方法。

    编辑:

    在您的 AnotherViewController.h 接口中,声明一个 ivar:

    id delegate;
    

    然后使用@property(在.h 中)和@synthesize(在.m 中)作为getter 和setter。

    然后执行上述操作。

    【讨论】:

    • 如何将我的 mainViewController 设置为委托?请你写代码好吗?我是小菜鸟
    • 抱歉,请您详细解释一下 compareText 吗??
    • compareText: 是一种您可以在 MainViewController 中编写的方法,它接受 NSString 作为参数。该参数将是它从 [delegate compareText:yourString] 收到的参数。 yourString 将是 textView 的文本。明白了吗?
    • 是的,但是我不知道 compareText 方法怎么写!例如 -(void) compareText :
    • 在 compareText 方法中,只比较两个字符串(MainViewController 中的字符串和你得到的字符串参数)。使用 [str1 isEqualToString:str2]。其中 str1 是 MainViewController 中的字符串,而 str2 是您获得的参数。如果两者都是相同的字符串,则返回 1。
    【解决方案2】:

    我认为您创建了 AnotherViewController 指针但没有设置它。

    在您可以访问 textEntered.TEXT 之前,您应该将 textEntered 指针设置为指向您的 AnotherViewController 对象。

    方法如下:

    在 iGhalamViewController 类上,创建一个属性,用于在接口声明中设置 textEntered 对象(可能在 iGhalamViewController.h 中)

    @property (assign) AnotherViewController *textEntered;
    

    因此,在您创建此视图控制器对象的根位置。创建 AnotherViewController 和 iGhalamViewController 后,像这样设置 textEntered 指针。

    AnotherViewController* a = [[AnotherViewController alloc] initWithBlah:blah];
    iGhalamViewController* b = [[iGhalamViewController alloc] initWithBlah:blah];
    b.textEntered = a;
    

    然后它应该像你预期的那样工作。但在 viewDidLoad 之外的其他方法中使用它。 viewDidLoad probaby 在您设置 textEntered 之前被调用。按钮按下事件方法应该没问题。

    【讨论】:

    • 废话! !! :D 我需要一些足够的方法
    • 很抱歉,但是有两种足够的方法可以解决;上面是伯恩的回答,代表团。一个是这个,只是参考。
    • 谢谢,请写下测试代码好吗?我是新手:(
    猜你喜欢
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 2020-03-05
    • 1970-01-01
    • 2020-11-04
    相关资源
    最近更新 更多