Reputation: 71
I have a custom class called "Fraction" and its method "display" in which i'd like to show on View a UILabel:
-(void)display{
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 200, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setText:@"______"];
[self.view addSubview:myLabel];
}
but I get this error on the last line:
Proprety 'view' not found on object of type 'Fraction *'
How can I solve?
(iPhone "single view" application, Xcode 4.5.2)
Upvotes: 0
Views: 109
Reputation: 1152
Check whether ‘self’ is a ViewController. addSubView is a method in UIView.
Upvotes: 0
Reputation: 287
In view controller.h
Fraction* fraction;
In view controller.m add this
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 200, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setText:[fraction methodForLabelString]];
[self.view addSubview:myLabel];
Try this
Upvotes: 0
Reputation: 14304
You're trying to add a subview to something that isn't a UIView - in your 'display' method's context, 'self' refers to an object of type Fraction. If Fraction doesn't have a UIView property called view and doesn't inherit from one that does (such as UIViewController), there is no reason for it to recognise what you're trying to do there. The correct way to do this would be to have an instance method in "Fraction" that returns an NSString with the fraction's representation. Use this returned value to do anything that the view requires, such as adding it to a UILabel's text property.
It's important to understand correct MVC - in your example, you're completely abusing the MVC abstraction and having a data object affect the view. (M and V should not be interacting directly).
Upvotes: 4