Use of Delegates to Communicate Between View Controllers

前端 未结 3 1129
遇见更好的自我
遇见更好的自我 2021-01-14 23:02

After asking some questions, I learned how to send orders from one view controller to another and managed to write the code its working but nothing happens...

In my

3条回答
  •  难免孤独
    2021-01-14 23:48

    Rereading your question, you ask how your first view controller can open the second view controller and set a text box. If that is, indeed, what you are trying to do, it's a far simpler question, no delegate protocol or delegates required at all.

    The two previous answers were informed by the discussion of delegates, but that's designed to solve a different problem. Delegates are only required if you need your second controller to pass something back to the first controller. But if you just want your second controller to receive something from the first controller, it's as simple as:

    //  FirstViewController.h
    
    #import 
    
    @interface FirstViewController : UIViewController
    
    @end
    

    with an implementation like:

    //  FirstViewController.m
    
    #import "FirstViewController.h"
    #import "SecondViewController.h"
    
    @implementation FirstViewController
    
    - (NSString *)generateRandomText
    {
        NSString *result;
    
        int random_num;
        random_num = (arc4random() % 5 - 1) + 1;
        if (random_num == 1)
            result = @"hello1";    
        else if (random_num == 2)
            result = @"hello2";
        else if (random_num == 3)
            result = @"hello3";
        else if (random_num == 4)
            result = @"hello4";
    
        return result;
    }
    
    // if you're using NIBs, it might be something like...
    // you only need this method if you're using NIBs and you've manually hooked a button up to this
    // if you're using segues, get rid of `goToNextViewController` and just use the following `prepareForSegue
    
    - (IBAction)goToNextViewController:(id)sender
    {
        SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
        secondController.textFromParent = [self generateRandomText];
        [self.navigationController pushViewController:secondController animated:YES];
    }
    
    // if you're using segues, give your segue an identifier, e.g. toSecondViewSegue, in Interface Builder and reference the exact same identifier here
    // if you're not using segues, you don't need this prepareForSegue method
    
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"toSecondViewSegue"])
        {
            SecondViewController *destinationController = segue.destinationViewController;
    
            destinationController.textFromParent = [self generateRandomText];
        }
    }
    
    @end
    

    And your second controller might look like:

    //  SecondViewController.h
    
    #import 
    
    @interface SecondViewController : UIViewController
    
    @property (strong, nonatomic) NSString *textFromParent;
    @property (weak, nonatomic) IBOutlet UILabel *label;
    
    @end
    

    With an implementation like:

    //  SecondViewController.m
    
    #import "SecondViewController.h"
    
    @implementation SecondViewController
    
    @synthesize textFromParent = _textFromParent;
    @synthesize label = _label;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.label.text = self.textFromParent;
    }
    
    @end
    

提交回复
热议问题