trying to update a UILabel on a parent view controller when dismissing the modal view

前端 未结 3 744
北海茫月
北海茫月 2020-12-06 21:53

I am trying to update a UILabel in a parent View after someone makes a change in a modal view. So, after they click \"save\" ... the newly entered value would change what t

相关标签:
3条回答
  • 2020-12-06 22:19

    in SWIFT:

    ParentViewController :

        func updateLabel() {
            yourLabel.text! = "what you need"
        }
    
    override func viewDidLoad() {
            super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.updateLabel), name: "DoUpdateLabel", object: nil) 
    }
    

    In OtherView:

    @IBAction func closePopUp(sender: AnyObject) {
    
           NSNotificationCenter.defaultCenter().postNotificationName("DoUpdateLabel", object: nil, userInfo: nil)
        }
    
    0 讨论(0)
  • 2020-12-06 22:22

    There are many ways to do this. One way is to use NSNotificationCenter to be able to do calls between different classes. So in the parent view you will have a function responsible for the update (lets call it updateLabel) and you will do the following:

    - (void) updateLabel
    {
        yourLabel.text = @"what you need";
    }
    
    - (void)viewDidLoad
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabel) name:@"DoUpdateLabel" object:nil];
    }
    

    Now in other view simply post a notification in the save button:

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

    EDIT: I have to mention 2 things here:

    1. In this scenario it is always preferable to have Shared Data Modal where you save your data in so you can access this data in any view in your program. In other words it is a good practice to separate the data from classes.
    2. Remember to resomve the NSNotificationCenter that you used in the main view by adding [[NSNotificationCenter defaultCenter] removeObserver:self];
    0 讨论(0)
  • 2020-12-06 22:28

    To elaborate on my comment. This is how I would implement a delegation method to update the label.

    In the header of the parent view controller:

    #import "ModalViewController.h"
    
    @interface ViewController : UIViewController <ModalViewControllerDelegate>
    
    /* This presents the modal view controller */
    - (IBAction)buttonModalPressed:(id)sender;
    
    @end
    

    And in the implementation:

    /* 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];
    }
    

    Here you see the delegation method in the top.

    The header of the modal view controller would contain the delegation protocol like this:

    @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
    

    The implementation of the modal view controller would contain a method similar to this one:

    /* 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];
    }
    

    When the save button is pressed, the delegate is notified and the text in your text view is sent through the delegation method.

    0 讨论(0)
提交回复
热议问题