3

我想以编程方式添加myView到父视图。但它不会出现在屏幕上。代码有什么问题?

@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];
}
4

2 回答 2

8

代替

@property (nonatomic,weak) UIView *myView;

@property (strong, nonatomic) UIView *myView;

它应该工作:)

于 2012-08-30T11:22:30.010 回答
2
@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];
}

试试这个,它会工作......</p>

于 2013-05-24T07:58:11.783 回答