Reputation: 3230
I wanted to programmatically add myView
to parent view. But it doesn't show up on screen. What's wrong with the code?
@interface ViewController ()
@property (nonatomic,weak) UIView *myView;
@end
@implementation ViewController
@synthesize myView = _myView;
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewRect = CGRectMake(10, 10, 100, 100);
self.myView = [[UIView alloc] initWithFrame:viewRect];
self.myView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.myView];
}
Upvotes: 3
Views: 10834
Reputation:
@interface ViewController ()
@end
@implementation ViewController
UIView *myView;
-(void)viewDidLoad
{
[super viewDidLoad];
CGRect viewRect = CGRectMake(10, 10, 100, 100);
myView = [[UIView alloc] initWithFrame:viewRect];
myView.backgroundColor = [UIColor redColor];
[self.view addSubview:myView];
}
try this it will work…
Upvotes: 2
Reputation: 146
Replace
@property (nonatomic,weak) UIView *myView;
with
@property (strong, nonatomic) UIView *myView;
and it should work :)
Upvotes: 8