【问题标题】:trying to update a UILabel on a parent view controller when dismissing the modal view在关闭模式视图时尝试更新父视图控制器上的 UILabel
【发布时间】:2012-02-13 14:19:51
【问题描述】:

有人在模态视图中进行更改后,我正在尝试更新父视图中的 UILabel。所以,在他们点击“保存”之后……新输入的值会改变父视图控制器上显示的文本。

但是,我似乎无法让 UILabel 刷新新输入的值。

关于我可以尝试什么的任何想法?我已经尝试了一些东西,但是由于视图已经加载,所以没有任何东西得到“刷新”。

谢谢!

【问题讨论】:

  • 我会做一个委托方法。当按下“保存”按钮时,模式被解除并调用委托方法。父视图实现了这个委托方法,当它被调用时,标签被刷新。

标签: iphone uiview modalviewcontroller


【解决方案1】:

详细说明我的评论。这就是我将如何实现一个委托方法来更新标签。

在父视图控制器的头部:

#import "ModalViewController.h"

@interface ViewController : UIViewController <ModalViewControllerDelegate>

/* This presents the modal view controller */
- (IBAction)buttonModalPressed:(id)sender;

@end

在实现中:

/* Modal view controller did save */
- (void)modalViewControllerDidSave:(ModalViewController *)viewController withText:(NSString *)text
{
    NSLog(@"Update label: %@", text);
}

/* Prepare for segue */
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"modalSegue"])
    {
        ModalViewController *mvc = (ModalViewController *) segue.destinationViewController;
        mvc.delegate = self;
    }
}

/* Present modal view */
- (IBAction)buttonModalPressed:(id)sender
{
    [self performSegueWithIdentifier:@"modalSegue" sender:self];
}

在这里您可以看到顶部的委托方法。

模态视图控制器的标题将包含这样的委托协议:

@protocol ModalViewControllerDelegate;

@interface ModalViewController : UIViewController

@property (nonatomic, weak) id <ModalViewControllerDelegate> delegate;

- (IBAction)buttonSavePressed:(id)sender;

@end

@protocol ModalViewControllerDelegate <NSObject>
- (void)modalViewControllerDidSave:(ModalViewController *)viewController withText:(NSString *)text;
@end

模态视图控制器的实现将包含一个类似的方法:

/* Save button was pressed */
- (IBAction)buttonSavePressed:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(modalViewControllerDidSave:withText:)])
        [self.delegate modalViewControllerDidSave:self withText:@"Some text"];

    [self dismissModalViewControllerAnimated:YES];
}

按下保存按钮时,会通知委托,并通过委托方法发送文本视图中的文本。

【讨论】:

  • 这看起来很可靠。非常感谢。现在就要测试了。
  • 谢谢。可能想查看this answer 以获得更完整的解释,并提供一些强有力的建议,例如始终将委托属性设为弱引用。
  • 没错,代表绝对应该很弱。那是个错误。我已经更新了答案。谢谢。
【解决方案2】:

有很多方法可以做到这一点。一种方法是使用NSNotificationCenter 能够在不同类之间进行调用。因此,在父视图中,您将拥有一个负责更新的函数(我们称之为 updateLabel),您将执行以下操作:

- (void) updateLabel
{
    yourLabel.text = @"what you need";
}

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabel) name:@"DoUpdateLabel" object:nil];
}

现在在其他视图中只需在保存按钮中发布通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"DoUpdateLabel" object:nil userInfo:nil];

编辑: 我必须在这里提到两件事:

  1. 在这种情况下,最好使用共享数据模式来保存数据,这样您就可以在程序的任何视图中访问这些数据。换句话说,将数据与类分开是一种很好的做法。
  2. 记得通过添加[[NSNotificationCenter defaultCenter] removeObserver:self]; 来修复您在主视图中使用的NSNotificationCenter

【讨论】:

  • 工作完美,非常直接。谢谢!
【解决方案3】:

在 SWIFT 中:

父视图控制器:

    func updateLabel() {
        yourLabel.text! = "what you need"
    }

override func viewDidLoad() {
        super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.updateLabel), name: "DoUpdateLabel", object: nil) 
}

在其他视图中:

@IBAction func closePopUp(sender: AnyObject) {

       NSNotificationCenter.defaultCenter().postNotificationName("DoUpdateLabel", object: nil, userInfo: nil)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多