在UIView上设置属性

时间:2014-03-24 11:50:57

标签: ios uiview properties

我需要在UIView的{​​{1}}上设置属性。听起来很简单(UIViewControllerMorePlanDetails),

UIView

MorePlanDetails上的属性以通常的方式定义:

MorePlanDetails *moreDetails = [[MorePlanDetails alloc] initWithFrame:CGRectMake(10, 60, self.view.frame.size.width-20, self.view.frame.size.height-80)];
moreDetails.plan = self.plan;
[self.navigationController.view addSubview:moreDetails];

并合成:

@property (nonatomic, strong) PFObject *plan;

应该这样做,不是吗?但由于某种原因,@synthesize plan = _plan; 会停留moreDetails.plan。我错过了什么?

3 个答案:

答案 0 :(得分:0)

如评论中所述,请使用self.plan。但实际上你甚至不需要添加" @ synthesize"再过,只要你有@property,编译器就会为你生成getter和setter。

添加:

@property (nonatomic, strong) PFObject *plan;

代码中的参考:

self.plan = whatever

答案 1 :(得分:0)

如果你将moredetails.plan变为null,那么self.plan返回的值为null如果使用调试器调试代码而不需要使用synthesize属性,则可以更清晰。

答案 2 :(得分:0)

问题很简单。我在- (id)initWithFrame:(CGRect)frame初始化我的小部件 - 这取决于计划对象 - 但是仅在下一行中设置计划对象。所以很明显它对于小部件来说是空的。

解决方案是简单地在单独的方法中初始化小部件,并且只有在moreDetails上设置计划后才调用它。

工作代码:

MorePlanDetails *moreDetails = [[MorePlanDetails alloc] initWithFrame:CGRectMake(10, 60, self.view.frame.size.width-20, self.view.frame.size.height-80)];
moreDetails.plan = self.plan;
[moreDetails initWidgets];