mardi 31 mars 2015

What is the proper way to implement MVC pattern in iOS app?

I'm trying to make a clear MVC project. So is it good or bad idea to use NSNotificationCenter's observers for communication between UIViews and ViewControllers?


For example in custom CustomView.m i make a button:



- (void) makeSomeButton{
....
[bt addTarget:self action:@(buttonWasClicked) forControlEvents:UIControlEventTouchDragInside];
...
}

- (void) buttonWasClicked {
[[NSNotificationCenter defaultCenter] postNotificationName:@"buttonWasClicked" object:nil];
}


And in the viewCotroller.m i'm adding observer in init section:



- (void)viewDidLoad { //
[self.view addSubview: [[CustomView alloc] initWithFrame ...]];
.....
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@(buttonWasClicked) name:@"buttonWasClicked" object:nil];
.....
}

then
- (void) buttonWasClicked{
// button was clicked in customView so do something
}


If it's not correct, please, explain what is the proper way to implement MVC pattern in iOS app?


Aucun commentaire:

Enregistrer un commentaire