netmajor
netmajor

Reputation: 6585

Send from UIView class information to UIViewController

I have controller

#import <UIKit/UIKit.h>
#import "ViewBoard.h"

@interface BallsViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *InfoLabel;
@property (weak, nonatomic) IBOutlet UIButton *nextBallButton;
@property (weak, nonatomic) IBOutlet UILabel *PointLabel;
@property (weak, nonatomic) IBOutlet ViewBoard *viewBoard;

- (IBAction)NewGame:(id)sender;

@end





#import "BallsViewController.h"
#import "Field.h"
@interface BallsViewController ()
@end

@implementation BallsViewController
@synthesize viewBoard;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.viewBoard Draw:@"Fields"];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)NewGame:(id)sender {
    self.viewBoard.canDrawBall = true;
    [self.viewBoard Draw:@"Fields"];

  }
@end

And UIView

@interface ViewBoard : UIView

@end

@implementation ViewBoard
-(void)sendScoreToUI{
int score = 10;
}
@end

How can I send information about score to UI and there set it to label ? I want to UIView send this information to controller than controller get it from UIView.

Upvotes: 0

Views: 225

Answers (1)

viggio24
viggio24

Reputation: 12366

Consider MVC, Model - View - Controller. The View is ViewBoard. The Controller is BallsViewController, where the app logic is contained. The Model should be the score.

So you have 3 choices on how to manage Model. Note that in my case the app logic is always inside the controller so is the controller that manages the game and the score, and not the UI.

Choice-1 : strict MVC

the score is modelled as an independent object. In such case you define a "Score" class, you send score updates from the controller to the model and let the view to listen for model changes:


@interface Score
@property (nonatomic,assign) NSInteger points;
@end
@implementation Score
@synthesize points;
@end

Then the controller instantiates the object score:


Score *myScore;

updates it when an scoring event happens:


[myScore setPoints:30];

Finally you can use KVO to let the ViewBoard to listen for changes of "points" property on myScore. So inside the controller, after myScore is initialised:


[myScore addObserver:self.viewBoard forKeyPath:@"points" options:NSKeyValueOptionNew context:NULL];

Note: the model and the view are linked by KVO only. So the view doesn't change the score, and the model notifies view only thanks to the KVO process. When the controller disappears, the KVO link is broken.

Choice-2 : the model is inside the controller In such case you just add a new property to your controller:


@property (nonatomic,assign) NSInteger points;

and every time you update the score you send the new value to the view (which updates itself). You can do this in the points setter: each time you update your internal points property you also ask viewBoard to update itself.

[self setPoints:30];

-(void)setPoints:(NSInteger)newPoints { points = newPoints; [self.viewBoard updatePoints:points]; }

Choice-3 : the model is inside the view This approach is simple but usually not recommended, because normally you don't want to add a strong dependency between the controller and the view representation (this happens because your view requirements could affect the way the view controller updates its logic). Also a limitation is that in a view unload event you could lose the score. In such case you add the points property to the view:


@property (nonatomic,assign) NSInteger points;

and in your view controller you can change the points in this way:


[self.viewBoards setPoints:30];

finally your view "setPoints:" setter will contain some "refresh" logic:


-(void)setPoints:(NSInteger)newPoints {
  points = newPoints;
  [self setNeedsLayout];
}

-(void)layoutSubviews {
  // here you update the subview
  [self.pointsLabel setText:[NSString stringWithFormat:@"%d",self.points]];
}



Upvotes: 2

Related Questions