Accessing a property from another class

后端 未结 3 1986
余生分开走
余生分开走 2021-01-29 13:52

Im trying to build an app where the user inputs a number at a textfield and after pressing the button another view shows up with the result of that input.

Each number th

3条回答
  •  时光说笑
    2021-01-29 14:44

    Since Library in this case is acting like your model it is your ViewController's responsibility to set and pull the data from it. The library does not (and should not) know anything about the ViewController class. This is a good solution to the problem you are facing:

    So first, you will need a Library instance in your ViewController:

    @interface ViewController ()
    // Make sure you import library above
    @property (strong, nonatomic) Library *library;
    @end
    

    Create the actual instance:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.library = [Library new];
    }
    

    Then in your button click you should access your view's text field and set it on the library:

    - (IBAction)showResultView:(id)sender {
        self.library.number = [[self.fieldLabel text] intValue];
    }
    

提交回复
热议问题