MVC: Passage of the methods in different classes

ginosal

I'm implementing an app iOS with the MVC design pattern.

The app has 5 interfaces and I proceeded in this way:

  • AppDelegate (CONTROLLER);
  • WebServiceModel (MODEL);
  • 5 interfaces that represent the 5 views of the app (VIEWS).

In the model I have implemented a method that sends messages to the web service to request the data. According to the MVC, the Controller must receive data from the Model and send them to the View, so in the Controller I have implemented a method that invokes the method of the Model. In the View I instantiate an object Controller and call the Controller method. When the app starts, Xcode starts up only the commands of the method of AppDelegate (Controller) without reading the call to the Model method.

I apologize if the reasoning is twisted. In summary:

// AppDelegate.h

#import "WebServiceModel.h"
@interface AppDelegate: UIResponder <UIApplicationDelegate> {
WebServiceModel *model;
}

@property (retain, nonatomic) WebServiceModel *model;
- (void) func;
_________________

// AppDelegate.m

@implementation AppDelegate
@syntesize model;

- (void) func {
    NSLog(@"OK!");
    [model function];
}
@end
_________________

// WebServiceModel.h

#import "AppDelegate.h"
@interface WebServiceModel: NSObject <NSXMLParserDelegate> {
AppDelegate *controller;
}

- (void) function;
_________________

// WebServiceModel.m

@implementation WebServiceModel

- (void) function {
    NSLog(@"YES!");
    //other instructions
}
@end
_________________

// View Controller.h

#import "AppDelegate.h"
@interface ViewController: UIViewController {
AppDelegate *controller;
}

_________________

// ViewController.m

@implementation ViewController

- (void) viewDidLoad {
    NSLog(@"OH!");
    controller = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    [controller func];
}
@end

When the app starts, in "All Output" you see only "OH!" and "OK!", but no "YES!".

Because the Model's method "function" isn't called?

Thanks to those who answer me!

David Doyle

You haven't actually created an instance of the model object, so what's actually happening is you're invoking -function on nil. Fixing this is easy, add the following method to AppDelegate:

- (id)init
{
  self = [super init];
  if (nil != self)
  {
    self.model = [[WebServiceModel alloc] init];
  }
  return self;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I properly work with calling methods on related but different classes in C#

分類Dev

Three classes with common methods

分類Dev

Is this the way to change my asp.net mvc controller to call different methods using async?

分類Dev

Calculate time execution for different methods

分類Dev

C++: Passing classes as argument to methods of other classes

分類Dev

Testing concrete methods in abstract classes using subclasses

分類Dev

Java inheritance - Two classes that use the same methods

分類Dev

What are the naming rules for CIL Classes and Methods

分類Dev

Python OOP: inefficient to put methods in classes?

分類Dev

c# multiple classes same methods

分類Dev

Select elements with different classes (Not one element with multiple classes)

分類Dev

Different landing page with MVC 4

分類Dev

Single struct adjusting methods to different data types

分類Dev

Reading same input twice, in two different methods

分類Dev

Passing multiple arguments with tidyeval - different methods?

分類Dev

Different methods for initializing embedding layer weights in Pytorch

分類Dev

Common methods for different page objects in Cucumber

分類Dev

Dictionary to switch between methods with different arguments

分類Dev

Spring Security: Different authentication methods depending on entity

分類Dev

Java - Separating Class Methods into Different Files

分類Dev

Rails - using form_for tag with different methods

分類Dev

Read Same Text File in Different Methods

分類Dev

Compile package classes with different Java versions

分類Dev

Make java class Comparable to 2 different Classes

分類Dev

How to access variables from different classes in tkinter?

分類Dev

creating elements with jquery and giving them different classes

分類Dev

Toggle classes between two different elements

分類Dev

How to sync two threads in different classes

分類Dev

Windows Phone Classes with same name but different folders

Related 関連記事

  1. 1

    How do I properly work with calling methods on related but different classes in C#

  2. 2

    Three classes with common methods

  3. 3

    Is this the way to change my asp.net mvc controller to call different methods using async?

  4. 4

    Calculate time execution for different methods

  5. 5

    C++: Passing classes as argument to methods of other classes

  6. 6

    Testing concrete methods in abstract classes using subclasses

  7. 7

    Java inheritance - Two classes that use the same methods

  8. 8

    What are the naming rules for CIL Classes and Methods

  9. 9

    Python OOP: inefficient to put methods in classes?

  10. 10

    c# multiple classes same methods

  11. 11

    Select elements with different classes (Not one element with multiple classes)

  12. 12

    Different landing page with MVC 4

  13. 13

    Single struct adjusting methods to different data types

  14. 14

    Reading same input twice, in two different methods

  15. 15

    Passing multiple arguments with tidyeval - different methods?

  16. 16

    Different methods for initializing embedding layer weights in Pytorch

  17. 17

    Common methods for different page objects in Cucumber

  18. 18

    Dictionary to switch between methods with different arguments

  19. 19

    Spring Security: Different authentication methods depending on entity

  20. 20

    Java - Separating Class Methods into Different Files

  21. 21

    Rails - using form_for tag with different methods

  22. 22

    Read Same Text File in Different Methods

  23. 23

    Compile package classes with different Java versions

  24. 24

    Make java class Comparable to 2 different Classes

  25. 25

    How to access variables from different classes in tkinter?

  26. 26

    creating elements with jquery and giving them different classes

  27. 27

    Toggle classes between two different elements

  28. 28

    How to sync two threads in different classes

  29. 29

    Windows Phone Classes with same name but different folders

ホットタグ

アーカイブ