【问题标题】:Access text from UITextField in modal View Controller from Home View Controller从主视图控制器访问模态视图控制器中的 UITextField 中的文本
【发布时间】:2013-08-10 02:20:07
【问题描述】:

我有一个ViewController1 和一个UILabel,它可以呈现一个带有模态搜索的ViewController2。我在ViewController2 中有一个UITextField,我需要从ViewController1 访问它,因此我可以使用收集的文本设置我的UILabel

我尝试过使用prepareForSegue,但没有成功。我该怎么办?

编辑:

我正在使用委托,但我做错了什么。这是我在ViewController2.h 中使用的代码:

@class ViewController2;

@protocol VCProtocol

-(void)setName:(NSString *)name;

@end

@interface ViewController2 : UIViewController

@property (nonatomic, weak) id<VCProtocol> delegate;
@property (strong, nonatomic) IBOutlet UITextField *nameField;

- (IBAction)setButton:(id)sender

@end

ViewController2.m

-(IBAction)setButton:(id)sender
{
    [self.delegate setName:nameField.text];
}

我的ViewController1.h 符合VCProtocol。然后,在我的ViewController1.m 中,我有这个代码:

- (void)setName:(NSString *)name
{
    self.firstSignatureNameLabel.text = name;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqual:@"Sign"])
    {
        ViewController2 *VC = [segue destinationViewController];
        VC.delegate = self;
    }
}

【问题讨论】:

  • 如果您希望每个视图都可以访问它,您需要创建一个模型/数据类。这个问题不是重复的,但答案适用于this questionThis question might be more like yours
  • 这就是代表的用途。 VC1 在进行演示时应将自己设置为 VC2 的代理。 VC2 应该使用一个方法定义一个协议,您可以在文本字段的操作方法中调用委托。

标签: ios objective-c uitextfield


【解决方案1】:

您可以创建一个协议并将 VC1 设置为 VC2 的委托,使用prepareForSegue 将 VC1 设置为 VC2 的委托应该可以工作。我知道你说它不起作用,但我不明白为什么。试试这个:

给你的 segue 一个标识符(在故事板上),并实现prepareForSegue,如下所示:

VC2Delegate.h:

@protocol VC2Delegate
    - (void)updateLabel:(NSString *)text;
@end

VC1.h:

#import "VC2Delegate.h"
@interface VC1 : UIViewController <VC2Delegate>
    // All your stuff
@end

VC1.m:

- (void)updateLabel:(NSString *)text {
    [_label setText:text];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"YourSegueIdentifier"]) {
        VC2 * vc = [segue destinationViewController];
        [vc2 setDelegate:self];
    }
}

VC2.h:

#import "VC2Delegate.h"
@interface VC2 : UIViewController
    @property (weak, nonatomic) id<VC2Delegate>delegate;
@end

VC2.m

- (void)textWasUpdated { // or whatever method where you detect the text has been changed
    if (_delegate)
        [_delegate updateLabel:[_textView text]];
}

告诉我它是否有效。否则,prepareForSegue 是否也被调用了?

编辑:更新了我的答案(不完全是您需要的)。 但正如你所说,它不起作用:

  • 是否调用了 prepareForSegue ?
  • 如果是,是否调用了委托方法?
  • 如果委托方法没有被调用,检查委托不是nil

您可能想删除 segue,并使用presentViewController:animated:completion: 自己以模态方式呈现它,就像这样:

- (IBAction)buttonWasTapped {
    static NSString * const idModalView = @"modalView";
    static NSString * const storyBoardName = @"MainStoryBoard"
    UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
    VC2 * vc = [storyboard instantiateViewControllerWithIdentifier:idModalView];
    [vc setDelegate:self];
    [self.navigationController presentViewController:vc2 animated:YES completion:nil];
}

【讨论】:

  • 我会试试这个,@DCMaxxx 让你知道会发生什么。
【解决方案2】:

I ran into this problem 的时候,我选择了单例的方式,效果很好。

【讨论】:

    猜你喜欢
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 2012-09-17
    • 2012-08-23
    • 2011-06-26
    • 1970-01-01
    相关资源
    最近更新 更多